Import XML Data

Contents
[ ]

XML is commonly used to store structured form data, making it a practical format for transferring values between systems. In this example, the Form facade from aspose.pdf.facades is used to load a PDF form and apply field values directly from an XML file. After importing the data, the updated PDF is saved as a new document.

  1. Initialize pdf_facades.Form() to interact with PDF form fields.
  2. Call ‘bind_pdf()’ to attach the PDF form template.
  3. Use ‘FileIO()’ to access the XML file containing form data.
  4. Call ‘import_xml()’ to populate PDF fields with values from the XML file.
  5. Save the updated PDF.
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 XML
def import_xml_to_pdf_fields(infile, datafile, outfile):
    """Import form data from XML file into PDF form fields."""
    # Create Form object
    pdf_form = pdf_facades.Form()

    # Bind PDF document
    pdf_form.bind_pdf(infile)

    # Open XML file as stream
    with FileIO(datafile, "r") as xml_input_stream:
        # Import data from XML into PDF form fields
        pdf_form.import_xml(xml_input_stream)

    # Save updated PDF
    pdf_form.save(outfile)