PDF Forms. Manage via Python

Manage Acroforms in PDF document using Aspose.PDF for Python via .NET Library

How to Manage PDF Forms Using Python via .NET Library

In order to add PDF Forms (Acroforms) in a PDF file, we’ll use Aspose.PDF for Python via .NET API, which is feature-rich, powerful, and easy-to-use document manipulation API for Python app. You can download its latest version directly from PyPi package manager, search for aspose-pdf and install. You may also use the following command from the console or terminal.

Console

pip install aspose-pdf

How to Create PDF Forms using Python

You need Aspose.PDF for Python via .NET to try the code in your environment.

  1. Load PDF in an instance of Document class.
  2. Access the Page via its index.
  3. Call the Form collection’s Add method.
  4. Create the form field you want to add.
  5. Save the PDF file.

Create PDF Forms in PDF - Python

This sample code shows how to Create PDF Forms in PDF using Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)
document = apdf.Document(path_infile)

# Create a new text box field
rectange = apdf.Rectangle(100, 100, 200, 120, True)
textBoxField = apdf.forms.TextBoxField(document.pages[1], rectange)
textBoxField.partial_name = "textbox1"
textBoxField.value = "Text Box"

# Customize the border of the text box field
border = apdf.annotations.Border(textBoxField)
border.width = 3
border.dash = apdf.annotations.Dash(1, 1)
textBoxField.border = border

# Set the color of the text box field
textBoxField.color = apdf.Color.dark_green

# Add the text box field to the form
document.form.add(textBoxField, 1)

# Save the modified PDF document
document.save(path_outfile)