SelectPdf for .NET - Digital Signatures - C# / ASP.NET Sample

Digital signatures can be added to a PDF document using PdfDigitalSignatureElement objects. Adding a digital signature requires a PKCS#12 certificate (provided as a .pfx or a .p12 file).

Sample certificate file:

The following sample shows how to add a digital signature to a pdf document using SelectPdf Library for .NET.

IMPORTANT: Please note that the certificate used is a self signed certificate generated for testing purposes only. The signature will appear as not valid in a pdf viewer. Using a valid certificate will result in a valid digital signature.

Sample Code C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SelectPdf;

namespace SelectPdf.Samples
    public partial class digital_signatures : System.Web.UI.Page
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
            // create a new pdf document
            PdfDocument doc = new PdfDocument();

            // add a new page to the document
            PdfPage page = doc.AddPage();

            // get image path 
            // the image will be used to display the digital signature over it
            string imgFile = Server.MapPath("~/files/logo.png");

            // get certificate path
            string certFile = Server.MapPath("~/files/selectpdf.pfx");

            // define a rendering result object
            PdfRenderingResult result;

            // create image element from file path 
            PdfImageElement img = new PdfImageElement(0, 0, imgFile);
            result = page.Add(img);

            // get the #PKCS12 certificate from file
            PdfDigitalCertificatesCollection certificates = 
                PdfDigitalCertificatesStore.GetCertificates(certFile, "selectpdf");
            PdfDigitalCertificate certificate = certificates[0];

            // create the digital signature object
            PdfDigitalSignatureElement signature = 
                new PdfDigitalSignatureElement(result.PdfPageLastRectangle, certificate);
            signature.Reason = "SelectPdf";
            signature.ContactInfo = "SelectPdf";
            signature.Location = "SelectPdf";

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document