Set Field Alignment Vertical

Contents
[ ]

PDF form fields can contain text that needs proper vertical alignment for a consistent and professional appearance. Using Aspose.PDF for Python, developers can programmatically modify the vertical alignment of form fields. Vertical alignment allows developers to control whether the field’s text appears at the top, center, or bottom of the field’s bounding box, improving the layout and readability of form data.

Using the FormEditor class and the FormFieldFacade constants, developers can adjust vertical alignment programmatically to achieve consistent form layout:

  1. Open an existing PDF document.
  2. Create a FormEditor object.
  3. Set the vertical alignment of a field named “First Name” to bottom.
  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_vertical(infile, outfile):
    # Open document
    doc = ap.Document(infile)

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

    # Attempt to set vertical alignment of the "First Name" field to bottom
    if form_editor.set_field_alignment_v(
        "First Name", pdf_facades.FormFieldFacade.ALIGN_BOTTOM
    ):
        # Save updated document
        form_editor.save(outfile)
    else:
        raise Exception(
            "Failed to set field vertical alignment. Field may not support vertical alignment."
        )