> 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/uwp/get-started/samples/fdftest.md).

# FDF

Sample code for using Apryse UWP 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 Fo

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

Learn more about our full [PDF Data Extraction SDK Capabilities](https://apryse.com/capabilities/extraction).

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

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

```csharp
//
// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
//

using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Foundation;

using pdftron.FDF;
using pdftron.PDF;
using pdftron.SDF;

using PDFNetUniversalSamples.ViewModels;

namespace PDFNetSamples
{
    public sealed class FDFTest : Sample
    {
        public FDFTest() :
            base("FDF", "PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract forms data (FDF) with/from PDF. The sample illustrates basic FDF merge/extract functionality available in PDFNet.")
        {
        }

        public override IAsyncAction RunAsync()
        {
            return Task.Run(new System.Action(async () => {
                WriteLine("--------------------------------");
                WriteLine("Starting FDF Test...");
                WriteLine("--------------------------------\n");
			    // Example 1)
			    // Iterate over all form fields in the document. Display all field names.
			    try  
			    {
                    String input_file_path = Path.Combine(InputPath, "form1.pdf");
                    PDFDoc doc = new PDFDoc(input_file_path);
				    doc.InitSecurityHandler();
				
				    FieldIterator itr;
				    for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
				    {
                        WriteLine(String.Format("Field name: {0:s}", itr.Current().GetName()));
					    WriteLine(String.Format("Field partial name: {0:s}", itr.Current().GetPartialName()));

					    Write("Field type: ");
					    FieldType type = itr.Current().GetType();
					    switch(type)
					    {
						    case FieldType.e_button: 
							    WriteLine("Button"); break;
						    case FieldType.e_text: 
							    WriteLine("Text"); break;
						    case FieldType.e_choice: 
							    WriteLine("Choice"); break;
						    case FieldType.e_signature: 
							    WriteLine("Signature"); break;
					    }

					    WriteLine("\n------------------------------");
				    }

				    doc.Destroy();
				    WriteLine("Done.");
			    }
			    catch (Exception e)
			    {
				    WriteLine(GetExceptionMessage(e));
			    }

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

                    String input_file_path = Path.Combine(InputPath, "form1_data.xfdf");
                    String output_file_path = Path.Combine(OutputPath, "form1_data.fdf");
                    FDFDoc fdf_doc1 = await FDFDoc.CreateFromXFDFAsync(input_file_path);
                    await fdf_doc1.SaveAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    // annotations
                    WriteLine("Import annotations from XFDF to FDF.");

                    input_file_path = Path.Combine(InputPath, "form1_annots.xfdf");
                    output_file_path = Path.Combine(OutputPath, "form1_annots.fdf");
                    FDFDoc fdf_doc2 = await FDFDoc.CreateFromXFDFAsync(input_file_path);
                    await fdf_doc2.SaveAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    // FDF to PDF
                    // form fields
                    WriteLine("Merge form field data from FDF.");
                    input_file_path = Path.Combine(InputPath, "form1.pdf");
                    PDFDoc doc = new PDFDoc(input_file_path);
				    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();

                    output_file_path = Path.Combine(OutputPath, "form1_filled.pdf");
                    await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    //annotations
                    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();

                    output_file_path = Path.Combine(OutputPath, "form1_filled_with_annots.pdf");
                    await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
				    doc.Destroy();
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

				    WriteLine("Done.");
			    }
			    catch (Exception e)
			    {
				    WriteLine(GetExceptionMessage(e));
			    }

                // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
			    try  
			    {
                    // PDF to FDF
                    String output_file_path = Path.Combine(OutputPath, "form1_filled_with_annots.pdf");
                    PDFDoc in_doc = new PDFDoc(output_file_path);
                    in_doc.InitSecurityHandler();

                    // form fields only
				    WriteLine("Extract form fields data to FDF.");

                    FDFDoc doc_fields = in_doc.FDFExtract(PDFDocExtractFlag.e_forms_only);
                    doc_fields.SetPdfFileName("../form1_filled_with_annots.pdf");
                    output_file_path = Path.Combine(OutputPath, "form1_filled_data.fdf");
                    await doc_fields.SaveAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    // annotations only
				    WriteLine("Extract annotations to FDF.");

                    FDFDoc doc_annots = in_doc.FDFExtract(PDFDocExtractFlag.e_annots_only);
                    doc_annots.SetPdfFileName("../form1_filled_with_annots.pdf");
                    output_file_path = Path.Combine(OutputPath, "form1_filled_annot.fdf");
                    await doc_annots.SaveAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    // both form fields and annotations
                    WriteLine("Extract both form fields and annotations to FDF.");

                    FDFDoc doc_both = in_doc.FDFExtract(PDFDocExtractFlag.e_both);
                    doc_both.SetPdfFileName("../form1_filled_with_annots.pdf");
                    output_file_path = Path.Combine(OutputPath, "form1_filled_both.fdf");
                    await doc_both.SaveAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    // FDF to XFDF
                    // form fields
                    WriteLine("Export form field data from FDF to XFDF.");
                    
                    output_file_path = Path.Combine(OutputPath, "form1_filled_data.xfdf");
                    await doc_fields.SaveAsXFDFAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    // annotations
                    WriteLine("Export annotations from FDF to XFDF.");
                    
                    output_file_path = Path.Combine(OutputPath, "form1_filled_annot.xfdf");
                    await doc_annots.SaveAsXFDFAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

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

                    output_file_path = Path.Combine(OutputPath, "form1_filled_both.xfdf");
                    await doc_both.SaveAsXFDFAsync(output_file_path);
                    WriteLine("Done. Result saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path).ConfigureAwait(false);

                    in_doc.Destroy();
                    WriteLine("Done.");
			    }
			    catch (Exception e)
			    {
                    WriteLine(GetExceptionMessage(e));
			    }

			    // Example 4) Read FDF files directly
			    try  
			    {
                    String output_file_path = Path.Combine(OutputPath, "form1_filled_data.fdf");
				    FDFDoc doc = new FDFDoc(output_file_path);
				    FDFFieldIterator itr = doc.GetFieldIterator();
				    for(; itr.HasNext(); itr.Next()) 
				    {
                        WriteLine(String.Format("Field name: {0:s}", itr.Current().GetName()));
                        WriteLine(String.Format("Field partial name: {0:s}", itr.Current().GetPartialName()));
                        WriteLine("------------------------------");
				    }
                    
                    WriteLine("Done.");
			    }
			    catch (Exception e)
			    {
                    WriteLine(GetExceptionMessage(e));
			    }

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

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

				    // doc.SetPdfFileName("mydoc.pdf");
                    String output_file_path = Path.Combine(OutputPath, "fdf_sample_output.fdf");
				    await doc.SaveAsync(output_file_path);
                    WriteLine("Done. Results saved in " + output_file_path);
                    await AddFileToOutputList(output_file_path);
			    }
			    catch (Exception e)
			    {
                    WriteLine(GetExceptionMessage(e));
			    }

                WriteLine("\n--------------------------------");
                WriteLine("Done FDF Test.");
                WriteLine("--------------------------------\n");
            })).AsAsyncAction();
		}
	}
}
```

{% 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/uwp/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.
