News Category

C#/VB.NET: Add or Remove Digital Signatures in PDF

2022-05-10 08:57:00 Written by  support iceblue
Rate this item
(0 votes)

As PDF documents become increasingly popular in business, ensuring their authenticity has become a key concern. Signing PDFs with a certificate-based signature can protect the content and also let others know who signed or approved the document. In this article, you will learn how to digitally sign PDF with an invisible or a visible signature, and how to remove digital signatures from PDF by using Spire.PDF for .NET.

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 DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Add an Invisible Digital Signature to PDF

The following are the steps to add an invisible digital signature to PDF using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object based on the certificate.
  • Set the document permissions through the PdfSignature object.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;

namespace AddInvisibleSignature
{
    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 
            PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
          
            //Set the document permission to forbid changes but allow form fill
            signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;

            //Save to another PDF file 
            doc.SaveToFile("InvisibleSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Add or Remove Digital Signatures in PDF

Add a Visible Digital Signature to PDF

The following are the steps to add a visible digital signature to PDF using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object and specify its position and size on the document.
  • Set the signature details including date, name, location, reason, handwritten signature image, and document permissions.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Security;
using Spire.Pdf.Graphics;

namespace AddVisibleSignature
{
    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 - 260 - 54 , 200, 260, 110);
            signature.Bounds = rectangleF;
            signature.Certificated = true;

            //Set the graphics mode to ImageAndSignDetail
            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\\handwrittingSignature.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("VisiableSignature.pdf");
            doc.Close();
        }
    }
}

C#/VB.NET: Add or Remove Digital Signatures in PDF

Remove Digital Signatures from PDF

The following are the steps to remove digital signatures from PDF using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Get form widgets from the document through PdfDocument.Form property.
  • Loop through the widgets and determine if a specific widget is a PdfSignatureFieldWidget.
  • Remove the signature widget using PdfFieldCollection.RemoveAt() method.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Widget;

namespace RemoveSignature
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument("C:\\Users\\Administrator\\Desktop\\VisiableSignature.pdf");

            //Get form widgets from the document
            PdfFormWidget widgets = doc.Form as PdfFormWidget;

            //Loop through the widgets
            for (int i = 0; i < widgets.FieldsWidget.List.Count; i++)
            {
                //Get the specific widget
                PdfFieldWidget widget = widgets.FieldsWidget.List[i] as PdfFieldWidget;

                //Determine if the widget is a PdfSignatureFieldWidget
                if (widget is PdfSignatureFieldWidget)
                {
                    //Remove the widget
                    widgets.FieldsWidget.RemoveAt(i);
                }
            }

            //Save the document to another PDF file
            doc.SaveToFile("RemoveSignatures.pdf");
        }
    }
}

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:48