Merging annotations in XFDF with pdf

1,299 views
Skip to first unread message

sagar priyadarshi

unread,
Jan 7, 2012, 5:37:50 AM1/7/12
to PDFTron PDFNet SDK
Dear All

Till now, I could see few details and sample about merging form data
with pdf.
But now, I am trying to merge xfdf with pdf file where xfdf does not
contain form data, instead it contains
annotations (and highlights, comments) xml segment and that I need to
merge with pdf.

A samle xfdf can be like below:

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><f
href="test.pdf"/><ids original="" modified=""/><annots>

<text icon="Comment" title="spp07339" creationdate="D:
20120105153702+0539;3039;" subject="Note" page="0" date="D:
20120105153710+0539;3039;" flags="28" name="null"
rect="161.891144,240.813141,179.891281,258.812927" color="#CC3399">
<contents-richtext><body xmlns="http://www.w3.org/1999/xhtml"
xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"
xfa:APIVersion="Acrobat:7.0.8" xfa:spec="2.0.2" style="font-size:
12.0pt;text-align:left;color:#000000;font-weight:normal;font-
style:normal;font-family:Arial;font-stretch:normal"><p><span
style="font-family:Arial;font-size:12.0pt">Test by admin</span></p></
body></contents-richtext><popup open="no" page="0" date="D:
20120105153710+0539;3039;" flags="28" name=""
rect="161.891144,164.812744,361.891144,276.812744" /></text>
</annots></xfdf>


Using pdf-xchange viewer, xfdf can be imported to pdf file and a new
pdf file is generated with annotations and markup.

But I want to know if this can be done with PDFTron or any other way
for this?
I understand that XFDF and FDF are mainly for form data but I am sure
xfdf has other purposes also.

Regards
Sagar

Support

unread,
Oct 2, 2012, 10:00:33 PM10/2/12
to pdfne...@googlegroups.com

The support for XFDF merge/extract (doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only), SaveAsXFDF()) was recently added as part of PDFNet v.5.8.2). For an example of how to use API please see the attached (updated) FDF sample project:

 

/// PDFNet includes full support for FDF (Forms Data Format) and for merging/extracting annotation and
/// forms data (FDF, XFDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
/// available in PDFNet.


