Import XFDF Data
Contents
[
Hide
]
XFDF (XML Forms Data Format) is an XML representation of PDF form data designed for interoperability and data exchange. In this example, the Form facade from the aspose.pdf.facades module is used to bind a PDF form and import field values from an external XFDF file. After importing the data, the updated PDF is saved as a new document.
- Initialize pdf_facades.Form() to interact with PDF form fields.
- Call ‘bind_pdf()’ to attach the PDF form template.
- Use ‘open()’ to read the XFDF file.
- Call ‘import_xfdf()’ to populate PDF fields with values from the XFDF file.
- Save the updated Document.
from io import FileIO
import sys
from os import path
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
# Import Data from XFDF
def import_data_from_xfdf(infile, datafile, outfile):
"""Import form data from XFDF file into PDF form fields."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Open XFDF file as stream
with open(datafile, "rb") as xfdf_input_stream:
# Import data from XFDF into PDF form fields
pdf_form.import_xfdf(xfdf_input_stream)
# Save updated PDF
pdf_form.save(outfile)