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 Xamarin SDK and PDF Data Extraction SDK Capabilities.
1//
2// Copyright (c) 2001-2021 by PDFTron Systems 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
13using NUnit.Framework;
14
15namespace MiscellaneousSamples
16{
17 /// <summary>
18 /// PDFNet includes full support for FDF (Forms Data Format) and for merging/extracting
19 /// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
20 /// available in PDFNet.
21 /// </summary>
22 [TestFixture]
23 public class FDFTest
24 {
25
26 /// <summary>
27 /// The main entry point for the application.
28 /// </summary>
29 [Test]
30 public static void Sample()
31 {
32
33 // Relative path to the folder containing test files.
34 const string input_path = "TestFiles/";
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(Utils.GetAssetTempFile(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 Assert.True(false);
80 }
81
82 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
83 try
84 {
85 // XFDF to FDF
86 // form fields
87 Console.WriteLine("Import form field data from XFDF to FDF.");
88
89 FDFDoc fdf_doc1 = FDFDoc.CreateFromXFDF(Utils.GetAssetTempFile(input_path + "form1_data.xfdf"));
90 fdf_doc1.Save(Utils.CreateExternalFile("form1_data.fdf"));
91
92 // annotations
93 Console.WriteLine("Import annotations from XFDF to FDF.");
94
95 FDFDoc fdf_doc2 = FDFDoc.CreateFromXFDF(Utils.GetAssetTempFile(input_path + "form1_annots.xfdf"));
96 fdf_doc2.Save(Utils.CreateExternalFile("form1_annots.fdf"));
97
98 // FDF to PDF
99 // form fields
100 Console.WriteLine("Merge form field data from FDF.");
101
102 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "form1.pdf")))
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"), SDFDoc.SaveOptions.e_linearized);
112
113 // annotations
114 Console.WriteLine("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"), SDFDoc.SaveOptions.e_linearized);
121
122 Console.WriteLine("Done.");
123 }
124 }
125 catch (PDFNetException e)
126 {
127 Console.WriteLine(e.Message);
128 Assert.True(false);
129 }
130
131 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
132 try
133 {
134 // PDF to FDF
135 using (PDFDoc in_doc = new PDFDoc(Utils.CreateExternalFile("form1_filled_with_annots.pdf")))
136 {
137 in_doc.InitSecurityHandler();
138
139 // form fields only
140 Console.WriteLine("Extract form fields data to FDF.");
141
142 FDFDoc doc_fields = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_forms_only);
143 doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf");
144 doc_fields.Save(Utils.CreateExternalFile("form1_filled_data.fdf"));
145
146 // annotations only
147 Console.WriteLine("Extract annotations to FDF.");
148
149 FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only);
150 doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf");
151 doc_annots.Save(Utils.CreateExternalFile("form1_filled_annot.fdf"));
152
153 // both form fields and annotations
154 Console.WriteLine("Extract both form fields and annotations to FDF.");
155
156 FDFDoc doc_both = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
157 doc_both.SetPdfFileName("../form1_filled_with_annots.pdf");
158 doc_both.Save(Utils.CreateExternalFile("form1_filled_both.fdf"));
159
160 // FDF to XFDF
161 // form fields
162 Console.WriteLine("Export form field data from FDF to XFDF.");
163
164 doc_fields.SaveAsXFDF(Utils.CreateExternalFile("form1_filled_data.xfdf"));
165
166 // annotations
167 Console.WriteLine("Export annotations from FDF to XFDF.");
168
169 doc_annots.SaveAsXFDF(Utils.CreateExternalFile("form1_filled_annot.xfdf"));
170
171 // both form fields and annotations
172 Console.WriteLine("Export both form fields and annotations from FDF to XFDF.");
173
174 doc_both.SaveAsXFDF(Utils.CreateExternalFile("form1_filled_both.xfdf"));
175
176 Console.WriteLine("Done.");
177 }
178 }
179 catch (PDFNetException e)
180 {
181 Console.WriteLine(e.Message);
182 Assert.True(false);
183 }
184
185 // Example 4) Merge/Extract XFDF into/from PDF
186 try
187 {
188 // Merge XFDF from string
189 PDFDoc in_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "numbered.pdf"));
190 {
191 in_doc.InitSecurityHandler();
192
193 Console.WriteLine("Merge XFDF string into PDF.");
194
195 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>";
196
197 using (FDFDoc fdoc = FDFDoc.CreateFromXFDF(str))
198 {
199 in_doc.FDFMerge(fdoc);
200 in_doc.Save(Utils.CreateExternalFile("numbered_modified.pdf"), SDFDoc.SaveOptions.e_linearized);
201 Console.WriteLine("Merge complete.");
202 }
203
204 // Extract XFDF as string
205 Console.WriteLine("Extract XFDF as a string.");
206 FDFDoc fdoc_new = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
207 string XFDF_str = fdoc_new.SaveAsXFDF();
208 Console.WriteLine("Extracted XFDF: ");
209 Console.WriteLine(XFDF_str);
210 Console.WriteLine("Extract complete.");
211 }
212 }
213 catch (PDFNetException e)
214 {
215 Console.WriteLine(e.Message);
216 Assert.True(false);
217 }
218
219 // Example 5) Read FDF files directly
220 try
221 {
222 FDFDoc doc = new FDFDoc(Utils.CreateExternalFile("form1_filled_data.fdf"));
223 FDFFieldIterator itr = doc.GetFieldIterator();
224 for(; itr.HasNext(); itr.Next())
225 {
226 Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
227 Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
228 Console.WriteLine("------------------------------");
229 }
230
231 Console.WriteLine("Done.");
232 }
233 catch (PDFNetException e)
234 {
235 Console.WriteLine(e.Message);
236 Assert.True(false);
237 }
238
239 // Example 6) Direct generation of FDF.
240 try
241 {
242 FDFDoc doc = new FDFDoc();
243
244 // Create new fields (i.e. key/value pairs).
245 doc.FieldCreate("Company", (int)Field.Type.e_text, "PDFTron Systems");
246 doc.FieldCreate("First Name", (int)Field.Type.e_text, "John");
247 doc.FieldCreate("Last Name", (int)Field.Type.e_text, "Doe");
248 // ...
249
250 // doc.SetPdfFileName("mydoc.pdf");
251 doc.Save(Utils.CreateExternalFile("sample_output.fdf"));
252 Console.WriteLine("Done. Results saved in sample_output.fdf");
253 }
254 catch (PDFNetException e)
255 {
256 Console.WriteLine(e.Message);
257 Assert.True(false);
258 }
259 }
260 }
261}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales