Delete Forms from PDF in Python

Remove All Forms from a Page

This code removes all form objects from the page specified by page_num and saves the updated document.

  1. Load the PDF document.
  2. Access page resources.
  3. Clear form objects.
  4. Save the updated document.
import aspose.pdf as ap

def remove_all_forms(input_file_name, page_num, output_file_name):
    document = ap.Document(input_file_name)
    forms = document.pages[page_num].resources.forms
    forms.clear()
    document.save(output_file_name)

Remove a Specific Form Type

Next example iterates through the form objects on a given PDF page, identifies typewriter form annotations, deletes them, and then saves the updated PDF using Aspose.PDF for Python via .NET.

  1. Load the PDF document.
  2. Access page forms.
  3. Iterate over forms.
  4. Check for typewriter forms.
  5. Delete the matched form.
  6. Save the updated document.
import aspose.pdf as ap

def remove_specified_form(input_file_name, page_num, output_file_name):
    document = ap.Document(input_file_name)
    forms = document.pages[page_num].resources.forms
    for form in forms:
        if form.it == "Typewriter" and form.subtype == "Form":
            name = forms.get_form_name(form)
            forms.delete(name)
    document.save(output_file_name)