> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/get-started/samples/fdftest.md).

# PDF Form Fill, Form Data Extraction with Forms Data Format (FDF)

Sample code for using Apryse Server SDK to programmatically merge forms data with the PDF in order to fill forms, or to extract form field data from the PDF, support for Forms Data Format (FDF). Sampl

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](https://apryse.com/capabilities/extraction).

To start your free trial, [get stated with Server SDK](/core/get-started/get-started.md).

{% tabs %}
{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/Field.h>
#include <FDF/FDFDoc.h>
#include <FDF/FDFField.h>
#include <iostream>
#include "../../LicenseKey/CPP/LicenseKey.h"

using namespace std;
using namespace pdftron;
using namespace SDF;
using namespace FDF;
using namespace PDF;

//---------------------------------------------------------------------------------------
// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
// available in PDFNet.
//---------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
	int ret = 0;
	PDFNet::Initialize(LicenseKey);

	// Relative path to the folder containing test files.
	string input_path =  "../../TestFiles/";
	string output_path = "../../TestFiles/Output/";


	// Example 1)
	// Iterate over all form fields in the document. Display all field names.
	try  
	{
		PDFDoc doc((input_path + "form1.pdf").c_str());
		doc.InitSecurityHandler();

		for(FieldIterator itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next()) 
		{
			cout << "Field name: " << itr.Current().GetName() << endl;
			cout << "Field partial name: " << itr.Current().GetPartialName() << endl;

			cout << "Field type: ";
			Field::Type type = itr.Current().GetType();
			switch(type)
			{
			case Field::e_button: cout << "Button" << endl; break;
			case Field::e_check: cout << "Check" << endl; break;
			case Field::e_radio: cout << "Radio" << endl; break;
			case Field::e_text: cout << "Text" << endl; break;
			case Field::e_choice: cout << "Choice" << endl; break;
			case Field::e_signature: cout << "Signature" << endl; break;
			case Field::e_null: cout << "Null" << endl; break;
			}

			cout << "------------------------------" << endl;
		}

		cout << "Done." << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}


	// Example 2) Import XFDF into FDF, then merge data from FDF into PDF
	try
	{
		// XFDF to FDF
		// form fields
		cout << "Import form field data from XFDF to FDF." << endl;
		
		FDFDoc fdf_doc1(FDFDoc::CreateFromXFDF((input_path + "form1_data.xfdf").c_str()));
		fdf_doc1.Save((output_path + "form1_data.fdf").c_str());

		// annotations
		cout << "Import annotations from XFDF to FDF." << endl;

		FDFDoc fdf_doc2(FDFDoc::CreateFromXFDF((input_path + "form1_annots.xfdf").c_str()));
		fdf_doc2.Save((output_path + "form1_annots.fdf").c_str());

		// FDF to PDF
		// form fields
		cout << "Merge form field data from FDF." << endl;

		PDFDoc doc((input_path + "form1.pdf").c_str());
		doc.InitSecurityHandler();
		doc.FDFMerge(fdf_doc1);
		
		// Refreshing missing appearances is not required here, but is recommended to make them 
		// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
		doc.RefreshAnnotAppearances();

		doc.Save((output_path + "form1_filled.pdf").c_str(), SDFDoc::e_linearized, 0);

		// annotations
		cout << "Merge annotations from FDF." << endl;

		doc.FDFMerge(fdf_doc2);
		// Refreshing missing appearances is not required here, but is recommended to make them 
		// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
		doc.RefreshAnnotAppearances();
		doc.Save((output_path + "form1_filled_with_annots.pdf").c_str(), SDFDoc::e_linearized, 0);
		cout << "Done." << endl;
	}
	catch (Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch (...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}


	// Example 3) Extract data from PDF to FDF, then export FDF as XFDF
	try  
	{
		// PDF to FDF
		PDFDoc in_doc((output_path + "form1_filled_with_annots.pdf").c_str());
		in_doc.InitSecurityHandler();
		
		// form fields only
		cout << "Extract form fields data to FDF." << endl;

		FDFDoc doc_fields = in_doc.FDFExtract(PDFDoc::e_forms_only);
		doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf");
		doc_fields.Save((output_path + "form1_filled_data.fdf").c_str());

		// annotations only
		cout << "Extract annotations to FDF." << endl;

		FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc::e_annots_only);
		doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf");
		doc_annots.Save((output_path + "form1_filled_annot.fdf").c_str());

		// both form fields and annotations
		cout << "Extract both form fields and annotations to FDF." << endl;

		FDFDoc doc_both = in_doc.FDFExtract(PDFDoc::e_both);
		doc_both.SetPDFFileName("../form1_filled_with_annots.pdf");
		doc_both.Save((output_path + "form1_filled_both.fdf").c_str());

		// FDF to XFDF
		// form fields
		cout << "Export form field data from FDF to XFDF." << endl;

		doc_fields.SaveAsXFDF((output_path + "form1_filled_data.xfdf").c_str());

		// annotations
		cout << "Export annotations from FDF to XFDF." << endl;

		doc_annots.SaveAsXFDF((output_path + "form1_filled_annot.xfdf").c_str());

		// both form fields and annotations
		cout << "Export both form fields and annotations from FDF to XFDF." << endl;

		doc_both.SaveAsXFDF((output_path + "form1_filled_both.xfdf").c_str());

		cout << "Done." << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	// Example 4) Merge/Extract XFDF into/from PDF
	try
	{
		// Merge XFDF from string
		PDFDoc in_doc((input_path + "numbered.pdf").c_str());
		in_doc.InitSecurityHandler();

		cout << "Merge XFDF string into PDF." << endl;

		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>";

		FDFDoc fdoc(FDFDoc::CreateFromXFDF(str));
		in_doc.FDFMerge(fdoc);
		in_doc.Save((output_path + "numbered_modified.pdf").c_str(), SDFDoc::e_linearized, 0);
		cout << "Merge complete." << endl;

		// Extract XFDF as string
		cout << "Extract XFDF as a string." << endl;

		FDFDoc fdoc_new = in_doc.FDFExtract(PDFDoc::e_both);
		UString XFDF_str = fdoc_new.SaveAsXFDF();
		cout << "Extracted XFDF: " << endl;
		cout << XFDF_str << endl;
		cout << "Extract complete." << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	// Example 5) Read FDF files directly
	try  
	{
		FDFDoc doc((output_path + "form1_filled_data.fdf").c_str());

		for(FDFFieldIterator itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next()) 
		{
			cout << "Field name: " << itr.Current().GetName() << endl;
			cout << "Field partial name: " << itr.Current().GetPartialName() << endl;

			cout << "------------------------------" << endl;
		}

		cout << "Done." << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	// Example 6) Direct generation of FDF.
	try  
	{
		FDFDoc doc;
		// Create new fields (i.e. key/value pairs).
		doc.FieldCreate("Company", PDF::Field::e_text, "PDFTron Systems");
		doc.FieldCreate("First Name", PDF::Field::e_text, "John");
		doc.FieldCreate("Last Name", PDF::Field::e_text, "Doe");
		// ...		

		// doc.SetPdfFileName("mydoc.pdf");

		doc.Save((output_path + "sample_output.fdf").c_str());
		cout << "Done. Results saved in sample_output.fdf" << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	PDFNet::Terminate();
	return ret;
}
```

{% endcode %}
{% endtab %}

{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
//
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
//

using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;
using pdftron.FDF;

namespace FDFTestCS
{
	/// <summary>
	/// PDFNet includes full support for FDF (Forms Data Format) and for merging/extracting
	/// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
	/// available in PDFNet.
	/// </summary>
	class Class1
	{
		private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
		static Class1() {}
		
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main(string[] args)
		{
			PDFNet.Initialize(PDFTronLicense.Key);

			// Relative path to the folder containing test files.
			string input_path =  "../../../../TestFiles/";
			string output_path = "../../../../TestFiles/Output/";

			// Example 1)
			// Iterate over all form fields in the document. Display all field names.
			try  
			{
				using (PDFDoc doc = new PDFDoc(input_path + "form1.pdf"))
				{
					doc.InitSecurityHandler();
					
					FieldIterator itr;
					for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
					{
						Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
						Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());

						Console.Write("Field type: ");
						Field.Type type = itr.Current().GetType();
						switch(type)
						{
							case Field.Type.e_button: 
								Console.WriteLine("Button"); break;
							case Field.Type.e_check: 
								Console.WriteLine("Check"); break;
							case Field.Type.e_radio: 
								Console.WriteLine("Radio"); break;
							case Field.Type.e_text: 
								Console.WriteLine("Text"); break;
							case Field.Type.e_choice: 
								Console.WriteLine("Choice"); break;
							case Field.Type.e_signature: 
								Console.WriteLine("Signature"); break;
							case Field.Type.e_null: 
								Console.WriteLine("Null"); break;
						}

						Console.WriteLine("------------------------------");
					}

					Console.WriteLine("Done.");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			// Example 2) Import XFDF into FDF, then merge data from FDF into PDF
			try  
			{
				// XFDF to FDF
				// form fields
				Console.WriteLine("Import form field data from XFDF to FDF.");
				
				FDFDoc fdf_doc1 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf"));
				fdf_doc1.Save(output_path + "form1_data.fdf");
				
				// annotations
				Console.WriteLine("Import annotations from XFDF to FDF.");
				
				FDFDoc fdf_doc2 = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf"));
				fdf_doc2.Save(output_path + "form1_annots.fdf");
				
				// FDF to PDF
				// form fields
				Console.WriteLine("Merge form field data from FDF.");
				
				using (PDFDoc doc = new PDFDoc(input_path + "form1.pdf"))
				{
					doc.InitSecurityHandler();
					doc.FDFMerge(fdf_doc1);
					
					// Refreshing missing appearances is not required here, but is recommended to make them 
					// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
					doc.RefreshAnnotAppearances();
					
					doc.Save(output_path + "form1_filled.pdf", SDFDoc.SaveOptions.e_linearized);
					
					// annotations
					Console.WriteLine("Merge annotations from FDF.");
					
					doc.FDFMerge(fdf_doc2);
					// Refreshing missing appearances is not required here, but is recommended to make them 
					// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
					doc.RefreshAnnotAppearances();
					doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc.SaveOptions.e_linearized);

					Console.WriteLine("Done.");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			// Example 3) Extract data from PDF to FDF, then export FDF as XFDF
			try  
			{
				// PDF to FDF
				using (PDFDoc in_doc = new PDFDoc(output_path + "form1_filled_with_annots.pdf"))
				{
					in_doc.InitSecurityHandler();
					
					// form fields only
					Console.WriteLine("Extract form fields data to FDF.");
					
					FDFDoc doc_fields = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_forms_only);
					doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf");
					doc_fields.Save(output_path + "form1_filled_data.fdf");
					
					// annotations only
					Console.WriteLine("Extract annotations to FDF.");
					
					FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only);
					doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf");
					doc_annots.Save(output_path + "form1_filled_annot.fdf");
					
					// both form fields and annotations
					Console.WriteLine("Extract both form fields and annotations to FDF.");
					
					FDFDoc doc_both = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
					doc_both.SetPdfFileName("../form1_filled_with_annots.pdf");
					doc_both.Save(output_path + "form1_filled_both.fdf");
					
					// FDF to XFDF
					// form fields
					Console.WriteLine("Export form field data from FDF to XFDF.");
					
					doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf");
					
					// annotations
					Console.WriteLine("Export annotations from FDF to XFDF.");
					
					doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf");
					
					// both form fields and annotations
					Console.WriteLine("Export both form fields and annotations from FDF to XFDF.");
					
					doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf");
					
					Console.WriteLine("Done.");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			// Example 4) Merge/Extract XFDF into/from PDF
			try
			{
				// Merge XFDF from string
				PDFDoc in_doc = new PDFDoc(input_path + "numbered.pdf");
				{
					in_doc.InitSecurityHandler();

					Console.WriteLine("Merge XFDF string into PDF.");

					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>";

					using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str)))
					{
						in_doc.FDFMerge(fdoc);
						in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc.SaveOptions.e_linearized);
						Console.WriteLine("Merge complete.");
					}

					// Extract XFDF as string
					Console.WriteLine("Extract XFDF as a string.");
					FDFDoc fdoc_new = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
					string XFDF_str = fdoc_new.SaveAsXFDF();
					Console.WriteLine("Extracted XFDF: ");
					Console.WriteLine(XFDF_str);
					Console.WriteLine("Extract complete.");
				}
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			// Example 5) Read FDF files directly
			try  
			{
				FDFDoc doc = new FDFDoc(output_path + "form1_filled_data.fdf");
				FDFFieldIterator itr = doc.GetFieldIterator();
				for(; itr.HasNext(); itr.Next()) 
				{
					Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
					Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
					Console.WriteLine("------------------------------");
				}

				Console.WriteLine("Done.");
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}

			// Example 6) Direct generation of FDF.
			try  
			{
				FDFDoc doc = new FDFDoc();

				// Create new fields (i.e. key/value pairs).
				doc.FieldCreate("Company", (int)Field.Type.e_text, "PDFTron Systems");
				doc.FieldCreate("First Name", (int)Field.Type.e_text, "John");
				doc.FieldCreate("Last Name", (int)Field.Type.e_text, "Doe");
				// ...		

				// doc.SetPdfFileName("mydoc.pdf");
				doc.Save(output_path + "sample_output.fdf");
				Console.WriteLine("Done. Results saved in sample_output.fdf");
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
			}
			PDFNet.Terminate();
		}
	}
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import com.pdftron.pdf.*;
import com.pdftron.sdf.SDFDoc;
import com.pdftron.fdf.*;


//---------------------------------------------------------------------------------------
// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
// available in PDFNet.
//---------------------------------------------------------------------------------------
public class FDFTest {
    public static void main(String[] args) {

        PDFNet.initialize(PDFTronLicense.Key());

        // Relative path to the folder containing test files.
        String input_path = "../../TestFiles/";
        String output_path = "../../TestFiles/Output/";

        // Example 1)
        // Iterate over all form fields in the document. Display all field names.
        try (PDFDoc doc = new PDFDoc((input_path + "form1.pdf"))) {
            doc.initSecurityHandler();

            for (FieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
                Field current = itr.next();
                System.out.println("Field name: " + current.getName());
                System.out.println("Field partial name: " + current.getPartialName());

                System.out.print("Field type: ");
                int type = current.getType();
                switch (type) {
                    case Field.e_button:
                        System.out.println("Button");
                        break;
                    case Field.e_check:
                        System.out.println("Check");
                        break;
                    case Field.e_radio:
                        System.out.println("Radio");
                        break;
                    case Field.e_text:
                        System.out.println("Text");
                        break;
                    case Field.e_choice:
                        System.out.println("Choice");
                        break;
                    case Field.e_signature:
                        System.out.println("Signature");
                        break;
                    case Field.e_null:
                        System.out.println("Null");
                        break;
                }

                System.out.println("------------------------------");
            }

            System.out.println("Done.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
        try (PDFDoc doc = new PDFDoc((input_path + "form1.pdf"))) {
            // XFDF to FDF
            // form fields
            System.out.println("Import form field data from XFDF to FDF.");

            FDFDoc fdf_doc1 = FDFDoc.createFromXFDF((input_path + "form1_data.xfdf"));
            fdf_doc1.save(output_path + "form1_data.fdf");
            // output FDF fdf_doc1

            // annotations
            System.out.println("Import annotations from XFDF to FDF.");

            FDFDoc fdf_doc2 = FDFDoc.createFromXFDF((input_path + "form1_annots.xfdf"));
            fdf_doc2.save(output_path + "form1_annots.fdf");
            // output FDF fdf_doc2

            // FDF to PDF
            // form fields
            System.out.println("Merge form field data from FDF.");

            doc.initSecurityHandler();
            doc.fdfMerge(fdf_doc1);

            // Refreshing missing appearances is not required here, but is recommended to make them 
            // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
            doc.refreshAnnotAppearances();

            doc.save((output_path + "form1_filled.pdf"), SDFDoc.SaveMode.LINEARIZED, null);
            // output PDF doc

            // annotations
            System.out.println("Merge annotations from FDF.");

            doc.fdfMerge(fdf_doc2);
            // Refreshing missing appearances is not required here, but is recommended to make them 
            // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
            doc.refreshAnnotAppearances();
            doc.save(output_path + "form1_filled_with_annots.pdf", SDFDoc.SaveMode.LINEARIZED, null);
            System.out.println("Done.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
        try (PDFDoc in_doc = new PDFDoc((output_path + "form1_filled_with_annots.pdf"))) {
            // PDF to FDF
            in_doc.initSecurityHandler();

            // form fields only
            System.out.println("Extract form fields data to FDF.");

            FDFDoc doc_fields = in_doc.fdfExtract(PDFDoc.e_forms_only);
            doc_fields.setPDFFileName("../form1_filled_with_annots.pdf");
            doc_fields.save(output_path + "form1_filled_data.fdf");
            // output FDF doc_fields

            // annotations only
            System.out.println("Extract annotations to FDF.");

            FDFDoc doc_annots = in_doc.fdfExtract(PDFDoc.e_annots_only);
            doc_annots.setPDFFileName("../form1_filled_with_annots.pdf");
            doc_annots.save(output_path + "form1_filled_annot.fdf");
            // output FDF doc_annots

            // both form fields and annotations
            System.out.println("Extract both form fields and annotations to FDF.");

            FDFDoc doc_both = in_doc.fdfExtract(PDFDoc.e_both);
            doc_both.setPDFFileName("../form1_filled_with_annots.pdf");
            doc_both.save(output_path + "form1_filled_both.fdf");
            // output FDF doc_both

            // FDF to XFDF
            // form fields
            System.out.println("Export form field data from FDF to XFDF.");

            doc_fields.saveAsXFDF((output_path + "form1_filled_data.xfdf"));
            // output FDF doc_fields

            // annotations
            System.out.println("Export annotations from FDF to XFDF.");

            doc_annots.saveAsXFDF((output_path + "form1_filled_annot.xfdf"));
            // output FDF doc_annots

            // both form fields and annotations
            System.out.println("Export both form fields and annotations from FDF to XFDF.");

            doc_both.saveAsXFDF((output_path + "form1_filled_both.xfdf"));
            // output FDF doc_both

            System.out.println("Done.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Example 4) Merge/Extract XFDF into/from PDF
        try (PDFDoc in_doc = new PDFDoc((input_path + "numbered.pdf"))) {
            // Merge XFDF from string
            in_doc.initSecurityHandler();

            System.out.println("Merge XFDF string into PDF.");

            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>";

            FDFDoc fdoc = FDFDoc.createFromXFDF(str);
            in_doc.fdfMerge(fdoc);
            in_doc.save(output_path + "numbered_modified.pdf", SDFDoc.SaveMode.LINEARIZED, null);
            // output PDF in_doc
            System.out.println("Merge complete.");

            // Extract XFDF as string
            System.out.println("Extract XFDF as a string.");

            FDFDoc fdoc_new = in_doc.fdfExtract(PDFDoc.e_both);
            String XFDF_str = fdoc_new.saveAsXFDF();
            System.out.println("Extracted XFDF: ");
            System.out.println(XFDF_str);
            System.out.println("Extract complete.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Example 5) Read FDF files directly
        try {
            FDFDoc doc = new FDFDoc((output_path + "form1_filled_data.fdf"));

            for (FDFFieldIterator itr = doc.getFieldIterator(); itr.hasNext(); ) {
                FDFField current = itr.next();
                System.out.println("Field name: " + current.getName());
                System.out.println("Field partial name: " + current.getPartialName());

                System.out.println("------------------------------");
            }
            doc.close();
            System.out.println("Done.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Example 6) Direct generation of FDF.
        try {
            FDFDoc doc = new FDFDoc();
            // Create new fields (i.e. key/value pairs).
            doc.fieldCreate("Company", Field.e_text, "PDFTron Systems");
            doc.fieldCreate("First Name", Field.e_text, "John");
            doc.fieldCreate("Last Name", Field.e_text, "Doe");
            // ...

            // doc.setPdfFileName("mydoc.pdf");

            doc.save(output_path + "sample_output.fdf");
            // output FDF doc
            doc.close();
            System.out.println("Done. Results saved in sample_output.fdf");
        } catch (Exception e) {
            e.printStackTrace();
        }

        PDFNet.terminate();
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
// available in PDFNet.
//---------------------------------------------------------------------------------------

const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {

  exports.runFDFTest = () => {
    const main = async () => {
      const inputPath = '../TestFiles/';
      const outputPath = '../TestFiles/Output/';

      // Example 1)
      // Iterate over all form fields in the document. Display all field names.
      try {
        const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'form1.pdf');
        doc.initSecurityHandler();

        for (const itr = await doc.getFieldIteratorBegin(); await itr.hasNext(); itr.next()) {
          const field = await itr.current();
          console.log('Field name: ' + await field.getName());
          console.log('Field partial name: ' + await field.getPartialName());

          switch (await field.getType()) {
            case PDFNet.Field.Type.e_button:
              console.log('Field type: Button');
              break;
            case PDFNet.Field.Type.e_check:
              console.log('Field type: Check');
              break;
            case PDFNet.Field.Type.e_radio:
              console.log('Field type: Radio');
              break;
            case PDFNet.Field.Type.e_text:
              console.log('Field type: Text');
              break;
            case PDFNet.Field.Type.e_choice:
              console.log('Field type: Choice');
              break;
            case PDFNet.Field.Type.e_signature:
              console.log('Field type: Signature');
              break;
            default:
              console.log('Field type: Null');
              break;
          }
          console.log('------------------------------')
        }
        console.log('Done.');
      } catch (err) {
        console.log(err);
      }

      // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
      try {
        // FDF to PDF
        // form fields
        console.log('Import form field data from XFDF to FDF.');

        const fdf_doc1 = await PDFNet.FDFDoc.createFromXFDF(inputPath + 'form1_data.xfdf');
        await fdf_doc1.save(outputPath + 'form1_data.fdf');

        // annotations
        console.log('Import annotations from XFDF to FDF.');

        const fdf_doc2 = await PDFNet.FDFDoc.createFromXFDF(inputPath + 'form1_annots.xfdf');
        await fdf_doc2.save(outputPath + 'form1_annots.fdf');

        // FDF to PDF
        // form fields
        console.log('Merge form field data from FDF.');

        const doc = await PDFNet.PDFDoc.createFromFilePath(`${inputPath}form1.pdf`);
        doc.initSecurityHandler();
        await doc.fdfMerge(fdf_doc1);

        // Refreshing missing appearances is not required here, but is recommended to make them 
        // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
        doc.refreshAnnotAppearances();

        await doc.save(outputPath + 'form1_filled.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);

        // annotations
        console.log('Merge annotations from FDF.');

        await doc.fdfMerge(fdf_doc2);
        // Refreshing missing appearances is not required here, but is recommended to make them 
        // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
        doc.refreshAnnotAppearances();
        await doc.save(outputPath + 'form1_filled_with_annots.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
        console.log('Done.');
      } catch (err) {
        console.log(err);
      }


      // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
      try {
        // PDF to FDF
        const in_doc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'form1_filled_with_annots.pdf');
        in_doc.initSecurityHandler();

        // form fields only
        console.log('Extract form fields data to FDF.');

        const doc_fields = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_forms_only);
        doc_fields.setPDFFileName('../form1_filled_with_annots.pdf');
        await doc_fields.save(outputPath + 'form1_filled_data.fdf');

        // annotations only
        console.log('Extract annotations to FDF.');

        const doc_annots = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_annots_only);
        doc_annots.setPDFFileName('../form1_filled_with_annots.pdf');
        await doc_annots.save(outputPath + 'form1_filled_annot.fdf');

        // both form fields and annotations
        console.log('Extract both form fields and annotations to FDF.');

        const doc_both = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
        doc_both.setPDFFileName('../form1_filled_with_annots.pdf');
        await doc_both.save(outputPath + 'form1_filled_both.fdf');

        // FDF to XFDF
        // form fields
        console.log('Export form field data from FDF to XFDF.');

        await doc_fields.saveAsXFDF(outputPath + 'form1_filled_data.xfdf');

        // annotations
        console.log('Export annotations from FDF to XFDF.');

        await doc_annots.saveAsXFDF(outputPath + 'form1_filled_annot.xfdf');

        // both form fields and annotations
        console.log('Export both form fields and annotations from FDF to XFDF.');

        await doc_both.saveAsXFDF(outputPath + 'form1_filled_both.xfdf');

        console.log('Done.');
      } catch (err) {
        console.log(err);
      }

      // Example 4) Merge/Extract XFDF into/from PDF
      try {
        // Merge XFDF from string
        const in_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'numbered.pdf');
        in_doc.initSecurityHandler();

        console.log('Merge XFDF string into PDF.');

        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>`;

        const fdoc = await PDFNet.FDFDoc.createFromXFDF(str);
        in_doc.fdfMerge(fdoc);
        await in_doc.save(outputPath + 'numbered_modified.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
        console.log('Merge complete.');

        // Extract XFDF as string
        console.log('Extract XFDF as a string.');

        const fdoc_new = await in_doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
        const XFDF_str = await fdoc_new.saveAsXFDFAsString();
        console.log('Extracted XFDF: ');
        console.log(XFDF_str);
        console.log('Extract complete.');
      } catch (err) {
        console.log(err);
      }

      // Example 5) Read FDF files directly
      try {
        const doc = await PDFNet.FDFDoc.createFromFilePath(outputPath + 'form1_filled_data.fdf');

        for (const itr = await doc.getFieldIteratorBegin(); await itr.hasNext(); itr.next()) {
          const field = await itr.current();
          console.log('Field name: ' + await field.getName());
          console.log('Field partial name: ' + await field.getPartialName());

          console.log('------------------------------');
        }

        console.log('Done.');
      } catch (err) {
        console.log(err);
      }

      // Example 6) Direct generation of FDF.
      try  
      {
        const doc = await PDFNet.FDFDoc.create();
        // Create new fields (i.e. key/value pairs).
        doc.fieldCreateFromString('Company', PDFNet.Field.Type.e_text, 'PDFTron Systems');
        doc.fieldCreateFromString('First Name', PDFNet.Field.Type.e_text, 'John');
        doc.fieldCreateFromString('Last Name', PDFNet.Field.Type.e_text, 'Doe');
    
        await doc.save(outputPath + 'sample_output.fdf');
        console.log('Done. Results saved in sample_output.fdf');
      } catch (err) {
        console.log(err);
      }
    };

    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
  };
  exports.runFDFTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=FDFTest.js
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------

package main
import (
	"fmt"
	. "pdftron"
)

import  "pdftron/Samples/LicenseKey/GO"

//---------------------------------------------------------------------------------------
// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
// available in PDFNet.
//---------------------------------------------------------------------------------------

func main(){
    PDFNetInitialize(PDFTronLicense.Key)
    
    // Relative path to the folder containing the test files.
    inputPath := "../../TestFiles/"
    outputPath := "../../TestFiles/Output/"
    
    // Example 1
    // Iterate over all form fields in the document. Display all field names.
    
    doc := NewPDFDoc(inputPath + "form1.pdf")
    doc.InitSecurityHandler()
    
    itr := doc.GetFieldIterator()
    for itr.HasNext(){
        fmt.Println("Field name: " + itr.Current().GetName())
        fmt.Println("Field partial name: " + itr.Current().GetPartialName())
        
        fieldType := itr.Current().GetType()
		fieldTypeStr := ""
        if fieldType == FieldE_button{
            fieldTypeStr = "Button"
		}else if fieldType == FieldE_text{
            fieldTypeStr = "Text"
		}else if fieldType == FieldE_choice{
            fieldTypeStr = "Choice"
		}else if fieldType == FieldE_signature{
            fieldTypeStr = "Signiture"
        }  
        fmt.Println("Field type: " + fieldTypeStr)
        fmt.Println("------------------------------")
        itr.Next()
    }
    doc.Close()
    fmt.Println("Done.")
    
    // Example 2
    // Import XFDF into FDF, then merge data from FDF
	
	// XFDF to FDF
	// form fields
    fmt.Println("Import form field data from XFDF to FDF.")
	
    fdfDoc1 := FDFDocCreateFromXFDF(inputPath + "form1_data.xfdf")
    fdfDoc1.Save(outputPath + "form1_data.fdf")
	
	// annotations
    fmt.Println("Import annotations from XFDF to FDF.")
	
    fdfDoc2 := FDFDocCreateFromXFDF(inputPath + "form1_annots.xfdf")
    fdfDoc2.Save(outputPath + "form1_annots.fdf")
	
	// FDF to PDF
	// form fields
    fmt.Println("Merge form field data from FDF.")
	
    doc = NewPDFDoc(inputPath + "form1.pdf")
    doc.InitSecurityHandler()
    doc.FDFMerge(fdfDoc1)
	
    // Refreshing missing appearances is not required here, but is recommended to make them 
    // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
    doc.RefreshAnnotAppearances()
	
    doc.Save(outputPath + "form1_filled.pdf", uint(SDFDocE_linearized))
	
	// annotations
    fmt.Println("Merge annotations from FDF.")

    doc.FDFMerge(fdfDoc2)
    // Refreshing missing appearances is not required here, but is recommended to make them 
    // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
    doc.RefreshAnnotAppearances()
    doc.Save(outputPath + "form1_filled_with_annots.pdf", uint(SDFDocE_linearized))
    doc.Close()
    fmt.Println("Done.")
	
    
    // Example 3
    // Extract data from PDF to FDF, then export FDF as XFDF
    
	// PDF to FDF
    inDoc := NewPDFDoc(outputPath + "form1_filled_with_annots.pdf")
    inDoc.InitSecurityHandler()
	
	// form fields only
    fmt.Println("Extract form fields data to FDF.")
	
    docFields := inDoc.FDFExtract(PDFDocE_forms_only)
    docFields.SetPDFFileName("../form1_filled_with_annots.pdf")
    docFields.Save(outputPath + "form1_filled_data.fdf")
	
	// annotations only
    fmt.Println("Extract annotations to FDF.")
	
    docAnnots := inDoc.FDFExtract(PDFDocE_annots_only)
    docAnnots.SetPDFFileName("../form1_filled_with_annots.pdf")
    docAnnots.Save(outputPath + "form1_filled_annot.fdf")
	
	// both form fields and annotations
    fmt.Println("Extract both form fields and annotations to FDF.")
	
    docBoth := inDoc.FDFExtract(PDFDocE_both)
    docBoth.SetPDFFileName("../form1_filled_with_annots.pdf")
    docBoth.Save(outputPath + "form1_filled_both.fdf")
	
	// FDF to XFDF
	// form fields
    fmt.Println("Export form field data from FDF to XFDF.")
	
    docFields.SaveAsXFDF(outputPath + "form1_filled_data.xfdf")
	
	// annotations
    fmt.Println("Export annotations from FDF to XFDF.")
	
    docAnnots.SaveAsXFDF(outputPath + "form1_filled_annot.xfdf")
	
	// both form fields and annotations
    fmt.Println("Export both form fields and annotations from FDF to XFDF.")
	
    docBoth.SaveAsXFDF(outputPath + "form1_filled_both.xfdf")
	
    inDoc.Close()
    fmt.Println("Done.")
	
    // Example 4
    // Merge/Extract XFDF into/from PDF
    
    // Merge XFDF from string
    inDoc = NewPDFDoc(inputPath + "numbered.pdf")
    inDoc.InitSecurityHandler()

    fmt.Println("Merge XFDF string into PDF.")

    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>"

    fdoc := FDFDocCreateFromXFDF(str)
    inDoc.FDFMerge(fdoc)
    inDoc.Save(outputPath + "numbered_modified.pdf", uint(SDFDocE_linearized))
    fmt.Println("Merge complete.")

    // Extract XFDF as string
    fmt.Println("Extract XFDF as a string.")

    fdocNew := inDoc.FDFExtract(PDFDocE_both)
    xfdfStr := fdocNew.SaveAsXFDF()
    fmt.Println("Extracted XFDF: ")
    fmt.Println(xfdfStr)
    inDoc.Close()
    fmt.Println("Extract complete.") 	
	
    // Example 5
    // Read FDF files directly
    
    fdoc2 := NewFDFDoc(outputPath + "form1_filled_data.fdf")
    
    fitr := fdoc2.GetFieldIterator()
    for fitr.HasNext(){
        fmt.Println("Field name: " + fitr.Current().GetName())
        fmt.Println("Field partial name: " + fitr.Current().GetPartialName())
        fmt.Println("------------------------------")
        fitr.Next()
    }
	
    fdoc2.Close()
    fmt.Println("Done.")
    
    // Example 6
    // Direct generation of FDF
    fdoc2 = NewFDFDoc()
    
    // Create new fields (i.r. key/value pairs
    fdoc2.FieldCreate("Company", FieldE_text, "PDFTron Systems")
    fdoc2.FieldCreate("First Name", FieldE_text, "John")
    fdoc2.FieldCreate("Last Name", FieldE_text, "Doe")
    
    fdoc2.Save(outputPath + "sample_output.fdf")
    fdoc2.Close()
    PDFNetTerminate()
    fmt.Println("Done. Results saved in sample_output.fdf")
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

import site
site.addsitedir("../../../PDFNetC/Lib")
import sys
from PDFNetPython import *

sys.path.append("../../LicenseKey/PYTHON")
from LicenseKey import *

#---------------------------------------------------------------------------------------
# PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
# forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
# available in PDFNet.
#---------------------------------------------------------------------------------------

def main():
    PDFNet.Initialize(LicenseKey)
    
    # Relative path to the folder containing the test files.
    input_path = "../../TestFiles/"
    output_path = "../../TestFiles/Output/"
    
    # Example 1
    # Iterate over all form fields in the document. Display all field names.
    
    doc = PDFDoc(input_path + "form1.pdf")
    doc.InitSecurityHandler()
    
    itr = doc.GetFieldIterator()
    while itr.HasNext():
        print("Field name: " + itr.Current().GetName())
        print("Field partial name: " + itr.Current().GetPartialName())
        
        sys.stdout.write("Field type: ")
        type = itr.Current().GetType()
        if type == Field.e_button:
            print("Button")
        elif type == Field.e_check:
            print("Check")
        elif type == Field.e_radio:
            print("Radio")
        elif type == Field.e_text:
            print("Text")
        elif type == Field.e_choice:
            print("Choice")
        elif type == Field.e_signature:
            print("Signiture")
        elif type == Field.e_null:
            print("Null")
            
        print("------------------------------")
        itr.Next()
    
    doc.Close()
    print("Done.")
    
    # Example 2
    # Import XFDF into FDF, then merge data from FDF
	
	# XFDF to FDF
	# form fields
    print("Import form field data from XFDF to FDF.")
	
    fdf_doc1 = FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf")
    fdf_doc1.Save(output_path + "form1_data.fdf")
	
	# annotations
    print("Import annotations from XFDF to FDF.")
	
    fdf_doc2 = FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf")
    fdf_doc2.Save(output_path + "form1_annots.fdf")
	
	# FDF to PDF
	# form fields
    print("Merge form field data from FDF.")
	
    doc = PDFDoc(input_path + "form1.pdf")
    doc.InitSecurityHandler()
    doc.FDFMerge(fdf_doc1)
	
    # Refreshing missing appearances is not required here, but is recommended to make them 
    # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
    doc.RefreshAnnotAppearances()
	
    doc.Save(output_path + "form1_filled.pdf", SDFDoc.e_linearized)
	
	# annotations
    print("Merge annotations from FDF.")

    doc.FDFMerge(fdf_doc2)
    # Refreshing missing appearances is not required here, but is recommended to make them 
    # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
    doc.RefreshAnnotAppearances()
    doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc.e_linearized)
    doc.Close()
    print("Done.")
	
    
    # Example 3
    # Extract data from PDF to FDF, then export FDF as XFDF
    
	# PDF to FDF
    in_doc = PDFDoc(output_path + "form1_filled_with_annots.pdf")
    in_doc.InitSecurityHandler()
	
	# form fields only
    print("Extract form fields data to FDF.")
	
    doc_fields = in_doc.FDFExtract(PDFDoc.e_forms_only)
    doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf")
    doc_fields.Save(output_path + "form1_filled_data.fdf")
	
	# annotations only
    print("Extract annotations to FDF.")
	
    doc_annots = in_doc.FDFExtract(PDFDoc.e_annots_only)
    doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf")
    doc_annots.Save(output_path + "form1_filled_annot.fdf")
	
	# both form fields and annotations
    print("Extract both form fields and annotations to FDF.")
	
    doc_both = in_doc.FDFExtract(PDFDoc.e_both)
    doc_both.SetPDFFileName("../form1_filled_with_annots.pdf")
    doc_both.Save(output_path + "form1_filled_both.fdf")
	
	# FDF to XFDF
	# form fields
    print("Export form field data from FDF to XFDF.")
	
    doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")
	
	# annotations
    print("Export annotations from FDF to XFDF.")
	
    doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")
	
	# both form fields and annotations
    print("Export both form fields and annotations from FDF to XFDF.")
	
    doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
	
    in_doc.Close()
    print("Done.")
	
    # Example 4
    # Merge/Extract XFDF into/from PDF
    
    # Merge XFDF from string
    in_doc = PDFDoc(input_path + "numbered.pdf")
    in_doc.InitSecurityHandler()

    print("Merge XFDF string into PDF.")

    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>"

    fdoc = FDFDoc.CreateFromXFDF(str)
    in_doc.FDFMerge(fdoc)
    in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc.e_linearized)
    print("Merge complete.")

    # Extract XFDF as string
    print("Extract XFDF as a string.")

    fdoc_new = in_doc.FDFExtract(PDFDoc.e_both)
    XFDF_str = fdoc_new.SaveAsXFDF()
    print("Extracted XFDF: ")
    print(XFDF_str)
    in_doc.Close()
    print("Extract complete.") 	
	
    # Example 5
    # Read FDF files directly
    
    doc = FDFDoc(output_path + "form1_filled_data.fdf")
    
    itr = doc.GetFieldIterator()
    while itr.HasNext():
        print("Field name: " + itr.Current().GetName())
        print("Field partial name: " + itr.Current().GetPartialName())
        print("------------------------------")
        itr.Next()
        
    doc.Close()
    print("Done.")
    
    # Example 6
    # Direct generation of FDF
    doc = FDFDoc()
    
    # Create new fields (i.r. key/value pairs
    doc.FieldCreate("Company", Field.e_text, "PDFTron Systems")
    doc.FieldCreate("First Name", Field.e_text, "John")
    doc.FieldCreate("Last Name", Field.e_text, "Doe")
    
    doc.Save(output_path + "sample_output.fdf")
    doc.Close()
    PDFNet.Terminate()
    print("Done. Results saved in sample_output.fdf")

if __name__ == '__main__':
    main()
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
<?php
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------
if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
include("../../../PDFNetC/Lib/PDFNetPHP.php");
include("../../LicenseKey/PHP/LicenseKey.php");

//---------------------------------------------------------------------------------------
// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
// available in PDFNet.
//---------------------------------------------------------------------------------------
	PDFNet::Initialize($LicenseKey);
	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.
	
	// Relative path to the folder containing the test files.
	$input_path = getcwd()."/../../TestFiles/";
	$output_path = $input_path."Output/";

	// Example 1: 
	// Iterate over all form fields in the document. Display all field names.

	$doc = new PDFDoc($input_path."form1.pdf");
	$doc->InitSecurityHandler();

	for($itr = $doc->GetFieldIterator(); $itr->HasNext(); $itr->Next()) 
	{
		echo nl2br("Field name: ".$itr->Current()->GetName()."\n");
		echo nl2br("Field partial name: ".$itr->Current()->GetPartialName()."\n");

		echo "Field type: ";
		$type = $itr->Current()->GetType();
		switch($type)
		{
		case Field::e_button: echo nl2br("Button"."\n"); break;
		case Field::e_check: echo nl2br("Check"."\n"); break;
		case Field::e_radio: echo nl2br("Radio"."\n"); break;
		case Field::e_text: echo nl2br("Text"."\n"); break;
		case Field::e_choice: echo nl2br("Choice"."\n"); break;
		case Field::e_signature: echo nl2br("Signature"."\n"); break;
		case Field::e_null: echo nl2br("Null"."\n"); break;
		}

		echo nl2br("------------------------------\n");
	}
	
	$doc->Close();
	echo nl2br("Done.\n");

	// Example 2) Import XFDF into FDF, then merge data from FDF into PDF
	
	// XFDF to FDF
	// form fields
	echo nl2br("Import form field data from XFDF to FDF.\n");
	
	$fdf_doc1 = FDFDoc::CreateFromXFDF($input_path."form1_data.xfdf");
	$fdf_doc1->Save($output_path."form1_data.fdf");
	
	// annotations
	echo nl2br("Import annotations from XFDF to FDF.\n");
	
	$fdf_doc2 = FDFDoc::CreateFromXFDF($input_path."form1_annots.xfdf");
	$fdf_doc2->Save($output_path."form1_annots.fdf");	
	
	// FDF to PDF
	// form fields
	echo nl2br("Merge form field data from FDF.\n");
	
	$doc = new PDFDoc($input_path."form1.pdf");
	$doc->InitSecurityHandler();
	$doc->FDFMerge($fdf_doc1);
	
	// Refreshing missing appearances is not required here, but is recommended to make them 
	// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
	$doc->RefreshAnnotAppearances();
	
	$doc->Save(($output_path."form1_filled.pdf"), SDFDoc::e_linearized);
	
	// annotations
	echo nl2br("Merge annotations from FDF.\n");
	
	$doc->FDFMerge($fdf_doc2);	
	// Refreshing missing appearances is not required here, but is recommended to make them 
	// visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
	$doc->RefreshAnnotAppearances();
	$doc->Save(($output_path."form1_filled_with_annots.pdf"), SDFDoc::e_linearized);
	$doc->Close();
	echo nl2br("Done.\n");


	// Example 3) Extract data from PDF to FDF, then export FDF as XFDF
	
	// PDF to FDF
	$in_doc = new PDFDoc($output_path."form1_filled_with_annots.pdf");
	$in_doc->InitSecurityHandler();
	
	// form fields only
	echo nl2br("Extract form fields data to FDF.\n");
	
	$doc_fields = $in_doc->FDFExtract(PDFDoc::e_forms_only);
	$doc_fields->SetPDFFileName("../form1_filled_with_annots.pdf");
	$doc_fields->Save($output_path."form1_filled_data.fdf");
	
	// annotations only
	echo nl2br("Extract annotations to FDF.\n");
	
	$doc_annots = $in_doc->FDFExtract(PDFDoc::e_annots_only);
	$doc_annots->SetPDFFileName("../form1_filled_with_annots.pdf");
	$doc_annots->Save($output_path."form1_filled_annot.fdf");
	
	// both form fields and annotations
	echo nl2br("Extract both form fields and annotations to FDF.\n");
	
	$doc_both = $in_doc->FDFExtract(PDFDoc::e_both);
	$doc_both->SetPDFFileName("../form1_filled_with_annots.pdf");
	$doc_both->Save($output_path."form1_filled_both.fdf");
	
	// FDF to XFDF
	// form fields
	echo nl2br("Export form field data from FDF to XFDF.\n");
	
	$doc_fields->SaveAsXFDF($output_path."form1_filled_data.xfdf");
	
	// annotations
	echo nl2br("Export annotations from FDF to XFDF.\n");
	
	$doc_annots->SaveAsXFDF($output_path."form1_filled_annot.xfdf");
	
	// both form fields and annotations
	echo nl2br("Export both form fields and annotations from FDF to XFDF.\n");
	
	$doc_both->SaveAsXFDF($output_path."form1_filled_both.xfdf");
	
	$in_doc->Close();
	echo nl2br("Done.\n");

	// Example 4) Merge/Extract XFDF into/from PDF
	
	// Merge XFDF from string
	$in_doc = new PDFDoc($input_path."numbered.pdf");
	$in_doc->InitSecurityHandler();
	
	echo nl2br("Merge XFDF string into PDF.\n");
	
	$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>";
	$fdoc = FDFDoc::CreateFromXFDF($str);
	$in_doc->FDFMerge($fdoc);
	$in_doc->Save(($output_path."numbered_modified.pdf"), SDFDoc::e_linearized);
	echo nl2br("Merge complete.\n");
	
	// Extract XFDF as string
	echo nl2br("Extract XFDF as a string.\n");
	
	$fdoc_new = $in_doc->FDFExtract(PDFDoc::e_both);
	$XFDF_str = $fdoc_new->SaveAsXFDF();
	echo nl2br("Extracted XFDF: \n");
	echo nl2br($XFDF_str);
	$in_doc->Close();
	echo nl2br("\nExtract complete.\n");
	
	// Example 5) Read FDF files directly
	
	$doc = new FDFDoc($output_path."form1_filled_data.fdf");

	for($itr = $doc->GetFieldIterator(); $itr->HasNext(); $itr->Next())
	{
		echo nl2br("Field name: ".$itr->Current()->GetName()."\n");
		echo nl2br("Field partial name: ".$itr->Current()->GetPartialName()."\n");
		echo nl2br("------------------------------\n");
	}
	
	$doc->Close();
	echo nl2br("Done.\n");

	// Example 6) Direct generation of FDF.
	
	$doc = new FDFDoc();
	// Create new fields (i.e. key/value pairs).
	$doc->FieldCreate("Company", Field::e_text, "PDFTron Systems");
	$doc->FieldCreate("First Name", Field::e_text, "John");
	$doc->FieldCreate("Last Name", Field::e_text, "Doe");
	// ...		

	// $doc->SetPdfFileName("mydoc.pdf");
	
	$doc->Save($output_path."sample_output.fdf");
	$doc->Close();
	PDFNet::Terminate();
	echo nl2br("Done. Results saved in sample_output.fdf");
	
	
?>
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

require '../../../PDFNetC/Lib/PDFNetRuby'
include PDFNetRuby
require '../../LicenseKey/RUBY/LicenseKey'

$stdout.sync = true

#---------------------------------------------------------------------------------------
# PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract 
# forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
# available in PDFNet.
#---------------------------------------------------------------------------------------

	PDFNet.Initialize(PDFTronLicense.Key)
	
	# Relative path to the folder containing the test files.
	input_path = "../../TestFiles/"
	output_path = "../../TestFiles/Output/"
	
	# Example 1
	# Iterate over all form fields in the document. Display all field names.
	
	doc = PDFDoc.new(input_path + "form1.pdf")
	doc.InitSecurityHandler()
	
	itr = doc.GetFieldIterator()
	while itr.HasNext() do
		puts "Field name: " + itr.Current().GetName()
		puts "Field partial name: " + itr.Current().GetPartialName()
		
		print "Field type: "
		type = itr.Current().GetType()
		if type == Field::E_button
			puts "Button"
		elsif type == Field::E_check
			puts "Check"
		elsif type == Field::E_radio
			puts "Radio"
		elsif type == Field::E_text
			puts "Text"
		elsif type == Field::E_choice
			puts "Choice"
		elsif type == Field::E_signature
			puts "Signiture"
		elsif type == Field::E_null
			puts "Null"
		end
			
		puts "------------------------------"
		itr.Next()
	end
	
	doc.Close()
	puts "Done."
	
	# Example 2
	# Import XFDF into FDF, then merge data from FDF into PDF
	
	# XFDF to FDF
	# form fields
	puts "Import form field data from XFDF to FDF."
	
	fdf_doc1 = FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf")
	fdf_doc1.Save(output_path + "form1_data.fdf")
	
	# annotations
	puts "Import annotations from XFDF to FDF."
	
	fdf_doc2 = FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf")
	fdf_doc2.Save(output_path + "form1_annots.fdf")
	
	# FDF to PDF
	# form fields
	puts "Merge form field data from FDF."
	
	doc = PDFDoc.new(input_path + "form1.pdf")
	doc.InitSecurityHandler()
	doc.FDFMerge(fdf_doc1)
	
	# Refreshing missing appearances is not required here, but is recommended to make them  
	# visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
	doc.RefreshAnnotAppearances()
	
	doc.Save(output_path + "form1_filled.pdf", SDFDoc::E_linearized)
	
	# annotations
	puts "Merge annotations from FDF."
	
	doc.FDFMerge(fdf_doc2)
	# Refreshing missing appearances is not required here, but is recommended to make them  
	# visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
	doc.RefreshAnnotAppearances()
	doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc::E_linearized)
	doc.Close()
	puts "Done."
	
	
	# Example 3
	# Extract data from PDF to FDF, then export FDF as XFDF
	
	# PDF to FDF
	in_doc = PDFDoc.new(output_path + "form1_filled_with_annots.pdf")
	in_doc.InitSecurityHandler()
	
	# form fields only
	puts "Extract form fields data to FDF."
	
	doc_fields = in_doc.FDFExtract(PDFDoc::E_forms_only)
	doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf")
	doc_fields.Save(output_path + "form1_filled_data.fdf")
	
	# annotations only
	puts "Extract annotations to FDF."
	
	doc_annots = in_doc.FDFExtract(PDFDoc::E_annots_only)
	doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf")
	doc_annots.Save(output_path + "form1_filled_annot.fdf")
	
	# both form fields and annotations
	puts "Extract both form fields and annotations to FDF."
	
	doc_both = in_doc.FDFExtract(PDFDoc::E_both)
	doc_both.SetPDFFileName("../form1_filled_with_annots.pdf")
	doc_both.Save(output_path + "form1_filled_both.fdf")
	
	# FDF to XFDF
	# form fields
	puts "Export form field data from FDF to XFDF."
	
	doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")
	
	# annotations
	puts "Export annotations from FDF to XFDF."
	
	doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")
	
	# both form fields and annotations
	puts "Export both form fields and annotations from FDF to XFDF."
	
	doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
	
	in_doc.Close()
	puts "Done."

	# Example 4
	# Merge/Extract XFDF into/from PDF
	in_doc = PDFDoc.new(input_path + "numbered.pdf")
	in_doc.InitSecurityHandler()
	
	puts "Merge XFDF string into PDF."
	
	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>"
	
	fdoc = FDFDoc.CreateFromXFDF(str)
	in_doc.FDFMerge(fdoc)
	in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc::E_linearized)
	puts "Merge complete."
	
	# Extract XFDF as string
	puts "Extract XFDF as a string."
	
	fdoc_new = in_doc.FDFExtract(PDFDoc::E_both)
	XFDF_str = fdoc_new.SaveAsXFDF()
	puts "Extracted XFDF: "
	puts XFDF_str
	in_doc.Close()
	puts "Extract complete."
	
	# Example 5
	# Read FDF files directly
	
	doc = FDFDoc.new(output_path + "form1_filled_data.fdf")
	
	itr = doc.GetFieldIterator()
	while itr.HasNext() do
		puts "Field name: " + itr.Current().GetName()
		puts "Field partial name: " + itr.Current().GetPartialName()
		puts "------------------------------"
		itr.Next()
	end
		
	doc.Close()
	puts "Done."
	
	# Example 6
	# Direct generation of FDF
	doc = FDFDoc.new()
	
	# Create new fields (i.r. key/value pairs
	doc.FieldCreate("Company", Field::E_text, "PDFTron Systems")
	doc.FieldCreate("First Name", Field::E_text, "John")
	doc.FieldCreate("Last Name", Field::E_text, "Doe")
	
	doc.Save(output_path + "sample_output.fdf")
	doc.Close()
	PDFNet.Terminate
	puts "Done. Results saved in sample_output.fdf"
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
'---------------------------------------------------------------------------------------
' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
' Consult legal.txt regarding legal and license information.
'---------------------------------------------------------------------------------------
Imports System

