Delete Forms from PDF in Python
Contents
[
Hide
]
Remove All Forms from a Page
This code removes all form objects from the page specified by page_num and saves the updated document.
- Load the PDF document.
- Access page resources.
- Clear form objects.
- 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.
- Load the PDF document.
- Access page forms.
- Iterate over forms.
- Check for typewriter forms.
- Delete the matched form.
- 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)