Parse, Edit, and Save PDF Form Fields - Python

This tutorial shows how to work with PDF form fields in a Python Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to load, edit, and save PDF form fields in a Python Console application.
Completion Time 15 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Python Console Application
IDE Visual Studio 2022
Runtime Target Python 3.10 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Parse, Edit, and Save PDF Form Fields - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project.

This tutorial requires the following .NET DLLs:

For a complete list of which files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the PDFFormField Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Project-Name.py and place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
LibraryLoader.add_reference("Leadtools") 
from Leadtools import * 
LibraryLoader.add_reference("Leadtools.Pdf") 
from Leadtools.Pdf import * 

Add a new method to the Project-Name.py file named parse_pdf_fields(in_file) to return a list of parsed PDFFormField objects from a loaded PDF file. Call this method below the set license code in the main() method:

def parse_pdf_fields(in_file): 
    pdf_document = PDFDocument(in_file) 
    pdf_document.ParsePages(PDFParsePagesOptions.FormFields, 1, -1) 
 
    fields = List[PDFFormField]() 
    for page in pdf_document.Pages: 
        fields.AddRange(page.FormFields) 
    return fields 

Add another method in the Project-Name.py file named edit_form_fields(edited_fields) to take the parsed form fields, edit them, and return them to the main() method to be saved. Add this method inside the main() method below the call to parse_pdf_fields().

def edit_form_fields(edited_fields): 
 
    for field in edited_fields: 
        if (field.ContentType == PDFFormField.ContentTypeUnrestrained): 
            field_list = List[str]() 
            field_list.Add("Hello world!") 
            field.Contents = field_list 
    return edited_fields 

Add the code below to the main() method, below the call to edit_form_fields(), to save the modified form fields.

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License")) 
 
    in_file = r"C:\LEADTOOLS23\Resources\Images\InteractiveForm.pdf" 
    out_file = r"C:\LEADTOOLS23\Resources\Images\FormFieldsOutput.pdf" 
 
    # Load and parse PDF form fields 
    fields = parse_pdf_fields(in_file) 
 
    # Edit the fields 
    edited_fields = edit_form_fields(fields) 
 
    # Save the fields 
    form_document = PDFFile(in_file) 
    form_document.FillFormFields(edited_fields, out_file) 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the form fields inside the PDF are loaded, parsed, edited, and saved back out to PDF file.

Wrap-up

This tutorial showed how to work with the PDFFile, PDFDocument and PDFFormField classes.

See Also

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.