PDF Form Fill, Form Data Extraction with Forms Data Format (FDF) - C# (.Net) 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//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11using pdftron.FDF;
12
13namespace FDFTestCS
14{
15 /// <summary>
16 /// PDFNet includes full support for FDF (Forms Data Format) and for merging/extracting
17 /// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
18 /// available in PDFNet.
19 /// </summary>
20 class Class1
21 {
22 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
23 static Class1() {}
24
25 /// <summary>
26 /// The main entry point for the application.
27 /// </summary>
28 static void Main(string[] args)
29 {
30 PDFNet.Initialize(PDFTronLicense.Key);
31
32 // Relative path to the folder containing test files.
33 string input_path = "../../../../TestFiles/";
34 string output_path = "../../../../TestFiles/Output/";
35
36 // Example 1)
37 // Iterate over all form fields in the document. Display all field names.
38 try
39 {
40 using (PDFDoc doc = new PDFDoc(input_path + "form1.pdf"))
41 {
42 doc.InitSecurityHandler();
43
44 FieldIterator itr;
45 for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
46 {
47 Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
48 Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
49
50 Console.Write("Field type: ");
51 Field.Type type = itr.Current().GetType();
52 switch(type)
53 {
54 case Field.Type.e_button:
55 Console.WriteLine("Button"); break;
56 case Field.Type.e_check:
57 Console.WriteLine("Check"); break;
58 case Field.Type.e_radio:
59 Console.WriteLine("Radio"); break;
60 case Field.Type.e_text:
61 Console.WriteLine("Text"); break;
62 case Field.Type.e_choice:
63 Console.WriteLine("Choice"); break;
64 case Field.Type.e_signature:
65 Console.WriteLine("Signature"); break;
66 case Field.Type.e_null:
67 Console.WriteLine("Null"); break;
68 }
69
70 Console.WriteLine("------------------------------");
71 }
72
73 Console.WriteLine("Done.");
74 }
75 }
76 catch (PDFNetException e)
77 {
78 Console.WriteLine(e.Message);
79 }
80
81 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
82 try
83 {
84 // XFDF to FDF
85 // form fields
86 Console.WriteLine("Import form field data from XFDF to FDF.");
87
88 FDFDoc fdf_doc1 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf"));
89 fdf_doc1.Save(output_path + "form1_data.fdf");
90
91 // annotations
92 Console.WriteLine("Import annotations from XFDF to FDF.");
93
94 FDFDoc fdf_doc2 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf"));
95 fdf_doc2.Save(output_path + "form1_annots.fdf");
96
97 // FDF to PDF
98 // form fields
99 Console.WriteLine("Merge form field data from FDF.");
100
101 using (PDFDoc doc = new PDFDoc(input_path + "form1.pdf"))
102 {
103 doc.InitSecurityHandler();
104 doc.FDFMerge(fdf_doc1);
105
106 // Refreshing missing appearances is not required here, but is recommended to make them
107 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
108 doc.RefreshAnnotAppearances();
109
110 doc.Save(output_path + "form1_filled.pdf", SDFDoc.SaveOptions.e_linearized);
111
112 // annotations
113 Console.WriteLine("Merge annotations from FDF.");
114
115 doc.FDFMerge(fdf_doc2);
116 // Refreshing missing appearances is not required here, but is recommended to make them
117 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
118 doc.RefreshAnnotAppearances();
119 doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc.SaveOptions.e_linearized);
120
121 Console.WriteLine("Done.");
122 }
123 }
124 catch (PDFNetException e)
125 {
126 Console.WriteLine(e.Message);
127 }
128
129 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
130 try
131 {
132 // PDF to FDF
133 using (PDFDoc in_doc = new PDFDoc(output_path + "form1_filled_with_annots.pdf"))
134 {
135 in_doc.InitSecurityHandler();
136
137 // form fields only
138 Console.WriteLine("Extract form fields data to FDF.");
139
140 FDFDoc doc_fields = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_forms_only);
141 doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf");
142 doc_fields.Save(output_path + "form1_filled_data.fdf");
143
144 // annotations only
145 Console.WriteLine("Extract annotations to FDF.");
146
147 FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only);
148 doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf");
149 doc_annots.Save(output_path + "form1_filled_annot.fdf");
150
151 // both form fields and annotations
152 Console.WriteLine("Extract both form fields and annotations to FDF.");
153
154 FDFDoc doc_both = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
155 doc_both.SetPdfFileName("../form1_filled_with_annots.pdf");
156 doc_both.Save(output_path + "form1_filled_both.fdf");
157
158 // FDF to XFDF
159 // form fields
160 Console.WriteLine("Export form field data from FDF to XFDF.");
161
162 doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf");
163
164 // annotations
165 Console.WriteLine("Export annotations from FDF to XFDF.");
166
167 doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf");
168
169 // both form fields and annotations
170 Console.WriteLine("Export both form fields and annotations from FDF to XFDF.");
171
172 doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf");
173
174 Console.WriteLine("Done.");
175 }
176 }
177 catch (PDFNetException e)
178 {
179 Console.WriteLine(e.Message);
180 }
181
182 // Example 4) Merge/Extract XFDF into/from PDF
183 try
184 {
185 // Merge XFDF from string
186 PDFDoc in_doc = new PDFDoc(input_path + "numbered.pdf");
187 {
188 in_doc.InitSecurityHandler();
189
190 Console.WriteLine("Merge XFDF string into PDF.");
191
192 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>";
193
194 using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
195 {
196 in_doc.FDFMerge(fdoc);
197 in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc.SaveOptions.e_linearized);
198 Console.WriteLine("Merge complete.");
199 }
200
201 // Extract XFDF as string
202 Console.WriteLine("Extract XFDF as a string.");
203 FDFDoc fdoc_new = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
204 string XFDF_str = fdoc_new.SaveAsXFDF();
205 Console.WriteLine("Extracted XFDF: ");
206 Console.WriteLine(XFDF_str);
207 Console.WriteLine("Extract complete.");
208 }
209 }
210 catch (PDFNetException e)
211 {
212 Console.WriteLine(e.Message);
213 }
214
215 // Example 5) Read FDF files directly
216 try
217 {
218 FDFDoc doc = new FDFDoc(output_path + "form1_filled_data.fdf");
219 FDFFieldIterator itr = doc.GetFieldIterator();
220 for(; itr.HasNext(); itr.Next())
221 {
222 Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
223 Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
224 Console.WriteLine("------------------------------");
225 }
226
227 Console.WriteLine("Done.");
228 }
229 catch (PDFNetException e)
230 {
231 Console.WriteLine(e.Message);
232 }
233
234 // Example 6) Direct generation of FDF.
235 try
236 {
237 FDFDoc doc = new FDFDoc();
238
239 // Create new fields (i.e. key/value pairs).
240 doc.FieldCreate("Company", (int)Field.Type.e_text, "PDFTron Systems");
241 doc.FieldCreate("First Name", (int)Field.Type.e_text, "John");
242 doc.FieldCreate("Last Name", (int)Field.Type.e_text, "Doe");
243 // ...
244
245 // doc.SetPdfFileName("mydoc.pdf");
246 doc.Save(output_path + "sample_output.fdf");
247 Console.WriteLine("Done. Results saved in sample_output.fdf");
248 }
249 catch (PDFNetException e)
250 {
251 Console.WriteLine(e.Message);
252 }
253 PDFNet.Terminate();
254 }
255 }
256}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales