Create digitally signed PDF in C# and VB.NET

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

The following example shows how to create a digitally signed PDF document.

Certificate file: GemBoxSampleExplorer.pfx.

Signature file: GemBoxSignature.png.

Screenshot

PDF Digital Signature Screenshot

See the full code below, use RUN EXAMPLE to execute.

Upload your file (Drag files here)

1using System;
2using GemBox.Document;
3using GemBox.Document.Drawing;
4using System.IO;
5using System.Linq;
6
7class Sample
8{
9    [STAThread]
10    static void Main(string[] args)
11    {
12        // If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
14
15        DocumentModel document = DocumentModel.Load("DigitalSignature.docx");
16
17        string pathToResources = "Resources";
18
19        // Signature line added with MS Word -> Insert tab -> Signature Line button by default has description 'Microsoft Office Signature Line...'.
20        var signatureLine = document.GetChildElements(true).OfType<DrawingElement>().FirstOrDefault(
21            de => de.Metadata.Description == "Microsoft Office Signature Line...");
22
23        var signature = new Picture(document, Path.Combine(pathToResources, "GemBoxSignature.png"));
24
25        // Signature in this document will be 1.5 cm right of TopLeft position of signature line 
26        // and 0.5 cm below of TopLeft position of signature line.
27        signature.Layout = Layout.Floating(
28            new HorizontalPosition(1.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
29            new VerticalPosition(0.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
30            signature.Layout.Size);
31
32        var options = new PdfSaveOptions()
33        {
34            DigitalSignature =
35            {
36                CertificatePath = Path.Combine(pathToResources, "GemBoxSampleExplorer.pfx"),
37                CertificatePassword = "GemBoxPassword",
38                // Placeholder where signature should be visualized.
39                SignatureLine = signatureLine,
40                // Visual representation of digital signature.
41                Signature = signature
42            }
43        };
44
45        document.Save("PDF Digital Signature.pdf", options);
46    }
47}
1Imports System
2Imports GemBox.Document
3Imports GemBox.Document.Drawing
4Imports System.IO
5Imports System.Linq
6
7Module Samples
8
9    Sub Main()
10
11        ' If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
13
14        Dim document As DocumentModel = DocumentModel.Load("DigitalSignature.docx")
15
16        Dim pathToResources As String = "Resources"
17
18        ' Signature line added with MS Word -> Insert tab -> Signature Line button by default has description 'Microsoft Office Signature Line...'.
19        Dim signatureLine As DrawingElement = document.GetChildElements(True).OfType(Of DrawingElement)().FirstOrDefault(
20            Function(de) de.Metadata.Description = "Microsoft Office Signature Line...")
21
22        Dim signature = New Picture(document, Path.Combine(pathToResources, "GemBoxSignature.png"))
23
24        ' Signature in this document will be 1.5 cm right of TopLeft position of signature line 
25        ' and 0.5 cm below of TopLeft position of signature line.
26        signature.Layout = Layout.Floating(
27            New HorizontalPosition(1.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
28            New VerticalPosition(0.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
29            signature.Layout.Size)
30
31        Dim options = New PdfSaveOptions()
32        Dim digitalSignature = options.DigitalSignature
33
34        digitalSignature.CertificatePath = Path.Combine(pathToResources, "GemBoxSampleExplorer.pfx")
35        digitalSignature.CertificatePassword = "GemBoxPassword"
36        ' Placeholder where signature should be visualized.
37        digitalSignature.SignatureLine = signatureLine
38        ' Visual representation of digital signature.
39        digitalSignature.Signature = signature
40
41        document.Save("PDF Digital Signature.pdf", options)
42
43    End Sub
44
45End Module

Check next sample or find out more about GemBox.Document and GemBox Software.