Imports pdftron
Imports pdftron.Common
Imports pdftron.SDF
Imports pdftron.FDF
Imports pdftron.PDF

Module FDFTestVB
	Dim pdfNetLoader As PDFNetLoader
	Sub New()
		pdfNetLoader = pdftron.PDFNetLoader.Instance()
	End Sub

	'---------------------------------------------------------------------------------------
	' PDFNet includes full support for FDF (Forms Data Format) and for merging/extracting
	' forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality 
	' available in PDFNet.
	'---------------------------------------------------------------------------------------
	Sub Main()

		PDFNet.Initialize(PDFTronLicense.Key)

		' Relative path to the folder containing test files.
		Dim input_path As String = "../../../../TestFiles/"
		Dim output_path As String = "../../../../TestFiles/Output/"

		' Example 1)
		' Iterate over all form fields in the document. Display all field names.
		Try
			Using doc As PDFDoc = New PDFDoc(input_path + "form1.pdf")
				doc.InitSecurityHandler()

				Dim itr As FieldIterator = doc.GetFieldIterator()
				While itr.HasNext()
					Console.WriteLine("Field name: {0:s}", itr.Current().GetName())
					Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName())

					Console.Write("Field type: ")
					Dim type As Field.Type = itr.Current().GetType()
					If type = Field.Type.e_button Then
						Console.WriteLine("Button")
					ElseIf type = Field.Type.e_check Then
						Console.WriteLine("Check")
					ElseIf type = Field.Type.e_radio Then
						Console.WriteLine("Radio")
					ElseIf type = Field.Type.e_text Then
						Console.WriteLine("Text")
					ElseIf type = Field.Type.e_choice Then
						Console.WriteLine("Choice")
					ElseIf type = Field.Type.e_signature Then
						Console.WriteLine("Signature")
					ElseIf type = Field.Type.e_null Then
						Console.WriteLine("Null")
					End If
					Console.WriteLine("------------------------------")
					itr.Next()
				End While
			End Using
			Console.WriteLine("Done.")
		Catch e As Exception
			Console.WriteLine("Exception caught:\n{0}", e)
		End Try

		' Example 2) Import XFDF into FDF, then merge data from FDF into PDF
		Try
			' XFDF to FDF
			' form fields
			Console.WriteLine("Import form field data from XFDF to FDF.")
			
			Dim fdf_doc1 As FDFDoc = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf"))
			fdf_doc1.Save(output_path + "form1_data.fdf")
			
			' annotations
			Console.WriteLine("Import annotations from XFDF to FDF.")
			
			Dim fdf_doc2 As FDFDoc = new FDFDoc(FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf"))
			fdf_doc2.Save(output_path + "form1_annots.fdf")
			
			' FDF to PDF
			' form fields
			Console.WriteLine("Merge form field data from FDF.")
			
			Using doc As PDFDoc = New PDFDoc(input_path + "form1.pdf")
				doc.InitSecurityHandler()
				doc.FDFMerge(fdf_doc1)

				' Refreshing missing appearances is not required here, but is recommended to make them  
				' visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
				doc.RefreshAnnotAppearances()

				doc.Save(output_path + "form1_filled.pdf", SDF.SDFDoc.SaveOptions.e_linearized)

				' annotations
				Console.WriteLine("Merge annotations from FDF.")

				doc.FDFMerge(fdf_doc2)
				' Refreshing missing appearances is not required here, but is recommended to make them  
				' visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
				doc.RefreshAnnotAppearances()
				doc.Save(output_path + "form1_filled_with_annots.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
			End Using

			Console.WriteLine("Done.")
		Catch e As Exception
			Console.WriteLine("Exception caught:\n{0}", e)
		End Try

		' Example 3) Extract data from PDF to FDF, then export FDF as XFDF
		Try
			' PDF to FDF
			Using in_doc As PDFDoc = New PDFDoc(output_path + "form1_filled_with_annots.pdf")
				in_doc.InitSecurityHandler()

				' form fields only
				Console.WriteLine("Extract form fields data to FDF.")

				Dim doc_fields As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_forms_only)
				doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf")
				doc_fields.Save(output_path + "form1_filled_data.fdf")

				' annotations only
				Console.WriteLine("Extract annotations to FDF.")

				Dim doc_annots As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_annots_only)
				doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf")
				doc_annots.Save(output_path + "form1_filled_annot.fdf")

				' both form fields and annotations
				Console.WriteLine("Extract both form fields and annotations to FDF.")

				Dim doc_both As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_both)
				doc_both.SetPdfFileName("../form1_filled_with_annots.pdf")
				doc_both.Save(output_path + "form1_filled_both.fdf")

				' FDF to XFDF
				' form fields
				Console.WriteLine("Export form field data from FDF to XFDF.")

				doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")

				' annotations
				Console.WriteLine("Export annotations from FDF to XFDF.")

				doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")

				' both form fields and annotations
				Console.WriteLine("Export both form fields and annotations from FDF to XFDF.")

				doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
			End Using
			Console.WriteLine("Done.")
		Catch e As Exception
			Console.WriteLine("Exception caught:\n{0}", e)
		End Try

		' Example 4) Merge/Extract XFDF into/from PDF
		Try
			' Merge XFDF from string
			Using in_doc As PDFDoc = New PDFDoc(input_path + "numbered.pdf")
				in_doc.InitSecurityHandler()

				Console.WriteLine("Merge XFDF string into PDF.")
				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>"

				Dim fdoc As FDFDoc = New FDFDoc(FDFDoc.CreateFromXFDF(str))
				in_doc.FDFMerge(fdoc)
				in_doc.Save(output_path + "numbered_modified.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
				Console.WriteLine("Merge complete.")

				' Extract XFDF as string
				Console.WriteLine("Extract XFDF as a string.")
				Dim fdoc_new As FDFDoc = in_doc.FDFExtract(PDF.PDFDoc.ExtractFlag.e_both)
				Dim XFDF_str As String = fdoc_new.SaveAsXFDF()
				Console.WriteLine("Extracted XFDF: ")
				Console.WriteLine(XFDF_str)
			End Using
			Console.WriteLine("Extract complete.")

		Catch e As Exception
			Console.WriteLine("Exception caught:\n{0}", e)
		End Try

		' Example 5) Read FDF files directly
		Try
			Dim doc As FDFDoc = New FDFDoc(output_path + "form1_filled_data.fdf")

			Dim itr As FDFFieldIterator = doc.GetFieldIterator()
			While itr.HasNext()
				Console.WriteLine("Field name: {0:s}", itr.Current().GetName())
				Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName())
				Console.WriteLine("------------------------------")
				itr.Next()
			End While

			Console.WriteLine("Done.")
		Catch e As Exception
			Console.WriteLine("Exception caught:\n{0}", e)
		End Try

		' Example 6) Direct generation of FDF.
		Try
			Dim doc As FDFDoc = New FDFDoc

			' Create new fields (i.e. key/value pairs).
			doc.FieldCreate("Company", Int(Field.Type.e_text), "PDFTron Systems")
			doc.FieldCreate("First Name", Int(Field.Type.e_text), "John")
			doc.FieldCreate("Last Name", Int(Field.Type.e_text), "Doe")
			' ...		

			' doc.SetPdfFileName("mydoc.pdf");
			doc.Save(output_path + "sample_output.fdf")
			Console.WriteLine("Done. Results saved in sample_output.fdf")
		Catch e As Exception
			Console.WriteLine("Exception caught:\n{0}", e)
		End Try
		PDFNet.Terminate()
	End Sub
End Module
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/get-started/samples/fdftest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
