Modifying AcroForm
Contents
[
Hide
]
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.
- Load the input PDF document.
- Access forms on the first page.
- Iterate over each form and check if it is a
Typewriterform. - Use TextFragmentAbsorber to find text fragments in the form.
- Clear the text of each fragment.
- 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.
- Create a
FormEditorobject. - Bind the input PDF.
- Set the field limit for a target field.
- 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.
- Load the PDF document.
- Access the target form field.
- Ensure the field is a
TextBoxField. - 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.
- Load the PDF document.
- Access the target field and verify its type.
- Find the font in
FontRepository. - Apply a new
DefaultAppearance. - 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.
- Load the PDF document.
- Delete a form field by name.
- 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)