Import JSON Data
Contents
[
Hide
]
JSON is widely used for storing and transferring structured data between systems. 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 JSON file. After the import process, the updated document is saved as a new PDF.
- Initialize pdf_facades.Form() to interact with PDF form fields.
- Call ‘bind_pdf()’ to attach the PDF form template.
- Use ‘FileIO()’ to read the JSON file containing form values.
- Call ‘import_json()’ to populate PDF fields using JSON key–value pairs.
- 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 from JSON
def import_json_to_pdf_form(infile, datafile, outfile):
"""Import form data from JSON file into PDF form fields."""
# Create Form object
form = pdf_facades.Form()
# Bind PDF document
form.bind_pdf(infile)
# Open JSON file as stream
with FileIO(datafile, "r") as json_stream:
# Import data from JSON into PDF form fields
form.import_json(json_stream)
# Save updated PDF
form.save(outfile)