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.

Overview  
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 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.Document") 
from Leadtools.Document import * 
LibraryLoader.add_reference("Leadtools.Document.Converter") 
from Leadtools.Document.Converter import * 
LibraryLoader.add_reference("Leadtools.Document.Writer") 
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:/LEADTOOLS23/Support/Common/License")) 
 
    pdf_to_sign = r"C:\LEADTOOLS23\Resources\Images\RentalLeaseAgreement.pdf" 
    sign_document(pdf_to_sign) 

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") 
 
    resources.Signatures.Add(signature) 
 
    # Update the document resources 
    document.FormFields.SetResources(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 
    container.Children.Add(signature_field) 
 
    if (document.IsReadOnly): 
        document.IsReadOnly = False 
     
    # Set the form fields containers in the LEADDocument 
 
    containers = List[DocumentFormFieldsContainer]() 
    containers = {container} 
    document.FormFields.SetFormFields(containers) 
 
    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) 
     
    converter.Jobs.RunJob(job) 
 
    if(job.Errors.Count > 0): 
        for error in job.Errors: 
            print(f"Error: {error.Error.Message}") 
    else: 
         print(f"Signed and saved to: {job.OutputDocumentFiles[0]}") 

Note: 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

Wrap-up

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 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.