Sign Documents with LEADDocument - Python

This tutorial shows how to add a signature image to a document in a Python application using the LEADTOOLS SDK.

Summary This tutorial covers how to add a signature image to a LEADDocument in a Python application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (6 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 Sign Documents with LEADDocument - 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 you do not have that project, 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 DLL 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 LEADDocument and Sign Document Code

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

In the Solution Explorer, open and place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
from Leadtools import * 
from Leadtools.Document import * 
from Leadtools.Document.Converter import * 
from Leadtools.Document.Writer import * 
from System.Collections.Generic import * 
from System.IO import * 

Add a new method called sign_document(pdf_to_sign). Call the new method in the main() method as seen below:

def main(): 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS22/Support/Common/License")) 
    pdf_to_sign = r"C:\LEADTOOLS22\Resources\Images\RentalLeaseAgreement.pdf" 

Add the following code to the sign_document() method:

def sign_document(pdf_to_sign): 
    document = DocumentFactory.LoadFromFile(pdf_to_sign, LoadDocumentOptions()) 
    # Define signature and resources 
    resources = document.FormFields.GetResources() 
    signature = resources.CreateSignature() 
    # SignerName and SignerInitial are optional fields 
    signature.SignerName = "John D." 
    signature.SignerInititials = "JD" 
    # Set the image for the signature 
    signature.SignatureImage = DocumentFactory.RasterCodecsTemplate.Load("Python-Sign-Documents-With-LEADDocument.png") 
    # If you would like to add an image for initial fields: 
    # signature.InitialsImage = DocumentFactory.RasterCodecsTemplate.Load(r"PATH-TO-INITIAL-IMAGE") 
    # Update the document resources 
    # Now that we have the resource ready, we can create the signatures fields 
    signature_field = DocumentSignatureFormField() 
    signature_field.Bounds = LeadRectD(3200, 600, 1800, 800) 
    signature_field.SignerID = signature.SignerID 
    signature_field.Signed = True 
    # Create a container and add the signature field to it. Use page 7 for this document 
    container = DocumentFormFieldsContainer() 
    container.PageNumber = 7 
    if (document.IsReadOnly): 
        document.IsReadOnly = False 
    # Set the form fields containers in the LEADDocument 
    containers = List[DocumentFormFieldsContainer]() 
    containers = {container} 
    converter = DocumentConverter() 
    job_data = DocumentConverterJobData() 
    job_data.Document = document 
    job_data.DocumentFormat = DocumentFormat.Pdf 
    job_data.OutputDocumentFileName = Path.Combine(Path.GetDirectoryName(pdf_to_sign), Path.GetFileNameWithoutExtension(pdf_to_sign) + "_signed.pdf") 
    job = converter.Jobs.CreateJob(job_data) 
    if(job.Errors.Count > 0): 
        for error in job.Errors: 
            print(f"Error: {error.Error.Message}") 
         print(f"Signed and saved to: {job.OutputDocumentFiles[0]}") 


The signature image used for this tutorial is included with the downloadable project or can be downloaded here: Signature Image

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 application loads the PDF document, adds a signature to the 7th page, and exports the document.

Signature Image


This tutorial showed how to load a document and sign it, using a PNG signature image. Also, it covered how to use the LEADDocument, DocumentFormFieldsContainer, DocumentSignatureFormField, and DocumentConverter classes.

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.