FDF

Sample Obj-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 iOS SDK and PDF Data Extraction SDK Capabilities.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//---------------------------------------------------------------------------------------
10// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract
11// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
12// available in PDFNet.
13//---------------------------------------------------------------------------------------
14int main(int argc, char *argv[])
15{
16 @autoreleasepool {
17 int ret = 0;
18 [PTPDFNet Initialize: 0];
19
20 // Example 1)
21 // Iterate over all form fields in the document. Display all field names.
22 @try
23 {
24 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/form1.pdf"];
25 [doc InitSecurityHandler];
26
27 PTFieldIterator * itr;
28 for(itr = [doc GetFieldIterator]; [itr HasNext]; [itr Next])
29 {
30 NSLog(@"Field name: %@", [[itr Current] GetName]);
31 NSLog(@"Field partial name: %@", [[itr Current] GetPartialName]);
32
33 //NSLog(@"Field type: ");
34 int type = [[itr Current] GetType];
35 switch(type)
36 {
37 case e_ptbutton: NSLog(@"Field type: Button"); NSLog(@"------------------------------");break;
38 case e_ptcheck: NSLog(@"Field type: Check"); NSLog(@"------------------------------");break;
39 case e_ptradio: NSLog(@"Field type: Radio"); NSLog(@"------------------------------");break;
40 case e_pttext: NSLog(@"Field type: Text"); NSLog(@"------------------------------");break;
41 case e_ptchoice: NSLog(@"Field type: Choice"); NSLog(@"------------------------------");break;
42 case e_ptsignature: NSLog(@"Field type: Signature"); NSLog(@"------------------------------");break;
43 case e_ptf_null: NSLog(@"Field type: Null"); NSLog(@"------------------------------");break;
44 default: NSLog(@"Field type: ------------------------------");
45
46 }
47
48 //NSLog(@"------------------------------");
49 }
50
51 NSLog(@"Done.");
52 }
53 @catch(NSException *e)
54 {
55 NSLog(@"%@", e.reason);
56 ret = 1;
57 }
58
59 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
60 @try
61 {
62 // XFDF to FDF
63 // form fields
64 NSLog(@"Import form field data from XFDF to FDF.");
65
66 PTFDFDoc *fdf_doc1 = [PTFDFDoc CreateFromXFDF: @"../../TestFiles/form1_data.xfdf"];
67 [fdf_doc1 SaveFDFDocToFile: @"../../TestFiles/Output/form1_data.fdf"];
68
69 // annotations
70 NSLog(@"Import annotations from XFDF to FDF.");
71
72 PTFDFDoc *fdf_doc2 = [PTFDFDoc CreateFromXFDF: @"../../TestFiles/form1_annots.xfdf"];
73 [fdf_doc2 SaveFDFDocToFile: @"../../TestFiles/Output/form1_annots.fdf"];
74
75 // FDF to PDF
76 // form fields
77 NSLog(@"Merge form field data from FDF.");
78
79 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/form1.pdf"];
80 [doc InitSecurityHandler];
81 [doc FDFMerge: fdf_doc1];
82
83 // Refreshing missing appearances is not required here, but is recommended to make them
84 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
85 [doc RefreshAnnotAppearances: NULL];
86
87 [doc SaveToFile: @"../../TestFiles/Output/form1_filled.pdf" flags: e_ptlinearized];
88
89 // annotations
90 NSLog(@"Merge annotations from FDF.");
91
92 [doc FDFMerge: fdf_doc2];
93 // Refreshing missing appearances is not required here, but is recommended to make them
94 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
95 [doc RefreshAnnotAppearances: NULL];
96 [doc SaveToFile: @"../../TestFiles/Output/form1_filled_with_annots.pdf" flags: e_ptlinearized];
97 NSLog(@"Done.");
98 }
99 @catch(NSException *e)
100 {
101 NSLog(@"%@", e.reason);
102 ret = 1;
103 }
104
105 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
106 @try
107 {
108 // PDF to FDF
109 PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/form1_filled_with_annots.pdf"];
110 [in_doc InitSecurityHandler];
111
112 // form fields only
113 NSLog(@"Extract form fields data to FDF.");
114
115 PTFDFDoc *doc_fields = [in_doc FDFExtract: e_ptforms_only];
116 [doc_fields SetPDFFileName: @"../form1_filled_with_annots.pdf"];
117 [doc_fields SaveFDFDocToFile: @"../../TestFiles/Output/form1_filled_data.fdf"];
118
119 // annotations only
120 NSLog(@"Extract annotations to FDF.");
121
122 PTFDFDoc *doc_annots = [in_doc FDFExtract: e_ptannots_only];
123 [doc_annots SetPDFFileName: @"../form1_filled_with_annots.pdf"];
124 [doc_annots SaveFDFDocToFile: @"../../TestFiles/Output/form1_filled_annot.fdf"];
125
126 // both form fields and annotations
127 NSLog(@"Extract both form fields and annotations to FDF.");
128
129 PTFDFDoc *doc_both = [in_doc FDFExtract: e_ptboth];
130 [doc_both SetPDFFileName: @"../form1_filled_with_annots.pdf"];
131 [doc_both SaveFDFDocToFile: @"../../TestFiles/Output/form1_filled_both.fdf"];
132
133 // FDF to XFDF
134 // form fields
135 NSLog(@"Export form field data from FDF to XFDF.");
136
137 [doc_fields SaveAsXFDF: @"../../TestFiles/Output/form1_filled_data.xfdf"];
138
139 // annotations
140 NSLog(@"Export annotations from FDF to XFDF.");
141
142 [doc_annots SaveAsXFDF: @"../../TestFiles/Output/form1_filled_annot.xfdf"];
143
144 // both form fields and annotations
145 NSLog(@"Export both form fields and annotations from FDF to XFDF.");
146
147 [doc_both SaveAsXFDF: @"../../TestFiles/Output/form1_filled_both.xfdf"];
148
149 NSLog(@"Done.");
150 }
151 @catch(NSException *e)
152 {
153 NSLog(@"%@", e.reason);
154 ret = 1;
155 }
156
157 // Example 4) Merge/Extract XFDF into/from PDF
158 @try
159 {
160 // Merge XFDF from string
161 PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/numbered.pdf"];
162 [in_doc InitSecurityHandler];
163
164 NSLog(@"Merge XFDF string into PDF.");
165 NSString *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>";
166
167 PTFDFDoc *fdoc = [PTFDFDoc CreateFromXFDF: str];
168 [in_doc FDFMerge: fdoc];
169 [in_doc SaveToFile: @"../../TestFiles/Output/numbered_modified.pdf" flags: e_ptlinearized];
170
171 NSLog(@"Merge complete.");
172
173 // Extract XFDF as string
174 NSLog(@"Extract XFDF as a string.");
175 PTFDFDoc *fdoc_new = [in_doc FDFExtract: e_ptboth];
176 NSString *XFDF_str = [fdoc_new SaveAsXFDFToString];
177 NSLog(@"Extracted XFDF: ");
178 NSLog(@"%@", XFDF_str);
179 NSLog(@"Extract complete.");
180 }
181 @catch(NSException *e)
182 {
183 NSLog(@"%@", e.reason);
184 ret = 1;
185 }
186
187 // Example 5) Read FDF files directly
188 @try
189 {
190 PTFDFDoc *doc = [[PTFDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/form1_filled_data.fdf"];
191
192 PTFDFFieldIterator *itr;
193 for(itr = [doc GetFieldIterator]; [itr HasNext]; [itr Next])
194 {
195 NSLog(@"Field name: %@", [[itr Current] GetName]);
196 NSLog(@"Field partial name: %@", [[itr Current] GetPartialName]);
197
198 NSLog(@"------------------------------");
199 }
200
201 NSLog(@"Done.");
202 }
203 @catch(NSException *e)
204 {
205 NSLog(@"%@", e.reason);
206 ret = 1;
207 }
208
209 // Example 6) Direct generation of FDF.
210 @try
211 {
212 PTFDFDoc *doc = [[PTFDFDoc alloc] init];
213 // Create new fields (i.e. key/value pairs).
214 [doc FieldCreateWithString: @"Company" type: e_pttext field_value: @"PDFTron Systems"];
215 [doc FieldCreateWithString: @"First Name" type: e_pttext field_value: @"John"];
216 [doc FieldCreateWithString: @"Last Name" type: e_pttext field_value: @"Doe"];
217 // ...
218
219 // [doc SetPdfFileName: @"mydoc.pdf"];
220
221 [doc SaveFDFDocToFile: @"../../TestFiles/Output/sample_output.fdf"];
222 NSLog(@"Done. Results saved in sample_output.fdf");
223 }
224 @catch(NSException *e)
225 {
226 NSLog(@"%@", e.reason);
227 ret = 1;
228 }
229 [PTPDFNet Terminate: 0];
230 return ret;
231 }
232}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales