Sample C# 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 UWP SDK and PDF Data Extraction SDK Capabilities.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.FDF;
11using pdftron.PDF;
12using pdftron.SDF;
13
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 public sealed class FDFTest : Sample
19 {
20 public FDFTest() :
21 base("FDF", "PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract forms data (FDF) with/from PDF. The sample illustrates basic FDF merge/extract functionality available in PDFNet.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting FDF Test...");
30 WriteLine("--------------------------------\n");
31 // Example 1)
32 // Iterate over all form fields in the document. Display all field names.
33 try
34 {
35 String input_file_path = Path.Combine(InputPath, "form1.pdf");
36 PDFDoc doc = new PDFDoc(input_file_path);
37 doc.InitSecurityHandler();
38
39 FieldIterator itr;
40 for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
41 {
42 WriteLine(String.Format("Field name: {0:s}", itr.Current().GetName()));
43 WriteLine(String.Format("Field partial name: {0:s}", itr.Current().GetPartialName()));
44
45 Write("Field type: ");
46 FieldType type = itr.Current().GetType();
47 switch(type)
48 {
49 case FieldType.e_button:
50 WriteLine("Button"); break;
51 case FieldType.e_text:
52 WriteLine("Text"); break;
53 case FieldType.e_choice:
54 WriteLine("Choice"); break;
55 case FieldType.e_signature:
56 WriteLine("Signature"); break;
57 }
58
59 WriteLine("\n------------------------------");
60 }
61
62 doc.Destroy();
63 WriteLine("Done.");
64 }
65 catch (Exception e)
66 {
67 WriteLine(GetExceptionMessage(e));
68 }
69
70 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
71 try
72 {
73 // XFDF to FDF
74 // form fields
75 WriteLine("Import form field data from XFDF to FDF.");
76
77 String input_file_path = Path.Combine(InputPath, "form1_data.xfdf");
78 String output_file_path = Path.Combine(OutputPath, "form1_data.fdf");
79 FDFDoc fdf_doc1 = await FDFDoc.CreateFromXFDFAsync(input_file_path);
80 await fdf_doc1.SaveAsync(output_file_path);
81 WriteLine("Done. Result saved in " + output_file_path);
82 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
83
84 // annotations
85 WriteLine("Import annotations from XFDF to FDF.");
86
87 input_file_path = Path.Combine(InputPath, "form1_annots.xfdf");
88 output_file_path = Path.Combine(OutputPath, "form1_annots.fdf");
89 FDFDoc fdf_doc2 = await FDFDoc.CreateFromXFDFAsync(input_file_path);
90 await fdf_doc2.SaveAsync(output_file_path);
91 WriteLine("Done. Result saved in " + output_file_path);
92 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
93
94 // FDF to PDF
95 // form fields
96 WriteLine("Merge form field data from FDF.");
97 input_file_path = Path.Combine(InputPath, "form1.pdf");
98 PDFDoc doc = new PDFDoc(input_file_path);
99 doc.InitSecurityHandler();
100 doc.FDFMerge(fdf_doc1);
101
102 // Refreshing missing appearances is not required here, but is recommended to make them
103 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
104 doc.RefreshAnnotAppearances();
105
106 output_file_path = Path.Combine(OutputPath, "form1_filled.pdf");
107 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
108 WriteLine("Done. Result saved in " + output_file_path);
109 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
110
111 //annotations
112 WriteLine("Merge annotations from FDF.");
113 doc.FDFMerge(fdf_doc2);
114
115 // Refreshing missing appearances is not required here, but is recommended to make them
116 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
117 doc.RefreshAnnotAppearances();
118
119 output_file_path = Path.Combine(OutputPath, "form1_filled_with_annots.pdf");
120 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
121 doc.Destroy();
122 WriteLine("Done. Result saved in " + output_file_path);
123 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
124
125 WriteLine("Done.");
126 }
127 catch (Exception e)
128 {
129 WriteLine(GetExceptionMessage(e));
130 }
131
132 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
133 try
134 {
135 // PDF to FDF
136 String output_file_path = Path.Combine(OutputPath, "form1_filled_with_annots.pdf");
137 PDFDoc in_doc = new PDFDoc(output_file_path);
138 in_doc.InitSecurityHandler();
139
140 // form fields only
141 WriteLine("Extract form fields data to FDF.");
142
143 FDFDoc doc_fields = in_doc.FDFExtract(PDFDocExtractFlag.e_forms_only);
144 doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf");
145 output_file_path = Path.Combine(OutputPath, "form1_filled_data.fdf");
146 await doc_fields.SaveAsync(output_file_path);
147 WriteLine("Done. Result saved in " + output_file_path);
148 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
149
150 // annotations only
151 WriteLine("Extract annotations to FDF.");
152
153 FDFDoc doc_annots = in_doc.FDFExtract(PDFDocExtractFlag.e_annots_only);
154 doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf");
155 output_file_path = Path.Combine(OutputPath, "form1_filled_annot.fdf");
156 await doc_annots.SaveAsync(output_file_path);
157 WriteLine("Done. Result saved in " + output_file_path);
158 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
159
160 // both form fields and annotations
161 WriteLine("Extract both form fields and annotations to FDF.");
162
163 FDFDoc doc_both = in_doc.FDFExtract(PDFDocExtractFlag.e_both);
164 doc_both.SetPdfFileName("../form1_filled_with_annots.pdf");
165 output_file_path = Path.Combine(OutputPath, "form1_filled_both.fdf");
166 await doc_both.SaveAsync(output_file_path);
167 WriteLine("Done. Result saved in " + output_file_path);
168 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
169
170 // FDF to XFDF
171 // form fields
172 WriteLine("Export form field data from FDF to XFDF.");
173
174 output_file_path = Path.Combine(OutputPath, "form1_filled_data.xfdf");
175 await doc_fields.SaveAsXFDFAsync(output_file_path);
176 WriteLine("Done. Result saved in " + output_file_path);
177 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
178
179 // annotations
180 WriteLine("Export annotations from FDF to XFDF.");
181
182 output_file_path = Path.Combine(OutputPath, "form1_filled_annot.xfdf");
183 await doc_annots.SaveAsXFDFAsync(output_file_path);
184 WriteLine("Done. Result saved in " + output_file_path);
185 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
186
187 // both form fields and annotations
188 WriteLine("Export both form fields and annotations from FDF to XFDF.");
189
190 output_file_path = Path.Combine(OutputPath, "form1_filled_both.xfdf");
191 await doc_both.SaveAsXFDFAsync(output_file_path);
192 WriteLine("Done. Result saved in " + output_file_path);
193 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
194
195 in_doc.Destroy();
196 WriteLine("Done.");
197 }
198 catch (Exception e)
199 {
200 WriteLine(GetExceptionMessage(e));
201 }
202
203 // Example 4) Read FDF files directly
204 try
205 {
206 String output_file_path = Path.Combine(OutputPath, "form1_filled_data.fdf");
207 FDFDoc doc = new FDFDoc(output_file_path);
208 FDFFieldIterator itr = doc.GetFieldIterator();
209 for(; itr.HasNext(); itr.Next())
210 {
211 WriteLine(String.Format("Field name: {0:s}", itr.Current().GetName()));
212 WriteLine(String.Format("Field partial name: {0:s}", itr.Current().GetPartialName()));
213 WriteLine("------------------------------");
214 }
215
216 WriteLine("Done.");
217 }
218 catch (Exception e)
219 {
220 WriteLine(GetExceptionMessage(e));
221 }
222
223 // Example 5) Direct generation of FDF.
224 try
225 {
226 FDFDoc doc = new FDFDoc();
227
228 // Create new fields (i.e. key/value pairs).
229 doc.FieldCreate("Company", (int)FieldType.e_text, "PDFTron Systems");
230 doc.FieldCreate("First Name", (int)FieldType.e_text, "John");
231 doc.FieldCreate("Last Name", (int)FieldType.e_text, "Doe");
232 // ...
233
234 // doc.SetPdfFileName("mydoc.pdf");
235 String output_file_path = Path.Combine(OutputPath, "fdf_sample_output.fdf");
236 await doc.SaveAsync(output_file_path);
237 WriteLine("Done. Results saved in " + output_file_path);
238 await AddFileToOutputList(output_file_path);
239 }
240 catch (Exception e)
241 {
242 WriteLine(GetExceptionMessage(e));
243 }
244
245 WriteLine("\n--------------------------------");
246 WriteLine("Done FDF Test.");
247 WriteLine("--------------------------------\n");
248 })).AsAsyncAction();
249 }
250 }
251}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales