DigitalSignature Sign Existing PDF

This example demonstrates how to add a digital signature to an existing PDF file.

Source PDF file:
Certificate file:
Password:
Reason:
Contact:
Location:

To sign an existing pdf document identifies the person signing a document. Unlike a handwritten signature, a digital signature is difficult to forge because it contains encrypted information that is unique to the signer and easily verified. It is usually password protected and can be stored on your computer in PKCS #12 file format (Personal Information Exchange File, PFX)

Source allow you to choose a pdf doucument that has to be signed

Certificate file - Choose the certificate file

Password - If certificate file is password protected

Signature properties like

  • Reason - add the reason on signature
  • Contact - add contact details like email
  • Location - add the location of signature

Clicking generate button will launch pdf with above added digital signature

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;
using OfficeComponent.Pdf.Security;

namespace OfficeComponent.Samples
{
    class DigitalSignatureSignExistingPDFExample : ExampleBase
#if WEB
, IUIExample
#endif
    {
        #region Input

        public string SourceFile { get; set; }
        public string Cert { get; set; }
        public string Password { get; set; }
        public string Reason { get; set; }
        public string Contact { get; set; }
        public string Location { get; set; }

        #endregion

        public override string Execute()
        {            
#if WEB
            ProcessForm();
#endif

            PdfDigitalSignature signature;

            PdfBitmap bmp;

            if (string.IsNullOrWhiteSpace(SourceFile) || string.IsNullOrWhiteSpace(Cert) || !SourceFile.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) || !Cert.EndsWith(".pfx", StringComparison.OrdinalIgnoreCase))
            {
                ShowError("Please select a PDF document to sign, along with certificate and the password");
                return null;
            }

            PdfCertificate pdfCert;
            try
            {
                pdfCert = new PdfCertificate(Cert, Password);
                using (PdfImportedDocument doc = new PdfImportedDocument(SourceFile))
                {
                    bmp = new PdfBitmap(CommonDataPath + "\\Logo.gif");

                    PdfPageBase page = doc.Pages[0];
                    signature = new PdfDigitalSignature(doc, page, pdfCert, "Signature");

                    signature.Bounds = new RectangleF(new PointF(5, 5), bmp.PhysicalDimension);
                    signature.ContactInfo = Contact;
                    signature.LocationInfo = Location;
                    signature.Reason = Reason;


                    // Save the PDF file.
                    var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
                    doc.Save(outputPath);
                    doc.Close(true);

                    return outputPath;
                }
            }
            catch (Exception ex)
            {
                ShowError("Error adding digital signature: " + ex.Message);
                return null;
            }

        }

        public override string ActionTitle
        {
            get
            {
                return "Sign Document";
            }
        }

#if WEB
        void ProcessForm()
        {
            SourceFile = GetPostFile("SourceFile");
            Cert = GetPostFile("Cert");
        }
#endif

        public DigitalSignatureSignExistingPDFExample()
            : base(null, null)
        {

        }

        public DigitalSignatureSignExistingPDFExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public DigitalSignatureSignExistingPDFExample(string commonDataPath, string outputDir, string xmlFile)
            : base(commonDataPath, outputDir, xmlFile)
        {

        }
    }

}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics
Imports OfficeComponent.Pdf.Security

Namespace OfficeComponent.Samples
#If WEB Then
	Friend Class DigitalSignatureSignExistingPDFExample
		Inherits ExampleBase
		Implements IUIExample
#Else
	Friend Class DigitalSignatureSignExistingPDFExample
		Inherits ExampleBase
#End If
		#Region "Input"

		Private privateSourceFile As String
		Public Property SourceFile() As String
			Get
				Return privateSourceFile
			End Get
			Set(ByVal value As String)
				privateSourceFile = value
			End Set
		End Property
		Private privateCert As String
		Public Property Cert() As String
			Get
				Return privateCert
			End Get
			Set(ByVal value As String)
				privateCert = value
			End Set
		End Property
		Private privatePassword As String
		Public Property Password() As String
			Get
				Return privatePassword
			End Get
			Set(ByVal value As String)
				privatePassword = value
			End Set
		End Property
		Private privateReason As String
		Public Property Reason() As String
			Get
				Return privateReason
			End Get
			Set(ByVal value As String)
				privateReason = value
			End Set
		End Property
		Private privateContact As String
		Public Property Contact() As String
			Get
				Return privateContact
			End Get
			Set(ByVal value As String)
				privateContact = value
			End Set
		End Property
		Private privateLocation As String
		Public Property Location() As String
			Get
				Return privateLocation
			End Get
			Set(ByVal value As String)
				privateLocation = value
			End Set
		End Property

		#End Region

		Public Overrides Function Execute() As String
#If WEB Then
			ProcessForm()
#End If

			Dim signature As PdfDigitalSignature

			Dim bmp As PdfBitmap

			If String.IsNullOrWhiteSpace(SourceFile) OrElse String.IsNullOrWhiteSpace(Cert) OrElse (Not SourceFile.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)) OrElse (Not Cert.EndsWith(".pfx", StringComparison.OrdinalIgnoreCase)) Then
				ShowError("Please select a PDF document to sign, along with certificate and the password")
				Return Nothing
			End If

			Dim pdfCert As PdfCertificate
			Try
				pdfCert = New PdfCertificate(Cert, Password)
				Using doc As New PdfImportedDocument(SourceFile)
					bmp = New PdfBitmap(CommonDataPath & "\Logo.gif")

					Dim page As PdfPageBase = doc.Pages(0)
					signature = New PdfDigitalSignature(doc, page, pdfCert, "Signature")

					signature.Bounds = New RectangleF(New PointF(5, 5), bmp.PhysicalDimension)
					signature.ContactInfo = Contact
					signature.LocationInfo = Location
					signature.Reason = Reason


					' Save the PDF file.
					Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
					doc.Save(outputPath)
					doc.Close(True)

					Return outputPath
				End Using
			Catch ex As Exception
				ShowError("Error adding digital signature: " & ex.Message)
				Return Nothing
			End Try

		End Function

		Public Overrides ReadOnly Property ActionTitle() As String
			Get
				Return "Sign Document"
			End Get
		End Property

#If WEB Then
		Private Sub ProcessForm()
			SourceFile = GetPostFile("SourceFile")
			Cert = GetPostFile("Cert")
		End Sub
#End If

		Public Sub New()
			MyBase.New(Nothing, Nothing)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub
	End Class

End Namespace