Set Field Alignment

Contents
[ ]

PDF form fields often require customized text alignment to maintain a consistent and professional layout. Using Aspose.PDF for Python, developers can programmatically set the alignment of a form field’s text content.

The FormEditor class, in combination with the FormFieldFacade constants, allows developers to modify the alignment of existing form fields programmatically.

  1. Open an existing PDF document.
  2. Create a FormEditor object.
  3. Set the alignment of a field named “First Name” to center.
  4. Save the modified document.
from io import FileIO
import sys
from os import path
import aspose.pdf as ap
import aspose.pydrawing as ap_pydrawing
import aspose.pdf.facades as pdf_facades

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def set_field_alignment(infile, outfile):
    # Open document
    doc = ap.Document(infile)

    # Create FormEditor object
    form_editor = pdf_facades.FormEditor(doc)

    # Set field alignment to center
    if form_editor.set_field_alignment(
        "First Name", pdf_facades.FormFieldFacade.ALIGN_CENTER
    ):
        # Save updated document
        form_editor.save(outfile)
    else:
        raise Exception(
            "Failed to set field alignment. Field may not support alignment."
        )