PDF Digital Signatures

HiQPdf HTML to PDF Converter allows you to add digital signatures to the generated PDF document using a certificate with private and public keys. These certificates are stored in PFX files in PKCS#12 format.

The PDF digital signature properties are encapsulated in a HiQPdf.ChromiumPdfDigitalSignature object. An object of this type is exposed by the PdfDocumentControlDigitalSignature property and an object of PdfDocumentControl class is exposed by the Document property of the HiQPdf.ChromiumHtmlToPdf class.

The digital signature can have an optional appearance in a page of the generated PDF document which can contain an image and a text.

The digital signature encapsulates also information about the reason, location or contact details. This information is displayed in the signatures panel in Adobe Reader and also it can be displayed in the signature appearance in PDF when the signature text was not set.

PDF Digital Signature Demo Source Code

C#
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;

using HiQPdf.Chromium;

namespace HiQPdf_Chrome_AspNetDemo.Controllers
{
    public class DigitalSignaturesController : Controller
    {
        IWebHostEnvironment m_hostingEnvironment;
        public DigitalSignaturesController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        // GET: DigitalSignatures
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePdf(IFormCollection collection)
        {
            // create the HTML to PDF converter
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // set a demo serial number
            htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            string certificateFilePath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoFiles/Pfx/hiqpdf.pfx";
            htmlToPdfConverter.Document.DigitalSignature = new PdfDigitalSignature(certificateFilePath, "hiqpdf");

            // optionally set digital signature field name
            htmlToPdfConverter.Document.DigitalSignature.FieldName = "HiQPdf Signature Field";

            // set digital signature information
            // the information is also used in appearance if a custom text is not set
            htmlToPdfConverter.Document.DigitalSignature.Reason = collection["textBoxSignatureReason"];
            htmlToPdfConverter.Document.DigitalSignature.Location = collection["textBoxSignatureLocation"];
            htmlToPdfConverter.Document.DigitalSignature.ContactInfo = collection["textBoxSignatureContact"];

            // enable or disable digital signature appearance in PDF page
            htmlToPdfConverter.Document.DigitalSignature.AppearanceEnabled = collection["checkBoxEnableAppearance"].Count > 0;

            if (htmlToPdfConverter.Document.DigitalSignature.AppearanceEnabled)
            {
                // set digital signature appearance position in PDF document
                if (collection["checkBoxDisplayOnLastPage"].Count > 0)
                {
                    // display the appearance at the bottom of the last page
                    htmlToPdfConverter.Document.DigitalSignature.Appearance.DisplayOnLastPage = true;
                    htmlToPdfConverter.Document.DigitalSignature.Appearance.BoundingRectangle =
                            new PdfRectangle(0, htmlToPdfConverter.Document.PageSize.Height - 50, 200, 50);

                    // optionally set PDF document top margin to make the appearance more visible
                    htmlToPdfConverter.Document.Margins.Bottom = 50;
                }
                else
                {
                    // display the appearance on first page
                    htmlToPdfConverter.Document.DigitalSignature.Appearance.PageNumber = 1;
                    htmlToPdfConverter.Document.DigitalSignature.Appearance.BoundingRectangle = new PdfRectangle(0, 0, 200, 50);

                    // optionally set PDF document top margin to make the appearance more visible
                    htmlToPdfConverter.Document.Margins.Top = 50;
                }

                // set a custom text for signature appearance
                // set the appearance text to null or empty string to display the signature information
                if (collection["checkBoxDisplayText"].Count > 0)
                    htmlToPdfConverter.Document.DigitalSignature.Appearance.Text = collection["textBoxSignatureText"];

                // set an image for signature appearance
                if (collection["checkBoxDisplayImage"].Count > 0)
                {
                    string imageFilePath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoFiles/Images/HiQPdfBanner.png";
                    htmlToPdfConverter.Document.DigitalSignature.Appearance.SetImage(imageFilePath, true);
                }
            }

            // convert URL to a PDF memory buffer
            string url = collection["textBoxUrl"];

            byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);

            FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "PdfDigitalSignature.pdf";

            return fileResult;
        }
    }
}

See Also