Create digitally signed PDF in C# and VB.NET

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.

Source file: digitalsignature.docx.
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";
Create a new invisible Shape for the digital signature. Place the Shape into top-left corner (0 mm, 0 mm) of page.
            Shape signatureShape = new Shape(dc, Layout.Floating(new HorizontalPosition(0f, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin),
                                    new VerticalPosition(0f, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), new Size(1, 1)));
            ((FloatingLayout)signatureShape.Layout).WrappingStyle = WrappingStyle.InFrontOfText;
            signatureShape.Outline.Fill.SetEmpty();

            // Find a first paragraph and insert our Shape inside it.
            Paragraph firstPar = dc.GetChildElements(true).OfType<Paragraph>().FirstOrDefault();
            firstPar.Inlines.Add(signatureShape);
Add a handwritten picture for our digital signature and specify the position on the page:
            // Picture which symbolizes a handwritten signature.
            Picture signaturePict = new Picture(dc, @"..\..\signature.png");

            // Signature picture will be positioned:
            // 14.5 cm from Top of the Shape.
            // 4.5 cm from Left of the Shape.
            signaturePict.Layout = Layout.Floating(
               new HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
               new VerticalPosition(14.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
               new Size(20, 10, LengthUnit.Millimeter));
And the last step is to point of a certificate (*.pfx) and its characteristics: Password of the certificate, Contact Info, etc.

            PdfSaveOptions options = new PdfSaveOptions();

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

            // The password for the certificate.
            // Each certificate is protected by a password.
            // The reason is to prevent unauthorized the using of the certificate.
            options.DigitalSignature.CertificatePassword = "123456789";

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

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

            // Visual representation of digital signature.
            options.DigitalSignature.Signature = signaturePict;
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();
        }

        /// <summary>
        /// Load an existing document (*.docx, *.rtf, *.pdf, *.html, *.txt, *.pdf) and save it in a PDF document with the digital signature.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/digital-signature-net-csharp-vb.php
        /// </remarks>
        public static void DigitalSignature()
        {
            // Path to a loadable document.
            string loadPath = @"..\..\digitalsignature.docx";
            string savePath = "Result.pdf";

            DocumentCore dc = DocumentCore.Load(loadPath);

            // Create a new invisible Shape for the digital signature.       
            // Place the Shape into top-left corner (0 mm, 0 mm) of page.
            Shape signatureShape = new Shape(dc, Layout.Floating(new HorizontalPosition(0f, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin),
                                    new VerticalPosition(0f, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), new Size(1, 1)));
            ((FloatingLayout)signatureShape.Layout).WrappingStyle = WrappingStyle.InFrontOfText;
            signatureShape.Outline.Fill.SetEmpty();

            // Find a first paragraph and insert our Shape inside it.
            Paragraph firstPar = dc.GetChildElements(true).OfType<Paragraph>().FirstOrDefault();
            firstPar.Inlines.Add(signatureShape);

            // Picture which symbolizes a handwritten signature.
            Picture signaturePict = new Picture(dc, @"..\..\signature.png");

            // Signature picture will be positioned:
            // 14.5 cm from Top of the Shape.
            // 4.5 cm from Left of the Shape.
            signaturePict.Layout = Layout.Floating(
               new HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
               new VerticalPosition(14.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
               new Size(20, 10, LengthUnit.Millimeter));

            PdfSaveOptions options = new PdfSaveOptions();

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

            // The password for the certificate.
            // Each certificate is protected by a password.
            // The reason is to prevent unauthorized the using of the certificate.
            options.DigitalSignature.CertificatePassword = "123456789";

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

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

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

            dc.Save(savePath, options);

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

Download.

        
            Imports System
Imports System.Linq
Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Module Sample
    Sub Main()
        DigitalSignature()
    End Sub

    ''' <summary>
    ''' Load an existing document (*.docx, *.rtf, *.pdf, *.html, *.txt, *.pdf) and save it in a PDF document with the digital signature.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/digital-signature-net-csharp-vb.php
    ''' </remarks>
    Sub DigitalSignature()
        ' Path to a loadable document.
        Dim loadPath As String = "..\digitalsignature.docx"
        Dim savePath As String = "Result.pdf"

        Dim dc As DocumentCore = DocumentCore.Load(loadPath)

        ' Create a new invisible Shape for the digital signature.       
        ' Place the Shape into top-left corner (0 mm, 0 mm) of page.
        Dim signatureShape As New Shape(dc, Layout.Floating(New HorizontalPosition(0F, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin), New VerticalPosition(0F, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), New Size(1, 1)))
        CType(signatureShape.Layout, FloatingLayout).WrappingStyle = WrappingStyle.InFrontOfText
        signatureShape.Outline.Fill.SetEmpty()

        ' Find a first paragraph and insert our Shape inside it.
        Dim firstPar As Paragraph = dc.GetChildElements(True).OfType(Of Paragraph)().FirstOrDefault()
        firstPar.Inlines.Add(signatureShape)

        ' Picture which symbolizes a handwritten signature.
        Dim signaturePict As New Picture(dc, "..\signature.png")

        ' Signature picture will be positioned:
        ' 14.5 cm from Top of the Shape.
        ' 4.5 cm from Left of the Shape.
        signaturePict.Layout = Layout.Floating(New HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page), New VerticalPosition(14.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page), New Size(20, 10, LengthUnit.Millimeter))

        Dim options As New PdfSaveOptions()

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

        ' The password for the certificate.
        ' Each certificate is protected by a password.
        ' The reason is to prevent unauthorized the using of the certificate.
        options.DigitalSignature.CertificatePassword = "123456789"

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

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

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

        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 Module

Download.

© SautinSoft 2002 - 2018