News Category

C#/VB.NET: Digitally Sign PDF with Text or/and Image

2021-12-31 06:29:00 Written by  Administrator
Rate this item
(4 votes)

The digital signature ensures that the signed document cannot be altered by anyone other than its author. Adding signatures is the most common way to assure the authenticity of the document content. A visual digital signature in a PDF document can show text or an image (e.g., handwritten signature). This article introduces how to digitally sign PDFs using Spire.PDF for .NET from the following three aspects.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Digitally Sign PDF with Text

The following are the steps to add a plain text signature to a PDF document.

  • Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a .pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object, specifying its position and size in the document.
  • Set the signature graphics mode to SignDetail, which will show signature details in plain text.
  • Set the signature details, including name, contact information, reason, date, etc. through the properties under the PdfSignature object.
  • Set the permissions of the certificated document to ForbidChanges and AllowFormFill.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;
using System;
using System.Drawing;
using Spire.Pdf.Graphics;

namespace AddTextSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

            //Load the certificate 
            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

            //Create a PdfSignature object and specify its position and size 
            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count-1], cert, "MySignature");     
            RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 340, 150, 290, 100);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to sign detail
            signature.GraphicsMode = GraphicMode.SignDetail;

            //Set the signature content 
            signature.NameLabel = "Signer:";
            signature.Name = "Gary";
            signature.ContactInfoLabel = "Phone:";
            signature.ContactInfo = "0123456";
            signature.DateLabel = "Date:";
            signature.Date = DateTime.Now;
            signature.LocationInfoLabel = "Location:";
            signature.LocationInfo = "USA";
            signature.ReasonLabel = "Reason:";
            signature.Reason = "I am the author";
            signature.DistinguishedNameLabel = "DN:";
            signature.DistinguishedName = signature.Certificate.IssuerName.Name;

            //Set the signature font 
            signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",12f,FontStyle.Regular));

            //Set the document permission to forbid changes but allow form fill
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

            //Save to file 
            doc.SaveToFile("TextSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Digitally Sign PDF with Text or/and Image

Digitally Sign PDF with an Image

The following are the steps to add an image signature to a PDF document.

  • Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a .pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object, specifying its position and size in the document.
  • Set the signature graphics mode to SignImageOnly, which will show signature image only.
  • Set the signature image through PdfSignature.SignImageSource property.
  • Set the permissions of the certificated document to ForbidChanges and AllowFormFill.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Security;
using System.Drawing;

namespace AddImageSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

            //Load the certificate 
            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

            //Create a PdfSignature object and specify its position and size 
            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
            RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 200, 150, 130, 130);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to image only
            signature.GraphicsMode = GraphicMode.SignImageOnly;

            //Set the sign image source
            signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\verified.png");

            //Set the signature font 
            signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));

            //Set the document permission to forbid changes but allow form fill
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

            //Save to file 
            doc.SaveToFile("ImageSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Digitally Sign PDF with Text or/and Image

Digitally Sign PDF with Text and an Image

The following are the steps to digitally sign a PDF document with text and an image.

  • Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a .pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object, specifying its position and size in the document.
  • Set the signature graphics mode to SignImageAndSignDetail, which will show both signature image and details.
  • Set the signature image through PdfSignature.SignImageSource property, and set the signature details, including name, contact information, reason, date, etc. through other properties under the PdfSignature object.
  • Set the permissions of the certificated document to ForbidChanges and AllowFormFill.
  • Save the document to another file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;
using System;
using System.Drawing;
using Spire.Pdf.Graphics;

namespace AddTextAndImageSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

            //Load the certificate 
            PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

            //Create a PdfSignature object and specify its position and size 
            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
            RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 320, 150, 260, 110);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to image and sign detail
            signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;

            //Set the signature content 
            signature.NameLabel = "Signer:";
            signature.Name = "Gary";
            signature.ContactInfoLabel = "Phone:";
            signature.ContactInfo = "0123456";
            signature.DateLabel = "Date:";
            signature.Date = DateTime.Now;
            signature.LocationInfoLabel = "Location:";
            signature.LocationInfo = "USA";
            signature.ReasonLabel = "Reason:";
            signature.Reason = "I am the author";
            signature.DistinguishedNameLabel = "DN:";
            signature.DistinguishedName = signature.Certificate.IssuerName.Name;

            //Set the signature image source
            signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handSignature.png");

            //Set the signature font 
            signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));

            //Set the document permission to forbid changes but allow form fill
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

            //Save to file 
            doc.SaveToFile("TextAndImageSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Digitally Sign PDF with Text or/and Image

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 20 June 2023 01:49