Create a digitally signed PDF in C# and VB.NET

PDF digital signature enables you to authenticate a document to establish that the sender of the document is who they say they are and that the content of the document has not been tampered with.

GemBox.Document will digitally sign a PDF file with the X.509 certificate if either Certificate, CertificateBytes or CertificatePath is specified.

If you require multiple signatures on a PDF or if you have an external digital signature (such as HSM, USB token, smart card or service), take a look at this alternative approach for digitally signing PDF text using GemBox.Pdf.

The following example shows how you can create a digitally signed PDF file in C# and VB.NET, with visual representation.

Digitally signing PDF file with visual representation in C# and VB.NET
Screenshot of PDF document's digital signature
Upload your file (Drag file here)
using System.Linq;
using GemBox.Document;
using GemBox.Document.Drawing;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = DocumentModel.Load("%InputFileName%");

        // Get placeholder where signature should be visualized.
        // Signature line was added with: Microsoft Word => Insert => Signature Line
        // By default it'll have "Microsoft Office Signature Line..." description.
        var signatureLine = document.GetChildElements(true).OfType<DrawingElement>().FirstOrDefault(
            de => de.Metadata.Description == "Microsoft Office Signature Line...");

        // Create visual representation of digital signature.
        var signature = new Picture(document, "%#GemBoxSignature.png%");

        // Position signature image in a signature line.
        // Image will be placed 1.5cm right and 0.5cm below the top-left corner of signature line.
        signature.Layout = Layout.Floating(
            new HorizontalPosition(1.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
            new VerticalPosition(0.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
            signature.Layout.Size);

        var options = new PdfSaveOptions()
        {
            DigitalSignature =
            {
                CertificatePath = "%#GemBoxExampleExplorer.pfx%",
                CertificatePassword = "GemBoxPassword",
                SignatureLine = signatureLine,
                Signature = signature
            }
        };

        document.Save("PDF Digital Signature.pdf", options);
    }
}
Imports System.Linq
Imports GemBox.Document
Imports GemBox.Document.Drawing

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document = DocumentModel.Load("%InputFileName%")

        ' Get placeholder where signature should be visualized.
        ' Signature line was added with: Microsoft Word => Insert => Signature Line
        ' By default it'll have "Microsoft Office Signature Line..." description.
        Dim signatureLine As DrawingElement = document.GetChildElements(True).OfType(Of DrawingElement)().FirstOrDefault(
            Function(de) de.Metadata.Description = "Microsoft Office Signature Line...")

        ' Create visual representation of digital signature.
        Dim signature As New Picture(document, "%#GemBoxSignature.png%")

        ' Position signature image in a signature line.
        ' Image will be placed 1.5cm right and 0.5cm below the top-left corner of signature line.
        signature.Layout = Layout.Floating(
            New HorizontalPosition(1.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
            New VerticalPosition(0.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
            signature.Layout.Size)

        Dim options As New PdfSaveOptions() With
        {
            .DigitalSignature = New PdfDigitalSignatureSaveOptions() With
            {
                .CertificatePath = "%#GemBoxExampleExplorer.pfx%",
                .CertificatePassword = "GemBoxPassword",
                .signatureLine = signatureLine,
                .signature = signature
            }
        }

        document.Save("PDF Digital Signature.pdf", options)

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.