Skip to main content Skip to footer

Import and Export PDF Form Data in .NET Core

Data plays a very crucial role in any kind of application, and forms are a great way of collecting data. Nowadays PDF forms are being used extensively because they are easier to use in order to design, distribute, and capture data than either web or print forms.

GrapeCity Documents for PDF (GcPdf) not only allows the creation of interactive PDF forms/AcroForm but also allows you to export/import the form data to FDF, XFDF, and XML formats.

Let's first try to get an understanding of these file formats.

What are FDF/XFDF/XML formats?

FDF: FDF stands for Forms Data Format. An FDF file contains values of the form fields in key/value fashion. This is how a form field named 'empName' with a value 'Jaime Smith' is represented in an FDF file:

<< 
/T (empName)
/V (Jaime Smith)
>> 

XFDF: XFDF stands for XML Forms Data Format. It is an XML-encoded FDF that stores the values of form fields in a hierarchical manner using XML tags. Each field tag has an attribute 'name' containing the field name and a sub tag, with the value representing the field value. This is how a form field named 'empName' with a value 'Jaime Smith' is represented in an XFDF file:

<field name="empName">
      <value>Jaime Smith</value>
</field>

XML: XML stands for Extensible Markup Language. XML data is self describing, meaning that the structure of the data is embedded with the data. This is how a form field named 'empName' with a value 'Jaime Smith' is represented in an XML file:

<empName>Jaime Smith</empName>

Advantages of using FDF/XFDF/XML Formats

  • The exported files are much smaller than the original PDF form file, making it much more convenient for distributing to others.
  • Once you have an FDF/XFDF/XML file containing the form data, you can let another application parse it and populate a database with the information.
  • You can export the form data to standalone files that can be stored, transmitted electronically, and imported back into the corresponding interactive form. (The form fields in the PDF file into which the form data is being imported must directly correspond to the form fields in the FDF/XFDF/XML file.)

Let's now see how you can import and export the PDF form data using GcPdf.

Exporting Form Data

For exporting the PDF form data to FDF, XFDF, and XML formats, you can make use of the ExportFormDataToFDF, ExportFormDataToXFDF, and ExportFormDataToXML methods of the GcPdfDocument class. The form data can be exported to a file or a stream for all the three formats.

Image1

The code snippet provided below shows how you can export the form data from the Employee Time Sheet Form shown in the above image to FDF, XFDF, and XML formats. The exported file is small and easy to incorporate into the original PDF and can be used to accurately track the number of hours an employee has worked.

var doc = new GcPdfDocument();
doc.Load(new FileStream(Path.Combine("TimeSheetForm.pdf"), FileMode.Open, FileAccess.Read)); //Load the document          
 doc.ExportFormDataToFDF("FormData_FDF.fdf"); //Export the form data to FDF file
doc.ExportFormDataToXFDF("FormData_XFDF.xfdf"); //Export the form data to XFDF file
doc.ExportFormDataToXML("FormData_XML.xml"); //Export the form data to XML file

Importing Form Data

To import the PDF form data from FDF, XFDF, and XML formats to a PDF form, you can take advantage of the ImportFormDataFromFDF, ImportFormDataFromXFDF, and ImportFormDataFromXML methods of the GcPdfDocument class. These methods accept FDF/XFDF/XML data stored in a stream as a parameter.

Image2

The code snippet provided below shows how you can import the form data stored in FDF format to the employee time sheet PDF form (as shown in the above image).

var doc = new GcPdfDocument();
doc.Load(new FileStream(Path.Combine("TimeSheetForm_Blank.pdf"), FileMode.Open, FileAccess.Read)); //Load the document         
FileStream stream = new FileStream(Path.Combine("FormData_FDF.fdf"), FileMode.Open, FileAccess.Read); //Open the FDF file
doc.ImportFormDataFromFDF(stream); //Import the form data       
doc.Save("PdfForm_FDF.pdf"); //Save the document

Similarly, you can import the data from XFDF and XML files using the appropriate methods provided by GcPdfDocument class.

Download the sample

Let us know the scenarios where you would use this feature. Leave a comment below. Thanks and happy coding!

Palak Bansal

Associate Software Engineer
comments powered by Disqus