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