Modifying AcroForm

Clear Text in Form

This example demonstrates how to clear text from Typewriter form fields in a PDF using Aspose.PDF for Python via .NET. It scans the first page of the PDF, identifies Typewriter forms, removes their content, and saves the updated document. This approach is useful for resetting or sanitizing form fields before redistributing a PDF.

  1. Load the input PDF document.
  2. Access forms on the first page.
  3. Iterate over each form and check if it is a Typewriter form.
  4. Use TextFragmentAbsorber to find text fragments in the form.
  5. Clear the text of each fragment.
  6. Save the modified PDF to the output file.
import aspose.pdf as ap


def clear_text_in_form(input_file_name, output_file_name):
    document = ap.Document(input_file_name)

    forms = document.pages[1].resources.forms

    for form in forms:
        if form.it == "Typewriter" and form.subtype == "Form":
            absorber = ap.text.TextFragmentAbsorber()
            absorber.visit(form)

            for fragment in absorber.text_fragments:
                fragment.text = ""

    document.save(output_file_name)

Set Field Limit

Use set_field_limit(field, limit) from FormEditor to define the maximum number of characters allowed in a text field.

  1. Create a FormEditor object.
  2. Bind the input PDF.
  3. Set the field limit for a target field.
  4. Save the updated PDF.
import aspose.pdf as ap


def set_field_limit(input_file_name, output_file_name):
    form = ap.facades.FormEditor()
    form.bind_pdf(input_file_name)
    form.set_field_limit("First Name", 15)
    form.save(output_file_name)

Get Field Limit

You can also read the character limit from a text field.

  1. Load the PDF document.
  2. Access the target form field.
  3. Ensure the field is a TextBoxField.
  4. Read and print max_len.
import aspose.pdf as ap
from aspose.pycore import cast, is_assignable


def get_field_limit(input_file_name):
    document = ap.Document(input_file_name)
    if is_assignable(document.form[1], ap.forms.TextBoxField):
        text_box_field = cast(ap.forms.TextBoxField, document.form[1])
        print(f"Limit: {text_box_field.max_len}")

Set Custom Font for the Form Field

This example sets a custom default appearance for a text box field, including font, size, and color.

  1. Load the PDF document.
  2. Access the target field and verify its type.
  3. Find the font in FontRepository.
  4. Apply a new DefaultAppearance.
  5. Save the updated PDF.
import aspose.pdf as ap
from aspose.pycore import cast, is_assignable


def set_form_field_font(input_file_name, output_file_name):
    document = ap.Document(input_file_name)
    if is_assignable(document.form[1], ap.forms.TextBoxField):
        text_box_field = cast(ap.forms.TextBoxField, document.form[1])
        font = ap.text.FontRepository.find_font("Calibri")
        text_box_field.default_appearance = ap.annotations.DefaultAppearance(
            font, 10, ap.Color.black.to_rgb()
        )

    document.save(output_file_name)

Remove Fields in an Existing Form

This code removes a specific form field (by its name) from a PDF document and saves the updated file using Aspose.PDF for Python via .NET.

  1. Load the PDF document.
  2. Delete a form field by name.
  3. Save the updated PDF.
import aspose.pdf as ap


def delete_form_field(input_file_name, output_file_name):
    document = ap.Document(input_file_name)
    document.form.delete("First Name")
    document.save(output_file_name)