C#: Import and Export PDF Form Data

Importing and exporting PDF form data allows users to seamlessly exchange form information with external files in formats such as FDF (Forms Data Format), XFDF (XML Forms Data Format), or XML. The import function enables quick population of PDF forms using data from external sources, while the export function extracts data from PDF forms and saves it to external files. This capability simplifies data management, making it especially valuable for processing large volumes of form data or integrating with other systems. In this article, we will demonstrate how to import PDF form data from FDF, XFDF, or XML files, or export PDF form data to FDF, XFDF, or XML files in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Import PDF Form Data from FDF, XFDF or XML Files in C#

Spire.PDF for .NET offers the PdfFormWidget.ImportData() method for importing PDF form data from FDF, XFDF, or XML files. The detailed steps are as follows.

  • Create an object of the PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the form of the PDF document using PdfDocument.Form property.
  • Import form data from an FDF, XFDF or XML file using PdfFormWidget.ImportData() method.
  • Save the resulting document using PdfDocument.SaveToFile() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Widget;

namespace ImportPdfFormData
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of the PdfDocument class
            PdfDocument document = new PdfDocument();
            //Load a PDF document
            document.LoadFromFile("Forms.pdf");

            //Get the form of the PDF document 
            PdfFormWidget loadedForm = document.Form as PdfFormWidget;
             
            //Import PDF form data from an XML file
            loadedForm.ImportData("Data.xml", DataFormat.Xml);

            //Import PDF form data from an FDF file
            //loadedForm.ImportData("Data.fdf", DataFormat.Fdf);

            //Import PDF form data from an XFDF file
            //loadedForm.ImportData("Data.xfdf", DataFormat.XFdf);

            //Save the resulting document
            document.SaveToFile("Output.pdf");
            //Close the PdfDocument object
            document.Close();
        }
    }
}

C#: Import and Export PDF Form Data

Export PDF Form Data to FDF, XFDF or XML Files in C#

Spire.PDF for .NET also enables you to export PDF form data to FDF, XFDF, or XML files by using the PdfFormWidget.ExportData() method. The detailed steps are as follows.

  • Create an object of the PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the form of the PDF document using PdfDocument.Form property.
  • Export form data to an FDF, XFDF or XML file using PdfFormWidget.ExportData() method.
  • C#
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Widget;

namespace ExportPdfFormData
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of the PdfDocument class
            PdfDocument document = new PdfDocument();
            //Load a PDF document
            document.LoadFromFile("Forms.pdf");

            //Get the form of the PDF document 
            PdfFormWidget loadedForm = document.Form as PdfFormWidget;

            //Export PDF form data to an XML file
            loadedForm.ExportData("Data.xml", DataFormat.Xml, "Form");

            //Export PDF form data to an FDF file
            //loadedForm.ExportData("Data.fdf", DataFormat.Fdf, "Form");

            //Export PDF form data to an XFDF file
            //loadedForm.ExportData("Data.xfdf", DataFormat.XFdf, "Form");

            //Close the PdfDocument object
            document.Close();
        }
    }
}

C#: Import and Export PDF Form Data

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.