PDF Form Fill, Form Data Extraction with Forms Data Format (FDF) - Python 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-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#---------------------------------------------------------------------------------------
15# PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract
16# forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
17# available in PDFNet.
18#---------------------------------------------------------------------------------------
19
20def main():
21 PDFNet.Initialize(LicenseKey)
22
23 # Relative path to the folder containing the test files.
24 input_path = "../../TestFiles/"
25 output_path = "../../TestFiles/Output/"
26
27 # Example 1
28 # Iterate over all form fields in the document. Display all field names.
29
30 doc = PDFDoc(input_path + "form1.pdf")
31 doc.InitSecurityHandler()
32
33 itr = doc.GetFieldIterator()
34 while itr.HasNext():
35 print("Field name: " + itr.Current().GetName())
36 print("Field partial name: " + itr.Current().GetPartialName())
37
38 sys.stdout.write("Field type: ")
39 type = itr.Current().GetType()
40 if type == Field.e_button:
41 print("Button")
42 elif type == Field.e_check:
43 print("Check")
44 elif type == Field.e_radio:
45 print("Radio")
46 elif type == Field.e_text:
47 print("Text")
48 elif type == Field.e_choice:
49 print("Choice")
50 elif type == Field.e_signature:
51 print("Signiture")
52 elif type == Field.e_null:
53 print("Null")
54
55 print("------------------------------")
56 itr.Next()
57
58 doc.Close()
59 print("Done.")
60
61 # Example 2
62 # Import XFDF into FDF, then merge data from FDF
63
64 # XFDF to FDF
65 # form fields
66 print("Import form field data from XFDF to FDF.")
67
68 fdf_doc1 = FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf")
69 fdf_doc1.Save(output_path + "form1_data.fdf")
70
71 # annotations
72 print("Import annotations from XFDF to FDF.")
73
74 fdf_doc2 = FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf")
75 fdf_doc2.Save(output_path + "form1_annots.fdf")
76
77 # FDF to PDF
78 # form fields
79 print("Merge form field data from FDF.")
80
81 doc = PDFDoc(input_path + "form1.pdf")
82 doc.InitSecurityHandler()
83 doc.FDFMerge(fdf_doc1)
84
85 # Refreshing missing appearances is not required here, but is recommended to make them
86 # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
87 doc.RefreshAnnotAppearances()
88
89 doc.Save(output_path + "form1_filled.pdf", SDFDoc.e_linearized)
90
91 # annotations
92 print("Merge annotations from FDF.")
93
94 doc.FDFMerge(fdf_doc2)
95 # Refreshing missing appearances is not required here, but is recommended to make them
96 # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
97 doc.RefreshAnnotAppearances()
98 doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc.e_linearized)
99 doc.Close()
100 print("Done.")
101
102
103 # Example 3
104 # Extract data from PDF to FDF, then export FDF as XFDF
105
106 # PDF to FDF
107 in_doc = PDFDoc(output_path + "form1_filled_with_annots.pdf")
108 in_doc.InitSecurityHandler()
109
110 # form fields only
111 print("Extract form fields data to FDF.")
112
113 doc_fields = in_doc.FDFExtract(PDFDoc.e_forms_only)
114 doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf")
115 doc_fields.Save(output_path + "form1_filled_data.fdf")
116
117 # annotations only
118 print("Extract annotations to FDF.")
119
120 doc_annots = in_doc.FDFExtract(PDFDoc.e_annots_only)
121 doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf")
122 doc_annots.Save(output_path + "form1_filled_annot.fdf")
123
124 # both form fields and annotations
125 print("Extract both form fields and annotations to FDF.")
126
127 doc_both = in_doc.FDFExtract(PDFDoc.e_both)
128 doc_both.SetPDFFileName("../form1_filled_with_annots.pdf")
129 doc_both.Save(output_path + "form1_filled_both.fdf")
130
131 # FDF to XFDF
132 # form fields
133 print("Export form field data from FDF to XFDF.")
134
135 doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")
136
137 # annotations
138 print("Export annotations from FDF to XFDF.")
139
140 doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")
141
142 # both form fields and annotations
143 print("Export both form fields and annotations from FDF to XFDF.")
144
145 doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
146
147 in_doc.Close()
148 print("Done.")
149
150 # Example 4
151 # Merge/Extract XFDF into/from PDF
152
153 # Merge XFDF from string
154 in_doc = PDFDoc(input_path + "numbered.pdf")
155 in_doc.InitSecurityHandler()
156
157 print("Merge XFDF string into PDF.")
158
159 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>"
160
161 fdoc = FDFDoc.CreateFromXFDF(str)
162 in_doc.FDFMerge(fdoc)
163 in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc.e_linearized)
164 print("Merge complete.")
165
166 # Extract XFDF as string
167 print("Extract XFDF as a string.")
168
169 fdoc_new = in_doc.FDFExtract(PDFDoc.e_both)
170 XFDF_str = fdoc_new.SaveAsXFDF()
171 print("Extracted XFDF: ")
172 print(XFDF_str)
173 in_doc.Close()
174 print("Extract complete.")
175
176 # Example 5
177 # Read FDF files directly
178
179 doc = FDFDoc(output_path + "form1_filled_data.fdf")
180
181 itr = doc.GetFieldIterator()
182 while itr.HasNext():
183 print("Field name: " + itr.Current().GetName())
184 print("Field partial name: " + itr.Current().GetPartialName())
185 print("------------------------------")
186 itr.Next()
187
188 doc.Close()
189 print("Done.")
190
191 # Example 6
192 # Direct generation of FDF
193 doc = FDFDoc()
194
195 # Create new fields (i.r. key/value pairs
196 doc.FieldCreate("Company", Field.e_text, "PDFTron Systems")
197 doc.FieldCreate("First Name", Field.e_text, "John")
198 doc.FieldCreate("Last Name", Field.e_text, "Doe")
199
200 doc.Save(output_path + "sample_output.fdf")
201 doc.Close()
202 PDFNet.Terminate()
203 print("Done. Results saved in sample_output.fdf")
204
205if __name__ == '__main__':
206 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales