Python: Flatten Forms in PDF

2024-03-21 01:08:17 Written by  support iceblue
Rate this item
(0 votes)

Flattening forms in PDF means transforming the interactive form fields (such as text boxes, checkboxes, and drop-down menus) into static content. Once a form is flattened, it cannot be edited or filled out anymore. When you need to maintain a permanent and unalterable record of a completed form, flattening is essential. This ensures that the data entered into the form fields cannot be modified or tampered with, providing a reliable reference for future use. In this article, we will demonstrate how to flatten forms in PDF in Python using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Flatten All Forms in a PDF in Python

Spire.PDF for Python provides the PdfDocument.Form.IsFlatten property, which enables you to flatten all forms in a PDF file. The detailed steps are as follows.

  • Create an object of the PdfDocument class.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Flatten all forms in the PDF file by setting the PdfDocument.Form.IsFlatten property to True.
  • Save the result file using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Specify the input and output PDF file paths
input_file = "Form.pdf"
output_file = "FlattenAll.pdf"

# Create an object of the PdfDocument class
doc = PdfDocument()
# Load a PDF file
doc.LoadFromFile(input_file)

# Flatten all forms in the PDF file
doc.Form.IsFlatten = True

# Save the result file
doc.SaveToFile(output_file)
doc.Close()

Python: Flatten Forms in PDF

Flatten a Specific Form in a PDF in Python

To flatten a specific form in a PDF file, you can use the PdfField.Flatten property. The detailed steps are as follows.

  • Create an object of the PdfDocument class.
  • Load a PDF file using the PdfDocument.LoadFromFile() method.
  • Get the forms of the PDF file using PdfDocument.Form property.
  • Get a specific form by its index or name using PdfFormWidget.FieldsWidget.get_Item() method.
  • Flatten the form by setting the PdfField.Flatten property to True.
  • Save the result file using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Specify the input and output PDF file paths
input_file = "Form.pdf"
output_file = "FlattenSpecific.pdf"

# Create an object of the PdfDocument class
doc = PdfDocument()
# Load a PDF file
doc.LoadFromFile(input_file)

# Get the forms of the PDF file
loadedForm = doc.Form

# Get a specific form by its index or name
formWidget = PdfFormWidget(loadedForm)
form = formWidget.FieldsWidget.get_Item(2)
# form = formWidget.FieldsWidget.get_Item("Address")

# Flatten the specific form
form.Flatten = True

# Save the result file
doc.SaveToFile(output_file)
doc.Close()

Python: Flatten Forms in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:27