Fill Fields by Name and Value

Contents
[ ]

Filling form fields using a name–value collection allows developers to create scalable and flexible solutions for PDF form automation. In this example, the Form facade from aspose.pdf.facades is used to bind a PDF document and iterate through a dictionary of field data. Each entry is applied using the ‘fill_field method’, enabling efficient bulk updates to form fields.

  1. Initialize ‘pdf_facades.Form()’ to work with interactive form fields.
  2. Use ‘bind_pdf()’ to attach the source PDF document.
  3. Create a dictionary containing field names and the values you want to insert.
  4. Iterate through the dictionary and call ‘fill_field()’ for each entry.
  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


# Fill Fields by Name and Value
def fill_fields_by_name_and_value(infile, outfile):
    """Fill PDF form fields by name and value."""
    # Create Form object
    pdf_form = pdf_facades.Form()

    # Bind PDF document
    pdf_form.bind_pdf(infile)

    # Fill fields by name and value
    fields = {
        "name": "Jane Smith",
        "address": "456 Elm St, Othertown, USA",
        "email": "jane.smith@example.com",
    }
    for field_name, value in fields.items():
        pdf_form.fill_field(field_name, value)

    # Save updated PDF using outfile
    pdf_form.save(outfile)