How to Fill PDF Forms

Learn how easily Fill PDF Forms with high quality using Python via .NET PDF library

How to Fill PDF Forms via Python

When dealing with documents, the need often arises to fill out a PDF form. Python, known for its versatility, offers a way to accomplish this task seamlessly.

To fill out the form, we will use Aspose.PDF for Python via .NET. This robust Python library enables effortless creation and editing of text documents. Install it within your Python application from PyPI using the following pip command.

pip install aspose-pdf 

Fill PDF Form

This Python script uses the Aspose.PDF library to manipulate a PDF file with form fields. Here’s a step-by-step breakdown:

  • Import Aspose.PDF module: The code imports the Aspose.PDF library using the alias pdf.
  • Load the license: It loads the licensing information required for Aspose.PDF operations.
  • Load the PDF with form fields: The script loads a PDF file named “TextBox_out.pdf” that contains form fields.
  • Access the target form field: It retrieves the first form field from the loaded PDF file.
  • Set a new value for the form field: Updates the value of the retrieved form field with a new text.
  • Adjust the appearance of the form field: Modifies the appearance properties of the form field, changing its text color to red and adjusting the vertical alignment to the bottom.
  • Save the modified PDF with filled form fields: The script saves the modified PDF document with the updated form field values as “TextBoxUpdated.pdf”.
  • Print a success message: Displays a message confirming that the form fields have been successfully filled in the PDF file.

Check out this code snippet to see it in action:

    import aspose.pdf as pdf

    # Load the license 
    license = pdf.License() 
    license.set_license("Aspose.Total.lic") 
    # Load the PDF 
    pdfWithFormFields = pdf.Document("TextBox_out.pdf") 
    # Get target field 
    formFieldTextBox = pdfWithFormFields.form.fields[0] 
    # Set new value 
    formFieldTextBox.value = "Here is the new value for the form field" 
    # Set the field appearance 
    formFieldTextBox.color = pdf.Color.red 
    formFieldTextBox.vertical_alignment=pdf.VerticalAlignment.BOTTOM 
    # Save the filled PDF 
    pdfWithFormFields.save("TextBoxUpdated.pdf")

In summary, this code uses Aspose.PDF in Python, loading the license and opening a PDF file named “TextBox_out.pdf” with form fields. It selects the first form field, updating its value and adjusting its appearance (color to red, vertical alignment to bottom). After filling in the form fields, it saves the modified PDF as “TextBoxUpdated.pdf” and prints a success message.

Documentation Aspose.PDF for .NET Library

Aspose.PDF for .NET is a powerful native library empowering developer to seamlessly integrate PDF processing functionalities into their applications. This API facilitates the creation of both 32-bit and 64-bit applications, providing the capability to generate, read, convert, and manipulate PDF files, eliminating the necessity for Adobe Acrobat. Explore other features of Aspose.PDF for .NET library using Documentation. To resolve particular cases you can visit our forum.

Get a Free License:

Get a temporary license and try to convert PDF to markdown without any limitation.

Conclusion

In summary, the provided code showcases the process of filling PDF forms using Python. It accesses the target text box field through the Document.form.fields collection, demonstrating the ability to interact with various types of fields such as buttons, checkboxes, list boxes, and more. The code sets specific properties of the form field like color and vertical alignment, highlighting the possibility to adjust additional attributes such as border, height, width, margin, etc., as needed. If you want more details about Aspose.PDF libraries - visit our landings pages