PDF Form Fill, Form Data Extraction with Forms Data Format (FDF) - Java Sample Code

Sample code for using Apryse SDK to programmatically merge forms data with the PDF in order to fill forms, or to extract form field data from the PDF. Apryse SDK has full support for Forms Data Format (FDF). Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our full PDF Data Extraction SDK Capabilities.

To start your free trial, get stated with Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.SDFDoc;
8import com.pdftron.fdf.*;
9
10
11//---------------------------------------------------------------------------------------
12// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract
13// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
14// available in PDFNet.
15//---------------------------------------------------------------------------------------
16public class FDFTest {
17 public static void main(String[] args) {
18
19 PDFNet.initialize(PDFTronLicense.Key());
20
21 // Relative path to the folder containing test files.
22 String input_path = "../../TestFiles/";
23 String output_path = "../../TestFiles/Output/";
24
25 // Example 1)
26 // Iterate over all form fields in the document. Display all field names.
27 try (PDFDoc doc = new PDFDoc((input_path + "form1.pdf"))) {
28 doc.initSecurityHandler();
29
30 for (FieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
31 Field current = itr.next();
32 System.out.println("Field name: " + current.getName());
33 System.out.println("Field partial name: " + current.getPartialName());
34
35 System.out.print("Field type: ");
36 int type = current.getType();
37 switch (type) {
38 case Field.e_button:
39 System.out.println("Button");
40 break;
41 case Field.e_check:
42 System.out.println("Check");
43 break;
44 case Field.e_radio:
45 System.out.println("Radio");
46 break;
47 case Field.e_text:
48 System.out.println("Text");
49 break;
50 case Field.e_choice:
51 System.out.println("Choice");
52 break;
53 case Field.e_signature:
54 System.out.println("Signature");
55 break;
56 case Field.e_null:
57 System.out.println("Null");
58 break;
59 }
60
61 System.out.println("------------------------------");
62 }
63
64 System.out.println("Done.");
65 } catch (Exception e) {
66 e.printStackTrace();
67 }
68
69 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
70 try (PDFDoc doc = new PDFDoc((input_path + "form1.pdf"))) {
71 // XFDF to FDF
72 // form fields
73 System.out.println("Import form field data from XFDF to FDF.");
74
75 FDFDoc fdf_doc1 = FDFDoc.createFromXFDF((input_path + "form1_data.xfdf"));
76 fdf_doc1.save(output_path + "form1_data.fdf");
77 // output FDF fdf_doc1
78
79 // annotations
80 System.out.println("Import annotations from XFDF to FDF.");
81
82 FDFDoc fdf_doc2 = FDFDoc.createFromXFDF((input_path + "form1_annots.xfdf"));
83 fdf_doc2.save(output_path + "form1_annots.fdf");
84 // output FDF fdf_doc2
85
86 // FDF to PDF
87 // form fields
88 System.out.println("Merge form field data from FDF.");
89
90 doc.initSecurityHandler();
91 doc.fdfMerge(fdf_doc1);
92
93 // Refreshing missing appearances is not required here, but is recommended to make them
94 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
95 doc.refreshAnnotAppearances();
96
97 doc.save((output_path + "form1_filled.pdf"), SDFDoc.SaveMode.LINEARIZED, null);
98 // output PDF doc
99
100 // annotations
101 System.out.println("Merge annotations from FDF.");
102
103 doc.fdfMerge(fdf_doc2);
104 // Refreshing missing appearances is not required here, but is recommended to make them
105 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
106 doc.refreshAnnotAppearances();
107 doc.save(output_path + "form1_filled_with_annots.pdf", SDFDoc.SaveMode.LINEARIZED, null);
108 System.out.println("Done.");
109 } catch (Exception e) {
110 e.printStackTrace();
111 }
112
113 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
114 try (PDFDoc in_doc = new PDFDoc((output_path + "form1_filled_with_annots.pdf"))) {
115 // PDF to FDF
116 in_doc.initSecurityHandler();
117
118 // form fields only
119 System.out.println("Extract form fields data to FDF.");
120
121 FDFDoc doc_fields = in_doc.fdfExtract(PDFDoc.e_forms_only);
122 doc_fields.setPDFFileName("../form1_filled_with_annots.pdf");
123 doc_fields.save(output_path + "form1_filled_data.fdf");
124 // output FDF doc_fields
125
126 // annotations only
127 System.out.println("Extract annotations to FDF.");
128
129 FDFDoc doc_annots = in_doc.fdfExtract(PDFDoc.e_annots_only);
130 doc_annots.setPDFFileName("../form1_filled_with_annots.pdf");
131 doc_annots.save(output_path + "form1_filled_annot.fdf");
132 // output FDF doc_annots
133
134 // both form fields and annotations
135 System.out.println("Extract both form fields and annotations to FDF.");
136
137 FDFDoc doc_both = in_doc.fdfExtract(PDFDoc.e_both);
138 doc_both.setPDFFileName("../form1_filled_with_annots.pdf");
139 doc_both.save(output_path + "form1_filled_both.fdf");
140 // output FDF doc_both
141
142 // FDF to XFDF
143 // form fields
144 System.out.println("Export form field data from FDF to XFDF.");
145
146 doc_fields.saveAsXFDF((output_path + "form1_filled_data.xfdf"));
147 // output FDF doc_fields
148
149 // annotations
150 System.out.println("Export annotations from FDF to XFDF.");
151
152 doc_annots.saveAsXFDF((output_path + "form1_filled_annot.xfdf"));
153 // output FDF doc_annots
154
155 // both form fields and annotations
156 System.out.println("Export both form fields and annotations from FDF to XFDF.");
157
158 doc_both.saveAsXFDF((output_path + "form1_filled_both.xfdf"));
159 // output FDF doc_both
160
161 System.out.println("Done.");
162 } catch (Exception e) {
163 e.printStackTrace();
164 }
165
166 // Example 4) Merge/Extract XFDF into/from PDF
167 try (PDFDoc in_doc = new PDFDoc((input_path + "numbered.pdf"))) {
168 // Merge XFDF from string
169 in_doc.initSecurityHandler();
170
171 System.out.println("Merge XFDF string into PDF.");
172
173 String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xfdf xmlns=\"http://ns.adobe.com/xfdf\" xml:space=\"preserve\"><square subject=\"Rectangle\" page=\"0\" name=\"cf4d2e58-e9c5-2a58-5b4d-9b4b1a330e45\" title=\"user\" creationdate=\"D:20120827112326-07'00'\" date=\"D:20120827112326-07'00'\" rect=\"227.7814207650273,597.6174863387978,437.07103825136608,705.0491803278688\" color=\"#000000\" interior-color=\"#FFFF00\" flags=\"print\" width=\"1\"><popup flags=\"print,nozoom,norotate\" open=\"no\" page=\"0\" rect=\"0,792,0,792\" /></square></xfdf>";
174
175 FDFDoc fdoc = FDFDoc.createFromXFDF(str);
176 in_doc.fdfMerge(fdoc);
177 in_doc.save(output_path + "numbered_modified.pdf", SDFDoc.SaveMode.LINEARIZED, null);
178 // output PDF in_doc
179 System.out.println("Merge complete.");
180
181 // Extract XFDF as string
182 System.out.println("Extract XFDF as a string.");
183
184 FDFDoc fdoc_new = in_doc.fdfExtract(PDFDoc.e_both);
185 String XFDF_str = fdoc_new.saveAsXFDF();
186 System.out.println("Extracted XFDF: ");
187 System.out.println(XFDF_str);
188 System.out.println("Extract complete.");
189 } catch (Exception e) {
190 e.printStackTrace();
191 }
192
193 // Example 5) Read FDF files directly
194 try {
195 FDFDoc doc = new FDFDoc((output_path + "form1_filled_data.fdf"));
196
197 for (FDFFieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
198 FDFField current = itr.next();
199 System.out.println("Field name: " + current.getName());
200 System.out.println("Field partial name: " + current.getPartialName());
201
202 System.out.println("------------------------------");
203 }
204 doc.close();
205 System.out.println("Done.");
206 } catch (Exception e) {
207 e.printStackTrace();
208 }
209
210 // Example 6) Direct generation of FDF.
211 try {
212 FDFDoc doc = new FDFDoc();
213 // Create new fields (i.e. key/value pairs).
214 doc.fieldCreate("Company", Field.e_text, "PDFTron Systems");
215 doc.fieldCreate("First Name", Field.e_text, "John");
216 doc.fieldCreate("Last Name", Field.e_text, "Doe");
217 // ...
218
219 // doc.setPdfFileName("mydoc.pdf");
220
221 doc.save(output_path + "sample_output.fdf");
222 // output FDF doc
223 doc.close();
224 System.out.println("Done. Results saved in sample_output.fdf");
225 } catch (Exception e) {
226 e.printStackTrace();
227 }
228
229 PDFNet.terminate();
230 }
231}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales