Rename Form Fields
Contents
[
Hide
]
Renaming form fields is useful when aligning PDF forms with internal naming conventions or preparing documents for structured data processing. In this example, the Form facade from the aspose.pdf.facades module is used to bind the source PDF and apply a mapping that replaces old field names with new ones. After updating the field identifiers, the document is saved with the changes applied.
- Initialize pdf_facades.Form() to interact with PDF form fields.
- Call ‘bind_pdf()’ to attach the PDF document.
- Create a list of tuples containing old field names and their corresponding new names.
- Iterate through the mapping and call ‘rename_field()’ for each entry.
- 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
# Rename form fields
def rename_form_fields(infile, outfile):
"""Rename form fields in a PDF document."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Rename form fields by providing a mapping of old names to new names
field_renaming_map = [("First Name", "NewFirstName"), ("Last Name", "NewLastName")]
for old_name, new_name in field_renaming_map:
pdf_form.rename_field(old_name, new_name)
# Save updated PDF
pdf_form.save(outfile)