PDF Form Fill, Form Data Extraction with Forms Data Format (FDF) - Go 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-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
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
20func main(){
21 PDFNetInitialize(PDFTronLicense.Key)
22
23 // Relative path to the folder containing the test files.
24 inputPath := "../../TestFiles/"
25 outputPath := "../../TestFiles/Output/"
26
27 // Example 1
28 // Iterate over all form fields in the document. Display all field names.
29
30 doc := NewPDFDoc(inputPath + "form1.pdf")
31 doc.InitSecurityHandler()
32
33 itr := doc.GetFieldIterator()
34 for itr.HasNext(){
35 fmt.Println("Field name: " + itr.Current().GetName())
36 fmt.Println("Field partial name: " + itr.Current().GetPartialName())
37
38 fieldType := itr.Current().GetType()
39 fieldTypeStr := ""
40 if fieldType == FieldE_button{
41 fieldTypeStr = "Button"
42 }else if fieldType == FieldE_text{
43 fieldTypeStr = "Text"
44 }else if fieldType == FieldE_choice{
45 fieldTypeStr = "Choice"
46 }else if fieldType == FieldE_signature{
47 fieldTypeStr = "Signiture"
48 }
49 fmt.Println("Field type: " + fieldTypeStr)
50 fmt.Println("------------------------------")
51 itr.Next()
52 }
53 doc.Close()
54 fmt.Println("Done.")
55
56 // Example 2
57 // Import XFDF into FDF, then merge data from FDF
58
59 // XFDF to FDF
60 // form fields
61 fmt.Println("Import form field data from XFDF to FDF.")
62
63 fdfDoc1 := FDFDocCreateFromXFDF(inputPath + "form1_data.xfdf")
64 fdfDoc1.Save(outputPath + "form1_data.fdf")
65
66 // annotations
67 fmt.Println("Import annotations from XFDF to FDF.")
68
69 fdfDoc2 := FDFDocCreateFromXFDF(inputPath + "form1_annots.xfdf")
70 fdfDoc2.Save(outputPath + "form1_annots.fdf")
71
72 // FDF to PDF
73 // form fields
74 fmt.Println("Merge form field data from FDF.")
75
76 doc = NewPDFDoc(inputPath + "form1.pdf")
77 doc.InitSecurityHandler()
78 doc.FDFMerge(fdfDoc1)
79
80 // Refreshing missing appearances is not required here, but is recommended to make them
81 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
82 doc.RefreshAnnotAppearances()
83
84 doc.Save(outputPath + "form1_filled.pdf", uint(SDFDocE_linearized))
85
86 // annotations
87 fmt.Println("Merge annotations from FDF.")
88
89 doc.FDFMerge(fdfDoc2)
90 // Refreshing missing appearances is not required here, but is recommended to make them
91 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
92 doc.RefreshAnnotAppearances()
93 doc.Save(outputPath + "form1_filled_with_annots.pdf", uint(SDFDocE_linearized))
94 doc.Close()
95 fmt.Println("Done.")
96
97
98 // Example 3
99 // Extract data from PDF to FDF, then export FDF as XFDF
100
101 // PDF to FDF
102 inDoc := NewPDFDoc(outputPath + "form1_filled_with_annots.pdf")
103 inDoc.InitSecurityHandler()
104
105 // form fields only
106 fmt.Println("Extract form fields data to FDF.")
107
108 docFields := inDoc.FDFExtract(PDFDocE_forms_only)
109 docFields.SetPDFFileName("../form1_filled_with_annots.pdf")
110 docFields.Save(outputPath + "form1_filled_data.fdf")
111
112 // annotations only
113 fmt.Println("Extract annotations to FDF.")
114
115 docAnnots := inDoc.FDFExtract(PDFDocE_annots_only)
116 docAnnots.SetPDFFileName("../form1_filled_with_annots.pdf")
117 docAnnots.Save(outputPath + "form1_filled_annot.fdf")
118
119 // both form fields and annotations
120 fmt.Println("Extract both form fields and annotations to FDF.")
121
122 docBoth := inDoc.FDFExtract(PDFDocE_both)
123 docBoth.SetPDFFileName("../form1_filled_with_annots.pdf")
124 docBoth.Save(outputPath + "form1_filled_both.fdf")
125
126 // FDF to XFDF
127 // form fields
128 fmt.Println("Export form field data from FDF to XFDF.")
129
130 docFields.SaveAsXFDF(outputPath + "form1_filled_data.xfdf")
131
132 // annotations
133 fmt.Println("Export annotations from FDF to XFDF.")
134
135 docAnnots.SaveAsXFDF(outputPath + "form1_filled_annot.xfdf")
136
137 // both form fields and annotations
138 fmt.Println("Export both form fields and annotations from FDF to XFDF.")
139
140 docBoth.SaveAsXFDF(outputPath + "form1_filled_both.xfdf")
141
142 inDoc.Close()
143 fmt.Println("Done.")
144
145 // Example 4
146 // Merge/Extract XFDF into/from PDF
147
148 // Merge XFDF from string
149 inDoc = NewPDFDoc(inputPath + "numbered.pdf")
150 inDoc.InitSecurityHandler()
151
152 fmt.Println("Merge XFDF string into PDF.")
153
154 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>"
155
156 fdoc := FDFDocCreateFromXFDF(str)
157 inDoc.FDFMerge(fdoc)
158 inDoc.Save(outputPath + "numbered_modified.pdf", uint(SDFDocE_linearized))
159 fmt.Println("Merge complete.")
160
161 // Extract XFDF as string
162 fmt.Println("Extract XFDF as a string.")
163
164 fdocNew := inDoc.FDFExtract(PDFDocE_both)
165 xfdfStr := fdocNew.SaveAsXFDF()
166 fmt.Println("Extracted XFDF: ")
167 fmt.Println(xfdfStr)
168 inDoc.Close()
169 fmt.Println("Extract complete.")
170
171 // Example 5
172 // Read FDF files directly
173
174 fdoc2 := NewFDFDoc(outputPath + "form1_filled_data.fdf")
175
176 fitr := fdoc2.GetFieldIterator()
177 for fitr.HasNext(){
178 fmt.Println("Field name: " + fitr.Current().GetName())
179 fmt.Println("Field partial name: " + fitr.Current().GetPartialName())
180 fmt.Println("------------------------------")
181 fitr.Next()
182 }
183
184 fdoc2.Close()
185 fmt.Println("Done.")
186
187 // Example 6
188 // Direct generation of FDF
189 fdoc2 = NewFDFDoc()
190
191 // Create new fields (i.r. key/value pairs
192 fdoc2.FieldCreate("Company", FieldE_text, "PDFTron Systems")
193 fdoc2.FieldCreate("First Name", FieldE_text, "John")
194 fdoc2.FieldCreate("Last Name", FieldE_text, "Doe")
195
196 fdoc2.Save(outputPath + "sample_output.fdf")
197 fdoc2.Close()
198 PDFNetTerminate()
199 fmt.Println("Done. Results saved in sample_output.fdf")
200}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales