Create PDF Forms via Python

Create acroforms in PDF programmatically using Aspose.PDF for Python for .NET Library

How to create PDF forms using Python

In order to create PDF Forms (Acroforms) in PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for C# platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

How to Create AcroForm in PDF 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. Create a field.
  3. Create decorations (like Border).
  4. Add field to the document and save modified PDF

Create PDF Forms in PDF - Python

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

import aspose.pdf as apdf

path_infile = self.dataDir + infile
path_outfile = self.dataDir + outfile
document = apdf.Document(path_infile)

# Create a new text box field
rectange = apdf.Rectangle(100, 100, 200, 200, 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 = 5
border.dash = apdf.annotations.Dash(1, 1)
textBoxField.border = border

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

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

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