Serialize a PDF Form - Python

This tutorial shows how to serialize a PDF Form and fill form fields with JSON and XML files in a Python Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to serialize PDF forms in a Python Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 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 and Parse,​ Edit,​ and Save PDF Form Fields tutorials, before working on the Serialize a PDF Form - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Parse,​ Edit,​ and Save PDF Form Fields 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 Serialize PDF Form 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 * 
LibraryLoader.add_reference("Newtonsoft.Json") 
from Newtonsoft.Json import * 
 
from System.Runtime.Serialization import DataContractSerializer 
from System.Collections.Generic import * 
from System.Xml import * 
from System.IO import * 

Add four new methods to the Project-Name.py file named serialize_to_json(input_pdf, json_file), fill_from_json(input_pdf, json_file), serialize_to_xml(input_pdf, xml_file), and fill_from_xml(input_pdf, xml_file). Call these new methods inside the main() method, as shown below.

Also, add three string values to the main() method, input_pdf = r"C:\LEADTOOLS22\Resources\Images\InteractiveForm.pdf", json_file = "formfields.json", and xml_file = "formfields.xml".

def main(): 
 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
 
    input_pdf = r"C:\LEADTOOLS22\Resources\Images\InteractiveForm.pdf" 
    json_file = "formfields.json" 
    xml_file = "formfields.xml" 
 
    serialize_to_json(input_pdf, json_file) 
    fill_from_json(input_pdf, json_file) 
     
    serialize_to_xml(input_pdf, xml_file) 
    fill_from_xml(input_pdf, xml_file) 

Add the code below to the serialize_to_json(input_pdf, json_file) method to write serialized fields to a JSON file.

def serialize_to_json(input_pdf, json_file): 
 
    # Create a variable to hold the parsed field data 
    form_fields = parse_pdf_fields(input_pdf) 
 
    # Convert the field data to JSON 
    json = JsonConvert.SerializeObject(form_fields) 
 
    # Write the JSON file 
    File.WriteAllText(json_file, json) 

Add the code below to the fill_from_json(input_pdf, json_file) method to fill serialized fields from a JSON file.

def fill_from_json(input_pdf, json_file): 
 
    # Read the JSON contents 
    json = File.ReadAllText(json_file) 
 
    # Create a variable to hold the converted JSON data 
    form_fields = JsonConvert.DeserializeObject[List[PDFFormField]](json) 
 
    # Create a new PDFFile object 
    pdf_file = PDFFile(input_pdf) 
 
    # Fill in the form fields 
    pdf_file.FillFormFields(form_fields, input_pdf) 

Add the code below to the serialize_to_xml(input_pdf, xml_file) method to write serialized fields to an XML file.

def serialize_to_xml(input_pdf, xml_file): 
 
    # Parse the PDF fields 
    form_fields = parse_pdf_fields(input_pdf) 
 
    # Get the type of form 
    form_type = form_fields.GetType() 
 
    # Create a DataContractSerializer object that will write the XML 
    serializer = DataContractSerializer(form_type) 
    string_writer = StringWriter() 
 
    xml_settings = XmlWriterSettings() 
    xml_settings.Indend = True 
    xml_writer = XmlWriter.Create(string_writer, xml_settings) 
 
    # Use the serializer object to write to XML objects 
    serializer.WriteObject(xml_writer, form_fields) 
    xml_writer.Close() 
 
    # Write the text of the serialized fields 
    File.WriteAllText(xml_file, string_writer.ToString()) 

Add the code below to the fill_from_xml(input_pdf, xml_file) method to write serialized fields to an XML file.

def fill_from_xml(input_pdf, xml_file): 
 
    # Read the XML file 
    xml_string = File.ReadAllText(xml_file) 
 
    # Create the object that holds the serialized data 
    serializer = DataContractSerializer(type(List[PDFFormField]())) 
 
    # String reader 
    string_reader = StringReader(xml_string) 
 
    # XML reader 
    xml_reader = XmlReader.Create(string_reader) 
 
    # Read the form fields from the XML file 
    form_fields = serializer.ReadObject(xml_reader) 
 
    # Create a pdf file with the data 
    pdf_file = PDFFile(input_pdf) 
 
    # Fill the data in the pdf file 
    pdf_file.FillFormFields(form_fields, input_pdf) 

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 serializes the input PDF file to JSON and XML files, and fills the PDF form field data from JSON and XML files.

Wrap-up

This tutorial showed how to work with the PDFFile, PDFDocument and PDFFormField to serialize and fill PDF Forms.

See Also

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

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