Fill AcroForm - Fill PDF Form using Python

Fill Form Field in a PDF Document

The following example fills multiple fields in an existing PDF form by using the Form facade.

Use the following steps:

  1. Create a dictionary with field names and values.
  2. Bind the input PDF to a Form object.
  3. Iterate through available form fields.
  4. Fill fields that exist in the dictionary.
  5. Save the updated PDF.
import aspose.pdf as ap

def fill_form(input_file_name, output_file_name):
    new_field_values = {
        "First Name": "Alexander_New",
        "Last Name": "Greenfield_New",
        "City": "Yellowtown_New",
        "Country": "Redland_New",
    }

    form = ap.facades.Form(input_file_name)

    for field_name in form.field_names:
        if field_name in new_field_values:
            form.fill_field(field_name, new_field_values[field_name])

    form.save(output_file_name)