Python: Import and Export PDF Form Data

2024-11-04 01:09:46 Written by  support iceblue
Rate this item
(0 votes)

PDF forms are essential tools for collecting information across various industries. Understanding how to import and export this data in different formats like FDF, XFDF, and XML can greatly enhance your data management processes. For instance, importing form data allows you to update or pre-fill PDF forms with existing information, saving time and increasing accuracy. Conversely, exporting form data enables you to share collected information effortlessly with other applications, facilitating seamless integration and minimizing manual entry errors. In this article, we will introduce how to import and export PDF form data in Python using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

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

Spire.PDF for Python 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.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("Forms.pdf")

# Get the form of the document
pdfForm = pdf.Form
formWidget = PdfFormWidget(pdfForm)

# Import PDF form data from an XML file
formWidget.ImportData("Data.xml", DataFormat.Xml)

# Import PDF form data from an FDF file
# formWidget.ImportData("Data.fdf", DataFormat.Fdf)

# Import PDF form data from an XFDF file
# formWidget.ImportData("Data.xfdf", DataFormat.XFdf)

# Save the resulting document
pdf.SaveToFile("Output.pdf")
# Close the PdfDocument object
pdf.Close()

Python: Import and Export PDF Form Data

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

Spire.PDF for Python also enables developers 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.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("Forms.pdf")

# Get the form of the document
pdfForm = pdf.Form
formWidget = PdfFormWidget(pdfForm)

# Export PDF form data to an XML file
formWidget.ExportData("Data.xml", DataFormat.Xml, "Form")

# Export PDF form data to an FDF file
# formWidget.ExportData("Data.fdf", DataFormat.Fdf, "Form")

# Export PDF form data to an XFDF file
# formWidget.ExportData("Data.xfdf", DataFormat.XFdf, "Form")

# Close the PdfDocument object
pdf.Close()

Python: 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.