static void Main(string[] args)
{
 PDFNet.Initialize();

 // Relative path to the folder containing test files.
 string input_path =  "../../../../TestFiles/";
 string output_path = "../../../../TestFiles/Output/";

 // Example 1)
 // Iterate over all form fields in the document. Display all field names.
 try 
 {
  PDFDoc doc = new PDFDoc(input_path + "form1.pdf");
  doc.InitSecurityHandler();
    
  FieldIterator itr;
  for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
  {
   Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
   Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());

   Console.Write("Field type: ");
   Field.Type type = itr.Current().GetType();
   switch(type)
   {
    case Field.Type.e_button:
     Console.WriteLine("Button"); break;
    case Field.Type.e_text:
     Console.WriteLine("Text"); break;
    case Field.Type.e_choice:
     Console.WriteLine("Choice"); break;
    case Field.Type.e_signature:
     Console.WriteLine("Signature"); break;
   }

   Console.WriteLine("------------------------------");
  }

  doc.Close();
  Console.WriteLine("Done.");
 }
 catch (PDFNetException e)
 {
  Console.WriteLine(e.Message);
 }

 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
 try 
 {
  // XFDF to FDF
  // form fields
  Console.WriteLine("Import form field data from XFDF to FDF.");
    
  FDFDoc fdf_doc1 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf"));
  fdf_doc1.Save(output_path + "form1_data.fdf");
    
  // annotations
  Console.WriteLine("Import annotations from XFDF to FDF.");
    
  FDFDoc fdf_doc2 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf"));
  fdf_doc2.Save(output_path + "form1_annots.fdf");
    
  // FDF to PDF
  // form fields
  Console.WriteLine("Merge form field data from FDF.");
    
  PDFDoc doc = new PDFDoc(input_path + "form1.pdf");
  doc.InitSecurityHandler();
  doc.FDFMerge(fdf_doc1);
    
  // To use PDFNet form field appearance generation instead of relying on
  // Acrobat, uncomment the following two lines:
  // doc.RefreshFieldAppearances();
  // doc.GetAcroForm().Put("NeedAppearances", Obj.CreateBool(false));
    
  doc.Save(output_path + "form1_filled.pdf", SDFDoc.SaveOptions.e_linearized);
    
  // annotations
  Console.WriteLine("Merge annotations from FDF.");
    
  doc.FDFMerge(fdf_doc2);
  doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc.SaveOptions.e_linearized);
  doc.Close();

  Console.WriteLine("Done.");
 }
 catch (PDFNetException e)
 {
  Console.WriteLine(e.Message);
 }

 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
 try 
 {
  // PDF to FDF
  PDFDoc in_doc = new PDFDoc(output_path + "form1_filled_with_annots.pdf");
  in_doc.InitSecurityHandler();
    
  // form fields only
  Console.WriteLine("Extract form fields data to FDF.");
    
  FDFDoc doc_fields = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_forms_only);
  doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf");
  doc_fields.Save(output_path + "form1_filled_data.fdf");
    
  // annotations only
  Console.WriteLine("Extract annotations to FDF.");
    
  FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only);
  doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf");
  doc_annots.Save(output_path + "form1_filled_annot.fdf");
    
  // both form fields and annotations
  Console.WriteLine("Extract both form fields and annotations to FDF.");
    
  FDFDoc doc_both = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
  doc_both.SetPdfFileName("../form1_filled_with_annots.pdf");
  doc_both.Save(output_path + "form1_filled_both.fdf");
    
  // FDF to XFDF
  // form fields
  Console.WriteLine("Export form field data from FDF to XFDF.");
    
  doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf");
    
  // annotations
  Console.WriteLine("Export annotations from FDF to XFDF.");
    
  doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf");
    
  // both form fields and annotations
  Console.WriteLine("Export both form fields and annotations from FDF to XFDF.");
    
  doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf");
    
  in_doc.Close();
  Console.WriteLine("Done.");
 }
 catch (PDFNetException e)
 {
  Console.WriteLine(e.Message);
 }

 // Example 4) Read FDF files directly
 try 
 {
  FDFDoc doc = new FDFDoc(output_path + "form1_filled_data.fdf");
  FDFFieldIterator itr = doc.GetFieldIterator();
  for(; itr.HasNext(); itr.Next())
  {
   Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
   Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
   Console.WriteLine("------------------------------");
  }

  Console.WriteLine("Done.");
 }
 catch (PDFNetException e)
 {
  Console.WriteLine(e.Message);
 }

 // Example 5) Direct generation of FDF.
 try 
 {
  FDFDoc doc = new FDFDoc();

  // Create new fields (i.e. key/value pairs).
  doc.FieldCreate("Company", (int)Field.Type.e_text, "PDFTron Systems");
  doc.FieldCreate("First Name", (int)Field.Type.e_text, "John");
  doc.FieldCreate("Last Name", (int)Field.Type.e_text, "Doe");
  // ...  

  // doc.SetPdfFileName("mydoc.pdf");
  doc.Save(output_path + "fdf_sample_output.fdf");
  Console.WriteLine("Done. Results saved in fdf_sample_output.fdf");
 }
 catch (PDFNetException e)
 {
  Console.WriteLine(e.Message);
 }

 PDFNet.Terminate();
}

 

 

 

  http://www.pdftron.com/ID-f94ls2-edm0aqZ/PDFNet.zip

  http://www.pdftron.com/ID-f94ls2-edm0aqZ/PDFNet64.zip

  http://www.pdftron.com/ID-f94ls2-edm0aqZ/PDFNetDotNet4.zip

  http://www.pdftron.com/ID-f94ls2-edm0aqZ/PDFNet64DotNet4.zip

Reply all
Reply to author
Forward
0 new messages