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// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/Field.h>
9#include <FDF/FDFDoc.h>
10#include <FDF/FDFField.h>
11#include <iostream>
12#include "../../LicenseKey/CPP/LicenseKey.h"
13
14using namespace std;
15using namespace pdftron;
16using namespace SDF;
17using namespace FDF;
18using namespace PDF;
19
20//---------------------------------------------------------------------------------------
21// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
22// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
23// available in PDFNet.
24//---------------------------------------------------------------------------------------
25int main(int argc, char *argv[])
26{
27	int ret = 0;
28	PDFNet::Initialize(LicenseKey);
29
30	// Relative path to the folder containing test files.
31	string input_path =  "../../TestFiles/";
32	string output_path = "../../TestFiles/Output/";
33
34
35	// Example 1)
36	// Iterate over all form fields in the document. Display all field names.
37	try  
38	{
39		PDFDoc doc((input_path + "form1.pdf").c_str());
40		doc.InitSecurityHandler();
41
42		for(FieldIterator itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next()) 
43		{
44			cout << "Field name: " << itr.Current().GetName() << endl;
45			cout << "Field partial name: " << itr.Current().GetPartialName() << endl;
46
47			cout << "Field type: ";
48			Field::Type type = itr.Current().GetType();
49			switch(type)
50			{
51			case Field::e_button: cout << "Button" << endl; break;
52			case Field::e_check: cout << "Check" << endl; break;
53			case Field::e_radio: cout << "Radio" << endl; break;
54			case Field::e_text: cout << "Text" << endl; break;
55			case Field::e_choice: cout << "Choice" << endl; break;
56			case Field::e_signature: cout << "Signature" << endl; break;
57			case Field::e_null: cout << "Null" << endl; break;
58			}
59
60			cout << "------------------------------" << endl;
61		}
62
63		cout << "Done." << endl;
64	}
65	catch(Common::Exception& e)
66	{
67		cout << e << endl;
68		ret = 1;
69	}
70	catch(...)
71	{
72		cout << "Unknown Exception" << endl;
73		ret = 1;
74	}
75
76
77	// Example 2) Import XFDF into FDF, then merge data from FDF into PDF
78	try
79	{
80		// XFDF to FDF
81		// form fields
82		cout << "Import form field data from XFDF to FDF." << endl;
83		
84		FDFDoc fdf_doc1(FDFDoc::CreateFromXFDF((input_path + "form1_data.xfdf").c_str()));
85		fdf_doc1.Save((output_path + "form1_data.fdf").c_str());
86
87		// annotations
88		cout << "Import annotations from XFDF to FDF." << endl;
89
90		FDFDoc fdf_doc2(FDFDoc::CreateFromXFDF((input_path + "form1_annots.xfdf").c_str()));
91		fdf_doc2.Save((output_path + "form1_annots.fdf").c_str());
92
93		// FDF to PDF
94		// form fields
95		cout << "Merge form field data from FDF." << endl;
96
97		PDFDoc doc((input_path + "form1.pdf").c_str());
98		doc.InitSecurityHandler();
99		doc.FDFMerge(fdf_doc1);
100		
101		// Refreshing missing appearances is not required here, but is recommended to make them 
102		// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
103		doc.RefreshAnnotAppearances();
104
105		doc.Save((output_path + "form1_filled.pdf").c_str(), SDFDoc::e_linearized, 0);
106
107		// annotations
108		cout << "Merge annotations from FDF." << endl;
109
110		doc.FDFMerge(fdf_doc2);
111		// Refreshing missing appearances is not required here, but is recommended to make them 
112		// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
113		doc.RefreshAnnotAppearances();
114		doc.Save((output_path + "form1_filled_with_annots.pdf").c_str(), SDFDoc::e_linearized, 0);
115		cout << "Done." << endl;
116	}
117	catch (Common::Exception& e)
118	{
119		cout << e << endl;
120		ret = 1;
121	}
122	catch (...)
123	{
124		cout << "Unknown Exception" << endl;
125		ret = 1;
126	}
127
128
129	// Example 3) Extract data from PDF to FDF, then export FDF as XFDF
130	try  
131	{
132		// PDF to FDF
133		PDFDoc in_doc((output_path + "form1_filled_with_annots.pdf").c_str());
134		in_doc.InitSecurityHandler();
135		
136		// form fields only
137		cout << "Extract form fields data to FDF." << endl;
138
139		FDFDoc doc_fields = in_doc.FDFExtract(PDFDoc::e_forms_only);
140		doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf");
141		doc_fields.Save((output_path + "form1_filled_data.fdf").c_str());
142
143		// annotations only
144		cout << "Extract annotations to FDF." << endl;
145
146		FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc::e_annots_only);
147		doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf");
148		doc_annots.Save((output_path + "form1_filled_annot.fdf").c_str());
149
150		// both form fields and annotations
151		cout << "Extract both form fields and annotations to FDF." << endl;
152
153		FDFDoc doc_both = in_doc.FDFExtract(PDFDoc::e_both);
154		doc_both.SetPDFFileName("../form1_filled_with_annots.pdf");
155		doc_both.Save((output_path + "form1_filled_both.fdf").c_str());
156
157		// FDF to XFDF
158		// form fields
159		cout << "Export form field data from FDF to XFDF." << endl;
160
161		doc_fields.SaveAsXFDF((output_path + "form1_filled_data.xfdf").c_str());
162
163		// annotations
164		cout << "Export annotations from FDF to XFDF." << endl;
165
166		doc_annots.SaveAsXFDF((output_path + "form1_filled_annot.xfdf").c_str());
167
168		// both form fields and annotations
169		cout << "Export both form fields and annotations from FDF to XFDF." << endl;
170
171		doc_both.SaveAsXFDF((output_path + "form1_filled_both.xfdf").c_str());
172
173		cout << "Done." << endl;
174	}
175	catch(Common::Exception& e)
176	{
177		cout << e << endl;
178		ret = 1;
179	}
180	catch(...)
181	{
182		cout << "Unknown Exception" << endl;
183		ret = 1;
184	}
185
186	// Example 4) Merge/Extract XFDF into/from PDF
187	try
188	{
189		// Merge XFDF from string
190		PDFDoc in_doc((input_path + "numbered.pdf").c_str());
191		in_doc.InitSecurityHandler();
192
193		cout << "Merge XFDF string into PDF." << endl;
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		FDFDoc fdoc(FDFDoc::CreateFromXFDF(str));
198		in_doc.FDFMerge(fdoc);
199		in_doc.Save((output_path + "numbered_modified.pdf").c_str(), SDFDoc::e_linearized, 0);
200		cout << "Merge complete." << endl;
201
202		// Extract XFDF as string
203		cout << "Extract XFDF as a string." << endl;
204
205		FDFDoc fdoc_new = in_doc.FDFExtract(PDFDoc::e_both);
206		UString XFDF_str = fdoc_new.SaveAsXFDF();
207		cout << "Extracted XFDF: " << endl;
208		cout << XFDF_str << endl;
209		cout << "Extract complete." << endl;
210	}
211	catch(Common::Exception& e)
212	{
213		cout << e << endl;
214		ret = 1;
215	}
216	catch(...)
217	{
218		cout << "Unknown Exception" << endl;
219		ret = 1;
220	}
221
222	// Example 5) Read FDF files directly
223	try  
224	{
225		FDFDoc doc((output_path + "form1_filled_data.fdf").c_str());
226
227		for(FDFFieldIterator itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next()) 
228		{
229			cout << "Field name: " << itr.Current().GetName() << endl;
230			cout << "Field partial name: " << itr.Current().GetPartialName() << endl;
231
232			cout << "------------------------------" << endl;
233		}
234
235		cout << "Done." << endl;
236	}
237	catch(Common::Exception& e)
238	{
239		cout << e << endl;
240		ret = 1;
241	}
242	catch(...)
243	{
244		cout << "Unknown Exception" << endl;
245		ret = 1;
246	}
247
248	// Example 6) Direct generation of FDF.
249	try  
250	{
251		FDFDoc doc;
252		// Create new fields (i.e. key/value pairs).
253		doc.FieldCreate("Company", PDF::Field::e_text, "PDFTron Systems");
254		doc.FieldCreate("First Name", PDF::Field::e_text, "John");
255		doc.FieldCreate("Last Name", PDF::Field::e_text, "Doe");
256		// ...		
257
258		// doc.SetPdfFileName("mydoc.pdf");
259
260		doc.Save((output_path + "sample_output.fdf").c_str());
261		cout << "Done. Results saved in sample_output.fdf" << endl;
262	}
263	catch(Common::Exception& e)
264	{
265		cout << e << endl;
266		ret = 1;
267	}
268	catch(...)
269	{
270		cout << "Unknown Exception" << endl;
271		ret = 1;
272	}
273
274	PDFNet::Terminate();
275	return ret;
276}
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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.SDFDoc;
8import com.pdftron.fdf.*;
9
10
11//---------------------------------------------------------------------------------------
12// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
13// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
14// available in PDFNet.
15//---------------------------------------------------------------------------------------
16public class FDFTest {
17    public static void main(String[] args) {
18
19        PDFNet.initialize(PDFTronLicense.Key());
20
21        // Relative path to the folder containing test files.
22        String input_path = "../../TestFiles/";
23        String output_path = "../../TestFiles/Output/";
24
25        // Example 1)
26        // Iterate over all form fields in the document. Display all field names.
27        try (PDFDoc doc = new PDFDoc((input_path + "form1.pdf"))) {
28            doc.initSecurityHandler();
29
30            for (FieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
31                Field current = itr.next();
32                System.out.println("Field name: " + current.getName());
33                System.out.println("Field partial name: " + current.getPartialName());
34
35                System.out.print("Field type: ");
36                int type = current.getType();
37                switch (type) {
38                    case Field.e_button:
39                        System.out.println("Button");
40                        break;
41                    case Field.e_check:
42                        System.out.println("Check");
43                        break;
44                    case Field.e_radio:
45                        System.out.println("Radio");
46                        break;
47                    case Field.e_text:
48                        System.out.println("Text");
49                        break;
50                    case Field.e_choice:
51                        System.out.println("Choice");
52                        break;
53                    case Field.e_signature:
54                        System.out.println("Signature");
55                        break;
56                    case Field.e_null:
57                        System.out.println("Null");
58                        break;
59                }
60
61                System.out.println("------------------------------");
62            }
63
64            System.out.println("Done.");
65        } catch (Exception e) {
66            e.printStackTrace();
67        }
68
69        // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
70        try (PDFDoc doc = new PDFDoc((input_path + "form1.pdf"))) {
71            // XFDF to FDF
72            // form fields
73            System.out.println("Import form field data from XFDF to FDF.");
74
75            FDFDoc fdf_doc1 = FDFDoc.createFromXFDF((input_path + "form1_data.xfdf"));
76            fdf_doc1.save(output_path + "form1_data.fdf");
77            // output FDF fdf_doc1
78
79            // annotations
80            System.out.println("Import annotations from XFDF to FDF.");
81
82            FDFDoc fdf_doc2 = FDFDoc.createFromXFDF((input_path + "form1_annots.xfdf"));
83            fdf_doc2.save(output_path + "form1_annots.fdf");
84            // output FDF fdf_doc2
85
86            // FDF to PDF
87            // form fields
88            System.out.println("Merge form field data from FDF.");
89
90            doc.initSecurityHandler();
91            doc.fdfMerge(fdf_doc1);
92
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();
96
97            doc.save((output_path + "form1_filled.pdf"), SDFDoc.SaveMode.LINEARIZED, null);
98            // output PDF doc
99
100            // annotations
101            System.out.println("Merge annotations from FDF.");
102
103            doc.fdfMerge(fdf_doc2);
104            // Refreshing missing appearances is not required here, but is recommended to make them 
105            // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
106            doc.refreshAnnotAppearances();
107            doc.save(output_path + "form1_filled_with_annots.pdf", SDFDoc.SaveMode.LINEARIZED, null);
108            System.out.println("Done.");
109        } catch (Exception e) {
110            e.printStackTrace();
111        }
112
113        // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
114        try (PDFDoc in_doc = new PDFDoc((output_path + "form1_filled_with_annots.pdf"))) {
115            // PDF to FDF
116            in_doc.initSecurityHandler();
117
118            // form fields only
119            System.out.println("Extract form fields data to FDF.");
120
121            FDFDoc doc_fields = in_doc.fdfExtract(PDFDoc.e_forms_only);
122            doc_fields.setPDFFileName("../form1_filled_with_annots.pdf");
123            doc_fields.save(output_path + "form1_filled_data.fdf");
124            // output FDF doc_fields
125
126            // annotations only
127            System.out.println("Extract annotations to FDF.");
128
129            FDFDoc doc_annots = in_doc.fdfExtract(PDFDoc.e_annots_only);
130            doc_annots.setPDFFileName("../form1_filled_with_annots.pdf");
131            doc_annots.save(output_path + "form1_filled_annot.fdf");
132            // output FDF doc_annots
133
134            // both form fields and annotations
135            System.out.println("Extract both form fields and annotations to FDF.");
136
137            FDFDoc doc_both = in_doc.fdfExtract(PDFDoc.e_both);
138            doc_both.setPDFFileName("../form1_filled_with_annots.pdf");
139            doc_both.save(output_path + "form1_filled_both.fdf");
140            // output FDF doc_both
141
142            // FDF to XFDF
143            // form fields
144            System.out.println("Export form field data from FDF to XFDF.");
145
146            doc_fields.saveAsXFDF((output_path + "form1_filled_data.xfdf"));
147            // output FDF doc_fields
148
149            // annotations
150            System.out.println("Export annotations from FDF to XFDF.");
151
152            doc_annots.saveAsXFDF((output_path + "form1_filled_annot.xfdf"));
153            // output FDF doc_annots
154
155            // both form fields and annotations
156            System.out.println("Export both form fields and annotations from FDF to XFDF.");
157
158            doc_both.saveAsXFDF((output_path + "form1_filled_both.xfdf"));
159            // output FDF doc_both
160
161            System.out.println("Done.");
162        } catch (Exception e) {
163            e.printStackTrace();
164        }
165
166        // Example 4) Merge/Extract XFDF into/from PDF
167        try (PDFDoc in_doc = new PDFDoc((input_path + "numbered.pdf"))) {
168            // Merge XFDF from string
169            in_doc.initSecurityHandler();
170
171            System.out.println("Merge XFDF string into PDF.");
172
173            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>";
174
175            FDFDoc fdoc = FDFDoc.createFromXFDF(str);
176            in_doc.fdfMerge(fdoc);
177            in_doc.save(output_path + "numbered_modified.pdf", SDFDoc.SaveMode.LINEARIZED, null);
178            // output PDF in_doc
179            System.out.println("Merge complete.");
180
181            // Extract XFDF as string
182            System.out.println("Extract XFDF as a string.");
183
184            FDFDoc fdoc_new = in_doc.fdfExtract(PDFDoc.e_both);
185            String XFDF_str = fdoc_new.saveAsXFDF();
186            System.out.println("Extracted XFDF: ");
187            System.out.println(XFDF_str);
188            System.out.println("Extract complete.");
189        } catch (Exception e) {
190            e.printStackTrace();
191        }
192
193        // Example 5) Read FDF files directly
194        try {
195            FDFDoc doc = new FDFDoc((output_path + "form1_filled_data.fdf"));
196
197            for (FDFFieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
198                FDFField current = itr.next();
199                System.out.println("Field name: " + current.getName());
200                System.out.println("Field partial name: " + current.getPartialName());
201
202                System.out.println("------------------------------");
203            }
204            doc.close();
205            System.out.println("Done.");
206        } catch (Exception e) {
207            e.printStackTrace();
208        }
209
210        // Example 6) Direct generation of FDF.
211        try {
212            FDFDoc doc = new FDFDoc();
213            // Create new fields (i.e. key/value pairs).
214            doc.fieldCreate("Company", Field.e_text, "PDFTron Systems");
215            doc.fieldCreate("First Name", Field.e_text, "John");
216            doc.fieldCreate("Last Name", Field.e_text, "Doe");
217            // ...
218
219            // doc.setPdfFileName("mydoc.pdf");
220
221            doc.save(output_path + "sample_output.fdf");
222            // output FDF doc
223            doc.close();
224            System.out.println("Done. Results saved in sample_output.fdf");
225        } catch (Exception e) {
226            e.printStackTrace();
227        }
228
229        PDFNet.terminate();
230    }
231}
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//---------------------------------------------------------------------------------------
7// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
8// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
9// available in PDFNet.
10//---------------------------------------------------------------------------------------
11
12const { PDFNet } = require('@pdftron/pdfnet-node');
13const PDFTronLicense = require('../LicenseKey/LicenseKey');
14
15((exports) => {
16
17  exports.runFDFTest = () => {
18    const main = async () => {
19      const inputPath = '../TestFiles/';
20      const outputPath = '../TestFiles/Output/';
21
22      // Example 1)
23      // Iterate over all form fields in the document. Display all field names.
24      try {
25        const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'form1.pdf');
26        doc.initSecurityHandler();
27
28        for (const itr = await doc.getFieldIteratorBegin(); await itr.hasNext(); itr.next()) {
29          const field = await itr.current();
30          console.log('Field name: ' + await field.getName());
31          console.log('Field partial name: ' + await field.getPartialName());
32
33          switch (await field.getType()) {
34            case PDFNet.Field.Type.e_button:
35              console.log('Field type: Button');
36              break;
37            case PDFNet.Field.Type.e_check:
38              console.log('Field type: Check');
39              break;
40            case PDFNet.Field.Type.e_radio:
41              console.log('Field type: Radio');
42              break;
43            case PDFNet.Field.Type.e_text:
44              console.log('Field type: Text');
45              break;
46            case PDFNet.Field.Type.e_choice:
47              console.log('Field type: Choice');
48              break;
49            case PDFNet.Field.Type.e_signature:
50              console.log('Field type: Signature');
51              break;
52            default:
53              console.log('Field type: Null');
54              break;
55          }
56          console.log('------------------------------')
57        }
58        console.log('Done.');
59      } catch (err) {
60        console.log(err);
61      }
62
63      // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
64      try {
65        // FDF to PDF
66        // form fields
67        console.log('Import form field data from XFDF to FDF.');
68
69        const fdf_doc1 = await PDFNet.FDFDoc.createFromXFDF(inputPath + 'form1_data.xfdf');
70        await fdf_doc1.save(outputPath + 'form1_data.fdf');
71
72        // annotations
73        console.log('Import annotations from XFDF to FDF.');
74
75        const fdf_doc2 = await PDFNet.FDFDoc.createFromXFDF(inputPath + 'form1_annots.xfdf');
76        await fdf_doc2.save(outputPath + 'form1_annots.fdf');
77
78        // FDF to PDF
79        // form fields
80        console.log('Merge form field data from FDF.');
81
82        const doc = await PDFNet.PDFDoc.createFromFilePath(`${inputPath}form1.pdf`);
83        doc.initSecurityHandler();
84        await doc.fdfMerge(fdf_doc1);
85
86        // Refreshing missing appearances is not required here, but is recommended to make them 
87        // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
88        doc.refreshAnnotAppearances();
89
90        await doc.save(outputPath + 'form1_filled.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
91
92        // annotations
93        console.log('Merge annotations from FDF.');
94
95        await doc.fdfMerge(fdf_doc2);
96        // Refreshing missing appearances is not required here, but is recommended to make them 
97        // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
98        doc.refreshAnnotAppearances();
99        await doc.save(outputPath + 'form1_filled_with_annots.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
100        console.log('Done.');
101      } catch (err) {
102        console.log(err);
103      }
104
105
106      // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
107      try {
108        // PDF to FDF
109        const in_doc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'form1_filled_with_annots.pdf');
110        in_doc.initSecurityHandler();
111
112        // form fields only
113        console.log('Extract form fields data to FDF.');
114
115        const doc_fields = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_forms_only);
116        doc_fields.setPDFFileName('../form1_filled_with_annots.pdf');
117        await doc_fields.save(outputPath + 'form1_filled_data.fdf');
118
119        // annotations only
120        console.log('Extract annotations to FDF.');
121
122        const doc_annots = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_annots_only);
123        doc_annots.setPDFFileName('../form1_filled_with_annots.pdf');
124        await doc_annots.save(outputPath + 'form1_filled_annot.fdf');
125
126        // both form fields and annotations
127        console.log('Extract both form fields and annotations to FDF.');
128
129        const doc_both = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
130        doc_both.setPDFFileName('../form1_filled_with_annots.pdf');
131        await doc_both.save(outputPath + 'form1_filled_both.fdf');
132
133        // FDF to XFDF
134        // form fields
135        console.log('Export form field data from FDF to XFDF.');
136
137        await doc_fields.saveAsXFDF(outputPath + 'form1_filled_data.xfdf');
138
139        // annotations
140        console.log('Export annotations from FDF to XFDF.');
141
142        await doc_annots.saveAsXFDF(outputPath + 'form1_filled_annot.xfdf');
143
144        // both form fields and annotations
145        console.log('Export both form fields and annotations from FDF to XFDF.');
146
147        await doc_both.saveAsXFDF(outputPath + 'form1_filled_both.xfdf');
148
149        console.log('Done.');
150      } catch (err) {
151        console.log(err);
152      }
153
154      // Example 4) Merge/Extract XFDF into/from PDF
155      try {
156        // Merge XFDF from string
157        const in_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'numbered.pdf');
158        in_doc.initSecurityHandler();
159
160        console.log('Merge XFDF string into PDF.');
161
162        const 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>`;
163
164        const fdoc = await PDFNet.FDFDoc.createFromXFDF(str);
165        in_doc.fdfMerge(fdoc);
166        await in_doc.save(outputPath + 'numbered_modified.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
167        console.log('Merge complete.');
168
169        // Extract XFDF as string
170        console.log('Extract XFDF as a string.');
171
172        const fdoc_new = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
173        const XFDF_str = await fdoc_new.saveAsXFDFAsString();
174        console.log('Extracted XFDF: ');
175        console.log(XFDF_str);
176        console.log('Extract complete.');
177      } catch (err) {
178        console.log(err);
179      }
180
181      // Example 5) Read FDF files directly
182      try {
183        const doc = await PDFNet.FDFDoc.createFromFilePath(outputPath + 'form1_filled_data.fdf');
184
185        for (const itr = await doc.getFieldIteratorBegin(); await itr.hasNext(); itr.next()) {
186          const field = await itr.current();
187          console.log('Field name: ' + await field.getName());
188          console.log('Field partial name: ' + await field.getPartialName());
189
190          console.log('------------------------------');
191        }
192
193        console.log('Done.');
194      } catch (err) {
195        console.log(err);
196      }
197
198      // Example 6) Direct generation of FDF.
199      try  
200      {
201        const doc = await PDFNet.FDFDoc.create();
202        // Create new fields (i.e. key/value pairs).
203        doc.fieldCreateFromString('Company', PDFNet.Field.Type.e_text, 'PDFTron Systems');
204        doc.fieldCreateFromString('First Name', PDFNet.Field.Type.e_text, 'John');
205        doc.fieldCreateFromString('Last Name', PDFNet.Field.Type.e_text, 'Doe');
206    
207        await doc.save(outputPath + 'sample_output.fdf');
208        console.log('Done. Results saved in sample_output.fdf');
209      } catch (err) {
210        console.log(err);
211      }
212    };
213
214    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
215  };
216  exports.runFDFTest();
217})(exports);
218// eslint-disable-next-line spaced-comment
219//# sourceURL=FDFTest.js
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}
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
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
20def main():
21    PDFNet.Initialize(LicenseKey)
22    
23    # Relative path to the folder containing the test files.
24    input_path = "../../TestFiles/"
25    output_path = "../../TestFiles/Output/"
26    
27    # Example 1
28    # Iterate over all form fields in the document. Display all field names.
29    
30    doc = PDFDoc(input_path + "form1.pdf")
31    doc.InitSecurityHandler()
32    
33    itr = doc.GetFieldIterator()
34    while itr.HasNext():
35        print("Field name: " + itr.Current().GetName())
36        print("Field partial name: " + itr.Current().GetPartialName())
37        
38        sys.stdout.write("Field type: ")
39        type = itr.Current().GetType()
40        if type == Field.e_button:
41            print("Button")
42        elif type == Field.e_check:
43            print("Check")
44        elif type == Field.e_radio:
45            print("Radio")
46        elif type == Field.e_text:
47            print("Text")
48        elif type == Field.e_choice:
49            print("Choice")
50        elif type == Field.e_signature:
51            print("Signiture")
52        elif type == Field.e_null:
53            print("Null")
54            
55        print("------------------------------")
56        itr.Next()
57    
58    doc.Close()
59    print("Done.")
60    
61    # Example 2
62    # Import XFDF into FDF, then merge data from FDF
63	
64	# XFDF to FDF
65	# form fields
66    print("Import form field data from XFDF to FDF.")
67	
68    fdf_doc1 = FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf")
69    fdf_doc1.Save(output_path + "form1_data.fdf")
70	
71	# annotations
72    print("Import annotations from XFDF to FDF.")
73	
74    fdf_doc2 = FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf")
75    fdf_doc2.Save(output_path + "form1_annots.fdf")
76	
77	# FDF to PDF
78	# form fields
79    print("Merge form field data from FDF.")
80	
81    doc = PDFDoc(input_path + "form1.pdf")
82    doc.InitSecurityHandler()
83    doc.FDFMerge(fdf_doc1)
84	
85    # Refreshing missing appearances is not required here, but is recommended to make them 
86    # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
87    doc.RefreshAnnotAppearances()
88	
89    doc.Save(output_path + "form1_filled.pdf", SDFDoc.e_linearized)
90	
91	# annotations
92    print("Merge annotations from FDF.")
93
94    doc.FDFMerge(fdf_doc2)
95    # Refreshing missing appearances is not required here, but is recommended to make them 
96    # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
97    doc.RefreshAnnotAppearances()
98    doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc.e_linearized)
99    doc.Close()
100    print("Done.")
101	
102    
103    # Example 3
104    # Extract data from PDF to FDF, then export FDF as XFDF
105    
106	# PDF to FDF
107    in_doc = PDFDoc(output_path + "form1_filled_with_annots.pdf")
108    in_doc.InitSecurityHandler()
109	
110	# form fields only
111    print("Extract form fields data to FDF.")
112	
113    doc_fields = in_doc.FDFExtract(PDFDoc.e_forms_only)
114    doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf")
115    doc_fields.Save(output_path + "form1_filled_data.fdf")
116	
117	# annotations only
118    print("Extract annotations to FDF.")
119	
120    doc_annots = in_doc.FDFExtract(PDFDoc.e_annots_only)
121    doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf")
122    doc_annots.Save(output_path + "form1_filled_annot.fdf")
123	
124	# both form fields and annotations
125    print("Extract both form fields and annotations to FDF.")
126	
127    doc_both = in_doc.FDFExtract(PDFDoc.e_both)
128    doc_both.SetPDFFileName("../form1_filled_with_annots.pdf")
129    doc_both.Save(output_path + "form1_filled_both.fdf")
130	
131	# FDF to XFDF
132	# form fields
133    print("Export form field data from FDF to XFDF.")
134	
135    doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")
136	
137	# annotations
138    print("Export annotations from FDF to XFDF.")
139	
140    doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")
141	
142	# both form fields and annotations
143    print("Export both form fields and annotations from FDF to XFDF.")
144	
145    doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
146	
147    in_doc.Close()
148    print("Done.")
149	
150    # Example 4
151    # Merge/Extract XFDF into/from PDF
152    
153    # Merge XFDF from string
154    in_doc = PDFDoc(input_path + "numbered.pdf")
155    in_doc.InitSecurityHandler()
156
157    print("Merge XFDF string into PDF.")
158
159    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>"
160
161    fdoc = FDFDoc.CreateFromXFDF(str)
162    in_doc.FDFMerge(fdoc)
163    in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc.e_linearized)
164    print("Merge complete.")
165
166    # Extract XFDF as string
167    print("Extract XFDF as a string.")
168
169    fdoc_new = in_doc.FDFExtract(PDFDoc.e_both)
170    XFDF_str = fdoc_new.SaveAsXFDF()
171    print("Extracted XFDF: ")
172    print(XFDF_str)
173    in_doc.Close()
174    print("Extract complete.") 	
175	
176    # Example 5
177    # Read FDF files directly
178    
179    doc = FDFDoc(output_path + "form1_filled_data.fdf")
180    
181    itr = doc.GetFieldIterator()
182    while itr.HasNext():
183        print("Field name: " + itr.Current().GetName())
184        print("Field partial name: " + itr.Current().GetPartialName())
185        print("------------------------------")
186        itr.Next()
187        
188    doc.Close()
189    print("Done.")
190    
191    # Example 6
192    # Direct generation of FDF
193    doc = FDFDoc()
194    
195    # Create new fields (i.r. key/value pairs
196    doc.FieldCreate("Company", Field.e_text, "PDFTron Systems")
197    doc.FieldCreate("First Name", Field.e_text, "John")
198    doc.FieldCreate("Last Name", Field.e_text, "Doe")
199    
200    doc.Save(output_path + "sample_output.fdf")
201    doc.Close()
202    PDFNet.Terminate()
203    print("Done. Results saved in sample_output.fdf")
204
205if __name__ == '__main__':
206    main()
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10//---------------------------------------------------------------------------------------
11// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
12// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
13// available in PDFNet.
14//---------------------------------------------------------------------------------------
15	PDFNet::Initialize($LicenseKey);
16	PDFNet::GetSystemFontList();    // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
17	
18	// Relative path to the folder containing the test files.
19	$input_path = getcwd()."/../../TestFiles/";
20	$output_path = $input_path."Output/";
21
22	// Example 1: 
23	// Iterate over all form fields in the document. Display all field names.
24
25	$doc = new PDFDoc($input_path."form1.pdf");
26	$doc->InitSecurityHandler();
27
28	for($itr = $doc->GetFieldIterator(); $itr->HasNext(); $itr->Next()) 
29	{
30		echo nl2br("Field name: ".$itr->Current()->GetName()."\n");
31		echo nl2br("Field partial name: ".$itr->Current()->GetPartialName()."\n");
32
33		echo "Field type: ";
34		$type = $itr->Current()->GetType();
35		switch($type)
36		{
37		case Field::e_button: echo nl2br("Button"."\n"); break;
38		case Field::e_check: echo nl2br("Check"."\n"); break;
39		case Field::e_radio: echo nl2br("Radio"."\n"); break;
40		case Field::e_text: echo nl2br("Text"."\n"); break;
41		case Field::e_choice: echo nl2br("Choice"."\n"); break;
42		case Field::e_signature: echo nl2br("Signature"."\n"); break;
43		case Field::e_null: echo nl2br("Null"."\n"); break;
44		}
45
46		echo nl2br("------------------------------\n");
47	}
48	
49	$doc->Close();
50	echo nl2br("Done.\n");
51
52	// Example 2) Import XFDF into FDF, then merge data from FDF into PDF
53	
54	// XFDF to FDF
55	// form fields
56	echo nl2br("Import form field data from XFDF to FDF.\n");
57	
58	$fdf_doc1 = FDFDoc::CreateFromXFDF($input_path."form1_data.xfdf");
59	$fdf_doc1->Save($output_path."form1_data.fdf");
60	
61	// annotations
62	echo nl2br("Import annotations from XFDF to FDF.\n");
63	
64	$fdf_doc2 = FDFDoc::CreateFromXFDF($input_path."form1_annots.xfdf");
65	$fdf_doc2->Save($output_path."form1_annots.fdf");	
66	
67	// FDF to PDF
68	// form fields
69	echo nl2br("Merge form field data from FDF.\n");
70	
71	$doc = new PDFDoc($input_path."form1.pdf");
72	$doc->InitSecurityHandler();
73	$doc->FDFMerge($fdf_doc1);
74	
75	// Refreshing missing appearances is not required here, but is recommended to make them 
76	// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
77	$doc->RefreshAnnotAppearances();
78	
79	$doc->Save(($output_path."form1_filled.pdf"), SDFDoc::e_linearized);
80	
81	// annotations
82	echo nl2br("Merge annotations from FDF.\n");
83	
84	$doc->FDFMerge($fdf_doc2);	
85	// Refreshing missing appearances is not required here, but is recommended to make them 
86	// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
87	$doc->RefreshAnnotAppearances();
88	$doc->Save(($output_path."form1_filled_with_annots.pdf"), SDFDoc::e_linearized);
89	$doc->Close();
90	echo nl2br("Done.\n");
91
92
93	// Example 3) Extract data from PDF to FDF, then export FDF as XFDF
94	
95	// PDF to FDF
96	$in_doc = new PDFDoc($output_path."form1_filled_with_annots.pdf");
97	$in_doc->InitSecurityHandler();
98	
99	// form fields only
100	echo nl2br("Extract form fields data to FDF.\n");
101	
102	$doc_fields = $in_doc->FDFExtract(PDFDoc::e_forms_only);
103	$doc_fields->SetPDFFileName("../form1_filled_with_annots.pdf");
104	$doc_fields->Save($output_path."form1_filled_data.fdf");
105	
106	// annotations only
107	echo nl2br("Extract annotations to FDF.\n");
108	
109	$doc_annots = $in_doc->FDFExtract(PDFDoc::e_annots_only);
110	$doc_annots->SetPDFFileName("../form1_filled_with_annots.pdf");
111	$doc_annots->Save($output_path."form1_filled_annot.fdf");
112	
113	// both form fields and annotations
114	echo nl2br("Extract both form fields and annotations to FDF.\n");
115	
116	$doc_both = $in_doc->FDFExtract(PDFDoc::e_both);
117	$doc_both->SetPDFFileName("../form1_filled_with_annots.pdf");
118	$doc_both->Save($output_path."form1_filled_both.fdf");
119	
120	// FDF to XFDF
121	// form fields
122	echo nl2br("Export form field data from FDF to XFDF.\n");
123	
124	$doc_fields->SaveAsXFDF($output_path."form1_filled_data.xfdf");
125	
126	// annotations
127	echo nl2br("Export annotations from FDF to XFDF.\n");
128	
129	$doc_annots->SaveAsXFDF($output_path."form1_filled_annot.xfdf");
130	
131	// both form fields and annotations
132	echo nl2br("Export both form fields and annotations from FDF to XFDF.\n");
133	
134	$doc_both->SaveAsXFDF($output_path."form1_filled_both.xfdf");
135	
136	$in_doc->Close();
137	echo nl2br("Done.\n");
138
139	// Example 4) Merge/Extract XFDF into/from PDF
140	
141	// Merge XFDF from string
142	$in_doc = new PDFDoc($input_path."numbered.pdf");
143	$in_doc->InitSecurityHandler();
144	
145	echo nl2br("Merge XFDF string into PDF.\n");
146	
147	$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>";
148	$fdoc = FDFDoc::CreateFromXFDF($str);
149	$in_doc->FDFMerge($fdoc);
150	$in_doc->Save(($output_path."numbered_modified.pdf"), SDFDoc::e_linearized);
151	echo nl2br("Merge complete.\n");
152	
153	// Extract XFDF as string
154	echo nl2br("Extract XFDF as a string.\n");
155	
156	$fdoc_new = $in_doc->FDFExtract(PDFDoc::e_both);
157	$XFDF_str = $fdoc_new->SaveAsXFDF();
158	echo nl2br("Extracted XFDF: \n");
159	echo nl2br($XFDF_str);
160	$in_doc->Close();
161	echo nl2br("\nExtract complete.\n");
162	
163	// Example 5) Read FDF files directly
164	
165	$doc = new FDFDoc($output_path."form1_filled_data.fdf");
166
167	for($itr = $doc->GetFieldIterator(); $itr->HasNext(); $itr->Next())
168	{
169		echo nl2br("Field name: ".$itr->Current()->GetName()."\n");
170		echo nl2br("Field partial name: ".$itr->Current()->GetPartialName()."\n");
171		echo nl2br("------------------------------\n");
172	}
173	
174	$doc->Close();
175	echo nl2br("Done.\n");
176
177	// Example 6) Direct generation of FDF.
178	
179	$doc = new FDFDoc();
180	// Create new fields (i.e. key/value pairs).
181	$doc->FieldCreate("Company", Field::e_text, "PDFTron Systems");
182	$doc->FieldCreate("First Name", Field::e_text, "John");
183	$doc->FieldCreate("Last Name", Field::e_text, "Doe");
184	// ...		
185
186	// $doc->SetPdfFileName("mydoc.pdf");
187	
188	$doc->Save($output_path."sample_output.fdf");
189	$doc->Close();
190	PDFNet::Terminate();
191	echo nl2br("Done. Results saved in sample_output.fdf");
192	
193	
194?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
14# forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
15# available in PDFNet.
16#---------------------------------------------------------------------------------------
17
18	PDFNet.Initialize(PDFTronLicense.Key)
19	
20	# Relative path to the folder containing the test files.
21	input_path = "../../TestFiles/"
22	output_path = "../../TestFiles/Output/"
23	
24	# Example 1
25	# Iterate over all form fields in the document. Display all field names.
26	
27	doc = PDFDoc.new(input_path + "form1.pdf")
28	doc.InitSecurityHandler()
29	
30	itr = doc.GetFieldIterator()
31	while itr.HasNext() do
32		puts "Field name: " + itr.Current().GetName()
33		puts "Field partial name: " + itr.Current().GetPartialName()
34		
35		print "Field type: "
36		type = itr.Current().GetType()
37		if type == Field::E_button
38			puts "Button"
39		elsif type == Field::E_check
40			puts "Check"
41		elsif type == Field::E_radio
42			puts "Radio"
43		elsif type == Field::E_text
44			puts "Text"
45		elsif type == Field::E_choice
46			puts "Choice"
47		elsif type == Field::E_signature
48			puts "Signiture"
49		elsif type == Field::E_null
50			puts "Null"
51		end
52			
53		puts "------------------------------"
54		itr.Next()
55	end
56	
57	doc.Close()
58	puts "Done."
59	
60	# Example 2
61	# Import XFDF into FDF, then merge data from FDF into PDF
62	
63	# XFDF to FDF
64	# form fields
65	puts "Import form field data from XFDF to FDF."
66	
67	fdf_doc1 = FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf")
68	fdf_doc1.Save(output_path + "form1_data.fdf")
69	
70	# annotations
71	puts "Import annotations from XFDF to FDF."
72	
73	fdf_doc2 = FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf")
74	fdf_doc2.Save(output_path + "form1_annots.fdf")
75	
76	# FDF to PDF
77	# form fields
78	puts "Merge form field data from FDF."
79	
80	doc = PDFDoc.new(input_path + "form1.pdf")
81	doc.InitSecurityHandler()
82	doc.FDFMerge(fdf_doc1)
83	
84	# Refreshing missing appearances is not required here, but is recommended to make them  
85	# visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
86	doc.RefreshAnnotAppearances()
87	
88	doc.Save(output_path + "form1_filled.pdf", SDFDoc::E_linearized)
89	
90	# annotations
91	puts "Merge annotations from FDF."
92	
93	doc.FDFMerge(fdf_doc2)
94	# Refreshing missing appearances is not required here, but is recommended to make them  
95	# visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
96	doc.RefreshAnnotAppearances()
97	doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc::E_linearized)
98	doc.Close()
99	puts "Done."
100	
101	
102	# Example 3
103	# Extract data from PDF to FDF, then export FDF as XFDF
104	
105	# PDF to FDF
106	in_doc = PDFDoc.new(output_path + "form1_filled_with_annots.pdf")
107	in_doc.InitSecurityHandler()
108	
109	# form fields only
110	puts "Extract form fields data to FDF."
111	
112	doc_fields = in_doc.FDFExtract(PDFDoc::E_forms_only)
113	doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf")
114	doc_fields.Save(output_path + "form1_filled_data.fdf")
115	
116	# annotations only
117	puts "Extract annotations to FDF."
118	
119	doc_annots = in_doc.FDFExtract(PDFDoc::E_annots_only)
120	doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf")
121	doc_annots.Save(output_path + "form1_filled_annot.fdf")
122	
123	# both form fields and annotations
124	puts "Extract both form fields and annotations to FDF."
125	
126	doc_both = in_doc.FDFExtract(PDFDoc::E_both)
127	doc_both.SetPDFFileName("../form1_filled_with_annots.pdf")
128	doc_both.Save(output_path + "form1_filled_both.fdf")
129	
130	# FDF to XFDF
131	# form fields
132	puts "Export form field data from FDF to XFDF."
133	
134	doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")
135	
136	# annotations
137	puts "Export annotations from FDF to XFDF."
138	
139	doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")
140	
141	# both form fields and annotations
142	puts "Export both form fields and annotations from FDF to XFDF."
143	
144	doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
145	
146	in_doc.Close()
147	puts "Done."
148
149	# Example 4
150	# Merge/Extract XFDF into/from PDF
151	in_doc = PDFDoc.new(input_path + "numbered.pdf")
152	in_doc.InitSecurityHandler()
153	
154	puts "Merge XFDF string into PDF."
155	
156	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>"
157	
158	fdoc = FDFDoc.CreateFromXFDF(str)
159	in_doc.FDFMerge(fdoc)
160	in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc::E_linearized)
161	puts "Merge complete."
162	
163	# Extract XFDF as string
164	puts "Extract XFDF as a string."
165	
166	fdoc_new = in_doc.FDFExtract(PDFDoc::E_both)
167	XFDF_str = fdoc_new.SaveAsXFDF()
168	puts "Extracted XFDF: "
169	puts XFDF_str
170	in_doc.Close()
171	puts "Extract complete."
172	
173	# Example 5
174	# Read FDF files directly
175	
176	doc = FDFDoc.new(output_path + "form1_filled_data.fdf")
177	
178	itr = doc.GetFieldIterator()
179	while itr.HasNext() do
180		puts "Field name: " + itr.Current().GetName()
181		puts "Field partial name: " + itr.Current().GetPartialName()
182		puts "------------------------------"
183		itr.Next()
184	end
185		
186	doc.Close()
187	puts "Done."
188	
189	# Example 6
190	# Direct generation of FDF
191	doc = FDFDoc.new()
192	
193	# Create new fields (i.r. key/value pairs
194	doc.FieldCreate("Company", Field::E_text, "PDFTron Systems")
195	doc.FieldCreate("First Name", Field::E_text, "John")
196	doc.FieldCreate("Last Name", Field::E_text, "Doe")
197	
198	doc.Save(output_path + "sample_output.fdf")
199	doc.Close()
200	PDFNet.Terminate
201	puts "Done. Results saved in sample_output.fdf"
1'---------------------------------------------------------------------------------------
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3' Consult legal.txt regarding legal and license information.
4'---------------------------------------------------------------------------------------
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.SDF
10Imports pdftron.FDF
11Imports pdftron.PDF
12
13Module FDFTestVB
14	Dim pdfNetLoader As PDFNetLoader
15	Sub New()
16		pdfNetLoader = pdftron.PDFNetLoader.Instance()
17	End Sub
18
19	'---------------------------------------------------------------------------------------
20	' PDFNet includes full support for FDF (Forms Data Format) and for merging/extracting
21	' forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
22	' available in PDFNet.
23	'---------------------------------------------------------------------------------------
24	Sub Main()
25
26		PDFNet.Initialize(PDFTronLicense.Key)
27
28		' Relative path to the folder containing test files.
29		Dim input_path As String = "../../../../TestFiles/"
30		Dim output_path As String = "../../../../TestFiles/Output/"
31
32		' Example 1)
33		' Iterate over all form fields in the document. Display all field names.
34		Try
35			Using doc As PDFDoc = New PDFDoc(input_path + "form1.pdf")
36				doc.InitSecurityHandler()
37
38				Dim itr As FieldIterator = doc.GetFieldIterator()
39				While itr.HasNext()
40					Console.WriteLine("Field name: {0:s}", itr.Current().GetName())
41					Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName())
42
43					Console.Write("Field type: ")
44					Dim type As Field.Type = itr.Current().GetType()
45					If type = Field.Type.e_button Then
46						Console.WriteLine("Button")
47					ElseIf type = Field.Type.e_check Then
48						Console.WriteLine("Check")
49					ElseIf type = Field.Type.e_radio Then
50						Console.WriteLine("Radio")
51					ElseIf type = Field.Type.e_text Then
52						Console.WriteLine("Text")
53					ElseIf type = Field.Type.e_choice Then
54						Console.WriteLine("Choice")
55					ElseIf type = Field.Type.e_signature Then
56						Console.WriteLine("Signature")
57					ElseIf type = Field.Type.e_null Then
58						Console.WriteLine("Null")
59					End If
60					Console.WriteLine("------------------------------")
61					itr.Next()
62				End While
63			End Using
64			Console.WriteLine("Done.")
65		Catch e As Exception
66			Console.WriteLine("Exception caught:\n{0}", e)
67		End Try
68
69		' Example 2) Import XFDF into FDF, then merge data from FDF into PDF
70		Try
71			' XFDF to FDF
72			' form fields
73			Console.WriteLine("Import form field data from XFDF to FDF.")
74			
75			Dim fdf_doc1 As FDFDoc = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf"))
76			fdf_doc1.Save(output_path + "form1_data.fdf")
77			
78			' annotations
79			Console.WriteLine("Import annotations from XFDF to FDF.")
80			
81			Dim fdf_doc2 As FDFDoc = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf"))
82			fdf_doc2.Save(output_path + "form1_annots.fdf")
83			
84			' FDF to PDF
85			' form fields
86			Console.WriteLine("Merge form field data from FDF.")
87			
88			Using doc As PDFDoc = New PDFDoc(input_path + "form1.pdf")
89				doc.InitSecurityHandler()
90				doc.FDFMerge(fdf_doc1)
91
92				' Refreshing missing appearances is not required here, but is recommended to make them  
93				' visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
94				doc.RefreshAnnotAppearances()
95
96				doc.Save(output_path + "form1_filled.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
97
98				' annotations
99				Console.WriteLine("Merge annotations from FDF.")
100
101				doc.FDFMerge(fdf_doc2)
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				doc.Save(output_path + "form1_filled_with_annots.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
106			End Using
107
108			Console.WriteLine("Done.")
109		Catch e As Exception
110			Console.WriteLine("Exception caught:\n{0}", e)
111		End Try
112
113		' Example 3) Extract data from PDF to FDF, then export FDF as XFDF
114		Try
115			' PDF to FDF
116			Using in_doc As PDFDoc = New PDFDoc(output_path + "form1_filled_with_annots.pdf")
117				in_doc.InitSecurityHandler()
118
119				' form fields only
120				Console.WriteLine("Extract form fields data to FDF.")
121
122				Dim doc_fields As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_forms_only)
123				doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf")
124				doc_fields.Save(output_path + "form1_filled_data.fdf")
125
126				' annotations only
127				Console.WriteLine("Extract annotations to FDF.")
128
129				Dim doc_annots As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_annots_only)
130				doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf")
131				doc_annots.Save(output_path + "form1_filled_annot.fdf")
132
133				' both form fields and annotations
134				Console.WriteLine("Extract both form fields and annotations to FDF.")
135
136				Dim doc_both As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_both)
137				doc_both.SetPdfFileName("../form1_filled_with_annots.pdf")
138				doc_both.Save(output_path + "form1_filled_both.fdf")
139
140				' FDF to XFDF
141				' form fields
142				Console.WriteLine("Export form field data from FDF to XFDF.")
143
144				doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")
145
146				' annotations
147				Console.WriteLine("Export annotations from FDF to XFDF.")
148
149				doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")
150
151				' both form fields and annotations
152				Console.WriteLine("Export both form fields and annotations from FDF to XFDF.")
153
154				doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
155			End Using
156			Console.WriteLine("Done.")
157		Catch e As Exception
158			Console.WriteLine("Exception caught:\n{0}", e)
159		End Try
160
161		' Example 4) Merge/Extract XFDF into/from PDF
162		Try
163			' Merge XFDF from string
164			Using in_doc As PDFDoc = New PDFDoc(input_path + "numbered.pdf")
165				in_doc.InitSecurityHandler()
166
167				Console.WriteLine("Merge XFDF string into PDF.")
168				Dim str As String = "<?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>"
169
170				Dim fdoc As FDFDoc = New FDFDoc(FDFDoc.CreateFromXFDF(str))
171				in_doc.FDFMerge(fdoc)
172				in_doc.Save(output_path + "numbered_modified.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
173				Console.WriteLine("Merge complete.")
174
175				' Extract XFDF as string
176				Console.WriteLine("Extract XFDF as a string.")
177				Dim fdoc_new As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_both)
178				Dim XFDF_str As String = fdoc_new.SaveAsXFDF()
179				Console.WriteLine("Extracted XFDF: ")
180				Console.WriteLine(XFDF_str)
181			End Using
182			Console.WriteLine("Extract complete.")
183
184		Catch e As Exception
185			Console.WriteLine("Exception caught:\n{0}", e)
186		End Try
187
188		' Example 5) Read FDF files directly
189		Try
190			Dim doc As FDFDoc = New FDFDoc(output_path + "form1_filled_data.fdf")
191
192			Dim itr As FDFFieldIterator = doc.GetFieldIterator()
193			While itr.HasNext()
194				Console.WriteLine("Field name: {0:s}", itr.Current().GetName())
195				Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName())
196				Console.WriteLine("------------------------------")
197				itr.Next()
198			End While
199
200			Console.WriteLine("Done.")
201		Catch e As Exception
202			Console.WriteLine("Exception caught:\n{0}", e)
203		End Try
204
205		' Example 6) Direct generation of FDF.
206		Try
207			Dim doc As FDFDoc = New FDFDoc
208
209			' Create new fields (i.e. key/value pairs).
210			doc.FieldCreate("Company", Int(Field.Type.e_text), "PDFTron Systems")
211			doc.FieldCreate("First Name", Int(Field.Type.e_text), "John")
212			doc.FieldCreate("Last Name", Int(Field.Type.e_text), "Doe")
213			' ...		
214
215			' doc.SetPdfFileName("mydoc.pdf");
216			doc.Save(output_path + "sample_output.fdf")
217			Console.WriteLine("Done. Results saved in sample_output.fdf")
218		Catch e As Exception
219			Console.WriteLine("Exception caught:\n{0}", e)
220		End Try
221		PDFNet.Terminate()
222	End Sub
223End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales