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.
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:
Let's now see how you can import and export the PDF form data using GcPdf.
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.
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
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.
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.
Let us know the scenarios where you would use this feature. Leave a comment below. Thanks and happy coding!