Sign Documents with LEADDocument - C# .NET 6

This tutorial shows how to add a signature image to a document in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to add a signature image to a LEADDocument in a C# .NET 6 application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (6 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET 6+ (Console)

  • Python: Python

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 - C# .NET 6 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.

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net\:

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 Program.cs. Add the following statements to the using block at the top of Program.cs.

C#
// Using block at the top 
using Leadtools; 
using Leadtools.Document; 
using Leadtools.Document.Converter; 

In the Main() method add the following code:

C#
static void Main(string[] args) 
{ 
    InitLEAD(); 
 
    var pdfToSign = @"C:\LEADTOOLS23\Resources\Images\RentalLeaseAgreement.pdf"; 
 
    using var document = DocumentFactory.LoadFromFile(pdfToSign, new LoadDocumentOptions()); 
 
    // Define signature and resources 
    var resources = document.FormFields.GetResources(); 
    var signature = resources.CreateSignature(); 
    // SignerName and SignerInitial are optional fields 
    signature.SignerName = "John D."; 
    signature.SignerInitials = "JD"; 
 
    // Set the image for the signature 
    signature.SignatureImage = DocumentFactory.RasterCodecsTemplate.Load("Dotnet-Console-Sign-Documents-With-LEADDocument.png"); 
 
    // If you would like to add an image for initial fields 
    //signature.InitialsImage = DocumentFactory.RasterCodecsTemplate.Load(@"PATH_TO_INITIAL_IMAGE"); 
 
    resources.Signatures.Add(signature); 
 
    // Update the document resources 
    document.FormFields.SetResources(resources); 
 
    // Now we have the resource ready, we can create the signatures fields 
    var signatureField = new DocumentSignatureFormField() 
    { 
        Bounds = new LeadRectD(3200, 600, 1800, 800), // Place the signature on the signature line in the document 
        SignerID = signature.SignerID, // Link the signature field to the Document Signature Resource 
        Signed = true 
    }; 
 
    // Create a container and add the signature field to it. Use page 7 for this document 
    var container = new DocumentFormFieldsContainer(); 
    container.PageNumber = 7; 
    container.Children.Add(signatureField); 
 
    if (document.IsReadOnly) document.IsReadOnly = false; 
 
    // Set the form fields containers in LEAD Document 
    document.FormFields.SetFormFields(new DocumentFormFieldsContainer[] { container }); 
 
    var converter = new DocumentConverter(); 
    var job = converter.Jobs.CreateJob(new DocumentConverterJobData 
    { 
        Document = document, 
        DocumentFormat = Leadtools.Document.Writer.DocumentFormat.Pdf, 
        OutputDocumentFileName = Path.Combine(Path.GetDirectoryName(pdfToSign), Path.GetFileNameWithoutExtension(pdfToSign) + "_signed.pdf") 
    }); 
    converter.Jobs.RunJob(job); 
 
    if (job.Errors.Count > 0) 
        foreach (var error in job.Errors) 
            Console.WriteLine($"Error: {error.Error.Message}"); 
    else 
        Console.WriteLine($"Signed and saved to: {job.OutputDocumentFiles.First()}"); 
} 

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