FDF

Sample Java 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). Learn more about our Android SDK and PDF Data Extraction SDK Capabilities.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.fdf.FDFDoc;
13import com.pdftron.fdf.FDFField;
14import com.pdftron.fdf.FDFFieldIterator;
15import com.pdftron.pdf.Field;
16import com.pdftron.pdf.FieldIterator;
17import com.pdftron.pdf.PDFDoc;
18import com.pdftron.sdf.SDFDoc;
19
20import java.util.ArrayList;
21
22public class FDFTest extends PDFNetSample {
23
24 private static OutputListener mOutputListener;
25
26 private static ArrayList<String> mFileList = new ArrayList<>();
27
28 public FDFTest() {
29 setTitle(R.string.sample_fdf_title);
30 setDescription(R.string.sample_fdf_description);
31 }
32
33 @Override
34 public void run(OutputListener outputListener) {
35 super.run(outputListener);
36 mOutputListener = outputListener;
37 mFileList.clear();
38 printHeader(outputListener);
39
40
41 // Example 1)
42 // Iterate over all form fields in the document. Display all field names.
43 try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "form1.pdf").getAbsolutePath()))) {
44 doc.initSecurityHandler();
45
46 for (FieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
47 Field current = itr.next();
48 mOutputListener.println("Field name: " + current.getName());
49 mOutputListener.println("Field partial name: " + current.getPartialName());
50
51 mOutputListener.print("Field type: ");
52 int type = current.getType();
53 switch (type) {
54 case Field.e_button:
55 mOutputListener.println("Button");
56 break;
57 case Field.e_check:
58 mOutputListener.println("Check");
59 break;
60 case Field.e_radio:
61 mOutputListener.println("Radio");
62 break;
63 case Field.e_text:
64 mOutputListener.println("Text");
65 break;
66 case Field.e_choice:
67 mOutputListener.println("Choice");
68 break;
69 case Field.e_signature:
70 mOutputListener.println("Signature");
71 break;
72 case Field.e_null:
73 mOutputListener.println("Null");
74 break;
75 }
76
77 mOutputListener.println("------------------------------");
78 }
79
80 mOutputListener.println("Done.");
81 } catch (Exception e) {
82 mOutputListener.printError(e.getStackTrace());
83 }
84
85 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
86 try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "form1.pdf").getAbsolutePath()))) {
87 // XFDF to FDF
88 // form fields
89 mOutputListener.println("Import form field data from XFDF to FDF.");
90
91 FDFDoc fdf_doc1 = FDFDoc.createFromXFDF((Utils.getAssetTempFile(INPUT_PATH + "form1_data.xfdf").getAbsolutePath()));
92 fdf_doc1.save(Utils.createExternalFile("form1_data.fdf", mFileList).getAbsolutePath());
93
94 // annotations
95 mOutputListener.println("Import annotations from XFDF to FDF.");
96
97 FDFDoc fdf_doc2 = FDFDoc.createFromXFDF((Utils.getAssetTempFile(INPUT_PATH + "form1_annots.xfdf").getAbsolutePath()));
98 fdf_doc2.save(Utils.createExternalFile("form1_annots.fdf", mFileList).getAbsolutePath());
99
100 // FDF to PDF
101 // form fields
102 mOutputListener.println("Merge form field data from FDF.");
103
104 doc.initSecurityHandler();
105 doc.fdfMerge(fdf_doc1);
106
107 // Refreshing missing appearances is not required here, but is recommended to make them
108 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
109 doc.refreshAnnotAppearances();
110
111 doc.save((Utils.createExternalFile("form1_filled.pdf", mFileList).getAbsolutePath()), SDFDoc.SaveMode.LINEARIZED, null);
112
113 // annotations
114 mOutputListener.println("Merge annotations from FDF.");
115
116 doc.fdfMerge(fdf_doc2);
117 // Refreshing missing appearances is not required here, but is recommended to make them
118 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
119 doc.refreshAnnotAppearances();
120 doc.save(Utils.createExternalFile("form1_filled_with_annots.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
121 mOutputListener.println("Done.");
122 } catch (Exception e) {
123 mOutputListener.printError(e.getStackTrace());
124 }
125
126 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
127 try (PDFDoc in_doc = new PDFDoc((Utils.createExternalFile("form1_filled_with_annots.pdf", mFileList).getAbsolutePath()))) {
128 // PDF to FDF
129 in_doc.initSecurityHandler();
130
131 // form fields only
132 mOutputListener.println("Extract form fields data to FDF.");
133
134 FDFDoc doc_fields = in_doc.fdfExtract(PDFDoc.e_forms_only);
135 doc_fields.setPDFFileName(Utils.createExternalFile("form1_filled_with_annots.pdf", mFileList).getAbsolutePath());
136 doc_fields.save(Utils.createExternalFile("form1_filled_data.fdf", mFileList).getAbsolutePath());
137
138 // annotations only
139 mOutputListener.println("Extract annotations to FDF.");
140
141 FDFDoc doc_annots = in_doc.fdfExtract(PDFDoc.e_annots_only);
142 doc_annots.setPDFFileName(Utils.createExternalFile("form1_filled_with_annots.pdf", mFileList).getAbsolutePath());
143 doc_annots.save(Utils.createExternalFile("form1_filled_annot.fdf", mFileList).getAbsolutePath());
144
145 // both form fields and annotations
146 mOutputListener.println("Extract both form fields and annotations to FDF.");
147
148 FDFDoc doc_both = in_doc.fdfExtract(PDFDoc.e_both);
149 doc_both.setPDFFileName(Utils.createExternalFile("form1_filled_with_annots.pdf", mFileList).getAbsolutePath());
150 doc_both.save(Utils.createExternalFile("form1_filled_both.fdf", mFileList).getAbsolutePath());
151
152 // FDF to XFDF
153 // form fields
154 mOutputListener.println("Export form field data from FDF to XFDF.");
155
156 doc_fields.saveAsXFDF((Utils.createExternalFile("form1_filled_data.xfdf", mFileList).getAbsolutePath()));
157
158 // annotations
159 mOutputListener.println("Export annotations from FDF to XFDF.");
160
161 doc_annots.saveAsXFDF((Utils.createExternalFile("form1_filled_annot.xfdf", mFileList).getAbsolutePath()));
162
163 // both form fields and annotations
164 mOutputListener.println("Export both form fields and annotations from FDF to XFDF.");
165
166 doc_both.saveAsXFDF((Utils.createExternalFile("form1_filled_both.xfdf", mFileList).getAbsolutePath()));
167
168 mOutputListener.println("Done.");
169 } catch (Exception e) {
170 mOutputListener.printError(e.getStackTrace());
171 }
172
173 // Example 4) Merge/Extract XFDF into/from PDF
174 try (PDFDoc in_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "numbered.pdf").getAbsolutePath()))) {
175 // Merge XFDF from string
176 in_doc.initSecurityHandler();
177
178 mOutputListener.println("Merge XFDF string into PDF.");
179
180 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>";
181
182 FDFDoc fdoc = FDFDoc.createFromXFDF(str);
183 in_doc.fdfMerge(fdoc);
184 in_doc.save(Utils.createExternalFile("numbered_modified.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.LINEARIZED, null);
185 mOutputListener.println("Merge complete.");
186
187 // Extract XFDF as string
188 mOutputListener.println("Extract XFDF as a string.");
189
190 FDFDoc fdoc_new = in_doc.fdfExtract(PDFDoc.e_both);
191 String XFDF_str = fdoc_new.saveAsXFDF();
192 mOutputListener.println("Extracted XFDF: ");
193 mOutputListener.println(XFDF_str);
194 mOutputListener.println("Extract complete.");
195 } catch (Exception e) {
196 mOutputListener.printError(e.getStackTrace());
197 }
198
199 // Example 5) Read FDF files directly
200 try {
201 FDFDoc doc = new FDFDoc((Utils.createExternalFile("form1_filled_data.fdf", mFileList).getAbsolutePath()));
202
203 for (FDFFieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
204 FDFField current = itr.next();
205 mOutputListener.println("Field name: " + current.getName());
206 mOutputListener.println("Field partial name: " + current.getPartialName());
207
208 mOutputListener.println("------------------------------");
209 }
210 doc.close();
211 mOutputListener.println("Done.");
212 } catch (Exception e) {
213 mOutputListener.printError(e.getStackTrace());
214 }
215
216 // Example 6) Direct generation of FDF.
217 try {
218 FDFDoc doc = new FDFDoc();
219 // Create new fields (i.e. key/value pairs).
220 doc.fieldCreate("Company", Field.e_text, "PDFTron Systems");
221 doc.fieldCreate("First Name", Field.e_text, "John");
222 doc.fieldCreate("Last Name", Field.e_text, "Doe");
223 // ...
224
225 // doc.setPdfFileName("mydoc.pdf");
226
227 doc.save(Utils.createExternalFile("sample_output.fdf", mFileList).getAbsolutePath());
228 doc.close();
229 mOutputListener.println("Done. Results saved in sample_output.fdf");
230 } catch (Exception e) {
231 mOutputListener.printError(e.getStackTrace());
232 }
233
234 for (String file : mFileList) {
235 addToFileList(file);
236 }
237 printFooter(outputListener);
238 }
239
240}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales