//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- import pdftron.PDF.*; import pdftron.SDF.SDFDoc; import pdftron.FDF.*; //--------------------------------------------------------------------------------------- // PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract // forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality // available in PDFNet. //--------------------------------------------------------------------------------------- public class FDFTest { public static void main(String[] args) { PDFNet.initialize(); PDFNet.setResourcesPath("../../../resources"); // 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(); for(FieldIterator itr = doc.getFieldIterator(); itr.hasNext();) { Field current=(Field)(itr.next()); System.out.println("Field name: " + current.getName()); System.out.println("Field partial name: " + current.getPartialName()); System.out.print("Field type: "); int type = current.getType(); switch(type) { case Field.e_button: System.out.println("Button"); break; case Field.e_text: System.out.println("Text"); break; case Field.e_choice: System.out.println("Choice"); break; case Field.e_signature: System.out.println("Signature"); break; } System.out.println("------------------------------"); } System.out.println("Done."); } catch(Exception e) { e.printStackTrace(); } // Example 2) Merge data from FDF try { System.out.println("Merge data from FDF."); PDFDoc doc=new PDFDoc((input_path + "form1.pdf")); doc.initSecurityHandler(); FDFDoc fdf_doc=new FDFDoc((input_path + "form1_data.fdf")); doc.fdfMerge(fdf_doc); // To use PDFNet form field appearance generation instead of relying on // Acrobat, uncomment the following two lines: //doc.refreshFieldAppearances(); //doc.getAcroForm().putBool("NeedAppearances", false); doc.save((output_path + "form1_filled.pdf"), SDFDoc.e_linearized, null); System.out.println("Done."); } catch(Exception e) { e.printStackTrace(); } // Example 3) Extract data to FDF try { System.out.println("Extract data to FDF."); PDFDoc in_doc=new PDFDoc((output_path + "form1_filled.pdf")); in_doc.initSecurityHandler(); FDFDoc doc = in_doc.fdfExtract(); doc.setPDFFileName("../form1.pdf"); doc.save((output_path + "form1_filled_data.fdf")); System.out.println("Done."); } catch(Exception e) { e.printStackTrace(); } // Example 4) Read FDF files directly try { FDFDoc doc=new FDFDoc((output_path + "form1_filled_data.fdf")); for(FDFFieldIterator itr = doc.getFieldIterator(); itr.hasNext();) { FDFField current=(FDFField)(itr.next()); System.out.println("Field name: " + current.getName()); System.out.println("Field partial name: " + current.getPartialName()); System.out.println("------------------------------"); } System.out.println("Done."); } catch(Exception e) { e.printStackTrace(); } // Example 5) Direct generation of FDF. try { FDFDoc doc=new FDFDoc(); // Create new fields (i.e. key/value pairs). doc.fieldCreate("Company", Field.e_text, "PDFTron Systems"); doc.fieldCreate("First Name", Field.e_text, "John"); doc.fieldCreate("Last Name", Field.e_text, "Doe"); // ... // doc.setPdfFileName("mydoc.pdf"); doc.save((output_path + "sample_output.fdf")); System.out.println("Done."); } catch(Exception e) { e.printStackTrace(); } PDFNet.terminate(); } }