Replace XFA Data

Contents
[ ]

XFA (XML Forms Architecture) forms store their data in XML format within the PDF structure. In this example, the Form facade from the aspose.pdf.facades module is used to bind a PDF and replace its existing XFA dataset using an external XML stream. After applying the new data, the updated PDF is saved as a separate file.

  1. Initialize pdf_facades.Form() to manage XFA form data.
  2. Call ‘bind_pdf()’ to attach the PDF containing XFA forms.
  3. Use ‘FileIO()’ to read the XFA XML file.
  4. Call ‘set_xfa_data()’ to update the PDF with new XFA content.
  5. 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


# Replace from XFA data
def replace_xfa_data(infile, datafile, outfile):
    """Import form data from XFA file into PDF form fields."""
    # Create Form object
    form = pdf_facades.Form()

    # Bind PDF document
    form.bind_pdf(infile)

    # Open XFA file as stream
    with FileIO(datafile, "r") as xfa_stream:
        # Import data from XFA into PDF form fields
        form.set_xfa_data(xfa_stream)

    # Save updated PDF
    form.save(outfile)