Set Field Limit

Contents
[ ]

Form fields in PDF documents may require input restrictions to maintain proper formatting. Using Aspose.PDF for Python, developers can programmatically set a maximum number of characters for a field.

The FormEditor class provides the ‘set_field_limit’ method to define the maximum input length for a field.

  1. Open an existing PDF form.
  2. Create a FormEditor object.
  3. Set the maximum character limit for the field “Last Name”.
  4. Save the updated PDF.
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_limit(infile, outfile):
    # Open document
    doc = ap.Document(infile)

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

    # Set field limit to 10
    if not form_editor.set_field_limit("Last Name", 10):
        raise Exception(
            "Failed to set field limit. Field may not support specified limit."
        )

    # Save updated document
    form_editor.save(outfile)