C# / vb. Net to add visual and invisible digital signatures to PDF

Time:2022-5-23

This paper shows how to add visual digital signature and invisible digital signature to PDF documents through c# program code. Visual digital signature, that is, add a signature at the specified page position in the PDF document, including relevant text information and signed pictures; Invisible digital signature, that is, when adding a signature, the signature position is not specified in the document, and the relevant digital signature information is not displayed in the document body.

The signature here is throughPdfOrdinarySignatureMakerClass to implement the signature. When adding a visual signature, you canIPdfSignatureAppearanceClass to set the specific location of the signature, signature image, etc.

The following are the specific implementation methods and steps for reference.

1. Install spire through nuget in vs program PDF for . Net version 7.8.2,The installation steps are as follows:

Right click “reference” and “manage nuget package”

Click “Browse”, enter “spire. PDF” in the search box, click to select the searched package, and click “Install”.

Click “OK” in the pop-up window

Wait for the installation to complete. The installation results are shown in the figure:

 

2. Add the following code in vs program and add digital signature

2.1 add visual digital signature

  C#

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Drawing;
using System.Security.Cryptography.X509Certificates;

namespace VisibleDigitalSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load PDF test document
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("test.pdf");

            //Get page 5 in PDF document
            PdfPageBase page = pdf.Pages[4];

            //Add a visual signature at the specified location on the page
            X509Certificate2 x509 = new X509Certificate2("cer.pfx", "e-iceblue");
            PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, x509);
            IPdfSignatureAppearance signatureAppearance = new PdfCustomSignatureAppearance();            
            signatureMaker.MakeSignature("signName_" , page, page.ActualSize.Width-240,page.ActualSize.Height-200,150,150, signatureAppearance);

            //Save document
            pdf.SaveToFile("VisibleDigitalSignature.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("VisibleDigitalSignature.pdf");
            pdf.Close();   
            
        }
        public class PdfCustomSignatureAppearance : IPdfSignatureAppearance
        {
            public void Generate(PdfCanvas g)
            {
                float fontSize = 15;
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", fontSize), true);
                float lineHeight = fontSize; g.DrawString("Sign Here:", font, PdfBrushes.Black, new PointF(0, 0));
                g.DrawImage(PdfImage.FromFile("sign.png"), new PointF(20, 20));
            }
        }

    }
}

Vb.net

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Interactive.DigitalSignatures
Imports System.Drawing
Imports System.Security.Cryptography.X509Certificates

Namespace VisibleDigitalSignature
    Class Program
        Private Shared Sub Main(args As String())
            'test document load pdf
            Dim pdf As New PdfDocument()
            pdf.LoadFromFile("test.pdf")

            'get page 5 in PDF document
            Dim page As PdfPageBase = pdf.Pages(4)

            'add a visual signature at the specified location on the page
            Dim x509 As New X509Certificate2("cer.pfx", "e-iceblue")
            Dim signatureMaker As New PdfOrdinarySignatureMaker(pdf, x509)
            Dim signatureAppearance As IPdfSignatureAppearance = New PdfCustomSignatureAppearance()
            signatureMaker.MakeSignature("signName_", page, page.ActualSize.Width - 240, page.ActualSize.Height - 200, 150, 150, _
                signatureAppearance)

            'save document
            pdf.SaveToFile("VisibleDigitalSignature.pdf", FileFormat.PDF)
            System.Diagnostics.Process.Start("VisibleDigitalSignature.pdf")
            pdf.Close()

        End Sub
        Public Class PdfCustomSignatureAppearance
            Implements IPdfSignatureAppearance
            Public Sub Generate(g As PdfCanvas)
                Dim fontSize As Single = 15
                Dim font As New PdfTrueTypeFont(New Font("Arial", fontSize), True)
                Dim lineHeight As Single = fontSize
                g.DrawString("Sign Here:", font, PdfBrushes.Black, New PointF(0, 0))
                g.DrawImage(PdfImage.FromFile("sign.png"), New PointF(20, 20))
            End Sub
        End Class

    End Class
End Namespace

Visual digital signature effect:

 

2.2 add invisible digital signature

    C#

using Spire.Pdf;
using Spire.Pdf.Interactive.DigitalSignatures;
using System.Security.Cryptography.X509Certificates;

namespace InvisibleDigitalSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a pdfdocument instance
            PdfDocument pdf = new PdfDocument();

            //Load PDF document
            pdf.LoadFromFile("test.pdf");

            //Add signature
            X509Certificate2 x509 = new X509Certificate2("cer.pfx", "e-iceblue");
            PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(pdf, x509);
            signatureMaker.MakeSignature("signName");
            signatureMaker.SetLocation("Chengdu");
            signatureMaker.SetReason("Edit limited");            
           
            //Save document
            pdf.SaveToFile("result.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

Vb.net

Imports Spire.Pdf
Imports Spire.Pdf.Interactive.DigitalSignatures
Imports System.Security.Cryptography.X509Certificates

Namespace InvisibleDigitalSignature
    Class Program
        Private Shared Sub Main(args As String())
            'initialize a pdfdocument instance
            Dim pdf As New PdfDocument()

            'load PDF document
            pdf.LoadFromFile("test.pdf")

            'add signature
            Dim x509 As New X509Certificate2("cer.pfx", "e-iceblue")
            Dim signatureMaker As New PdfOrdinarySignatureMaker(pdf, x509)
            signatureMaker.MakeSignature("signName")
            signatureMaker.SetLocation("Chengdu")
            signatureMaker.SetReason("Edit limited")

            'save document
            pdf.SaveToFile("result.pdf", FileFormat.PDF)
            System.Diagnostics.Process.Start("result.pdf")
        End Sub
    End Class
End Namespace

Invisible digital signature effect:

 

—End—