Create digitally signed PDF in C# and VB.NET

Digital signatures can be used for many types of documents where traditional pen-and-ink signatures were used in the past. However, the mere existence of a digital signature is not adequate assurance that a document is what it appears to be. Moreover, government and enterprise settings often need to impose additional constraints on their signature workflows, such as restricting user choices and document behavior during and after signing.

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

Certificate file: sautinsoft.pfx.
Signature file: signature.png.

First of all, you need to point an existing document (*.docx, *.rtf, *.pdf, *.html, *.txt, etc) which you want to sign by digital signature:

               // Path to a loadable document.
            string loadPath = @"..\..\digitalsignature.docx";
If you want, you may to add in your document a picture, symbolizes a handwritten signature.
// Signature line added with MS Word -> Insert tab -> Signature Line button by default has description 'Microsoft Office Signature Line...'.
            ShapeBase signatureLine = document.GetChildElements(true).OfType<ShapeBase>().FirstOrDefault();
            // This picture symbolizes a handwritten signature
            Picture signature = new Picture(document, @"..\..\signature.png");
Signature in this document will be 4.5 cm right of TopLeft position of signature line and 4.5 cm below of TopLeft position of signature line.

                signature.Layout = Layout.Floating(
                new HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
                new VerticalPosition(-4.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
                signature.Layout.Size);

And the last step is to point of a certificate (*.pfx) and its characteristics: Password of the certificate, Contact Info, etc.

                    // Path to the certificate (*.pfx).
                    CertificatePath = @"..\..\sautinsoft.pfx",
                    // Password of the certificate.
                    CertificatePassword = "123456789",
                    // Additional information about the certificate.
                    Location = "World Wide Web",
                    Reason = "Document.Net by SautiSoft",
                    ContactInfo = "info@sautinsoft.ru",
                    // Placeholder where signature should be visualized.
                    SignatureLine = signatureLine,
                    // Visual representation of digital signature.
                    Signature = signature

As the result, you will get the digitally signed PDF document.

Complete code

using System.IO;
using System.Linq;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
namespace Sample
{
    class Sample
    {
        
        static void Main(string[] args)
        {
            DigitalSignature();
        }

        // Load an existing document (*.docx, *.rtf, *.pdf, *.html, *.txt, *.pdf) and save it in a PDF document with the digital signature.
        public static void DigitalSignature()
        {

            // Path to a loadable document.
            string loadPath = @"..\..\digitalsignature.docx";

            DocumentCore dc = DocumentCore.Load(loadPath);

            // Signature line added with MS Word -> Insert tab -> Signature Line button by default has description 'Microsoft Office Signature Line...'.
            ShapeBase signatureLine = dc.GetChildElements(true).OfType<ShapeBase>().FirstOrDefault();
            
            // This picture symbolizes a handwritten signature
            Picture signature = new Picture(dc, @"..\..\signature.png");

            // Signature in this document will be 4.5 cm right of TopLeft position of signature line
            // and 4.5 cm below of TopLeft position of signature line.
            signature.Layout = Layout.Floating(
               new HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
               new VerticalPosition(-4.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
               signature.Layout.Size);

            //signature.Layout = Layout.Inline(signature.Layout.Size);
            PdfSaveOptions options = new PdfSaveOptions();

            // Path to the certificate (*.pfx).
            options.DigitalSignature.CertificatePath = @"..\..\sautinsoft.pfx";

            // Password of the certificate.
            options.DigitalSignature.CertificatePassword = "123456789";

            // Additional information about the certificate.
            options.DigitalSignature.Location = "World Wide Web";
            options.DigitalSignature.Reason = "Document.Net by SautiSoft";
            options.DigitalSignature.ContactInfo = "info@sautinsoft.ru";

            // Placeholder where signature should be visualized.
            options.DigitalSignature.SignatureLine = signatureLine;

            // Visual representation of digital signature.
            options.DigitalSignature.Signature = signature;

            string savePath = Path.ChangeExtension(loadPath, ".pdf");
            dc.Save(savePath, options);

            // Open the result for demonstation purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });
        }
    }
}

Download.

        
            Imports System.IO
Imports System.Linq
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Namespace Sample
    Friend Class Sample
        Shared Sub Main(ByVal args() As String)
            DigitalSignature()
        End Sub

        ' Load an existing document (*.docx, *.rtf, *.pdf, *.html, *.txt, *.pdf) and save it in a PDF document with the digital signature.
        Public Shared Sub DigitalSignature()

            ' Path to a loadable document.
            Dim loadPath As String = "..\digitalsignature.docx"

            Dim dc As DocumentCore = DocumentCore.Load(loadPath)

            ' Signature line added with MS Word -> Insert tab -> Signature Line button by default has description 'Microsoft Office Signature Line...'.
            Dim signatureLine As ShapeBase = dc.GetChildElements(True).OfType(Of ShapeBase)().FirstOrDefault()
            ' This picture symbolizes a handwritten signature
            Dim signature As New Picture(dc, "..\signature.png")

            ' Signature in this document will be 4.5 cm right of TopLeft position of signature line
            ' and 4.5 cm below of TopLeft position of signature line.
            signature.Layout = Layout.Floating(New HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page), New VerticalPosition(-4.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page), signature.Layout.Size)

            'signature.Layout = Layout.Inline(signature.Layout.Size);
            Dim options As New PdfSaveOptions()

            ' Path to the certificate (*.pfx).
            options.DigitalSignature.CertificatePath = "..\sautinsoft.pfx"

            ' Password of the certificate.
            options.DigitalSignature.CertificatePassword = "123456789"

            ' Additional information about the certificate.
            options.DigitalSignature.Location = "World Wide Web"
            options.DigitalSignature.Reason = "Document.Net by SautiSoft"
            options.DigitalSignature.ContactInfo = "info@sautinsoft.ru"

            ' Placeholder where signature should be visualized.
            options.DigitalSignature.SignatureLine = signatureLine

            ' Visual representation of digital signature.
            options.DigitalSignature.Signature = signature

            Dim savePath As String = Path.ChangeExtension(loadPath, ".pdf")
            dc.Save(savePath, options)

            ' Open the result for demonstation purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(savePath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download.

© SautinSoft 2019