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 |
|
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.
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
Leadtools.dllLeadtools.Document.dllLeadtools.Document.Converter.dllLeadtools.Document.Writer.dllFor a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
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 LEADTOOLSfrom leadtools import LibraryLoaderLibraryLoader.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 resourcesresources = document.FormFields.GetResources()signature = resources.CreateSignature()# SignerName and SignerInitial are optional fieldssignature.SignerName = "John D."signature.SignerInititials = "JD"# Set the image for the signaturesignature.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 resourcesdocument.FormFields.SetResources(resources)# Now that we have the resource ready, we can create the signatures fieldssignature_field = DocumentSignatureFormField()signature_field.Bounds = LeadRectD(3200, 600, 1800, 800)signature_field.SignerID = signature.SignerIDsignature_field.Signed = True# Create a container and add the signature field to it. Use page 7 for this documentcontainer = DocumentFormFieldsContainer()container.PageNumber = 7container.Children.Add(signature_field)if (document.IsReadOnly):document.IsReadOnly = False# Set the form fields containers in the LEADDocumentcontainers = List[DocumentFormFieldsContainer]()containers = {container}document.FormFields.SetFormFields(containers)converter = DocumentConverter()job_data = DocumentConverterJobData()job_data.Document = documentjob_data.DocumentFormat = DocumentFormat.Pdfjob_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 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.

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.