HiQPdf Documentation

Digital Signatures

HiQPdf Client for .NET Core

HiQPdf library allows you to digitally sign the generated PDF documents using a digital certificate. You can create a HiQPdfClientPdfDigitalSignature object that you can add to a PDF document using the PdfDocumentLayout(PdfObject) method.

PDF Digital Signatures Demo

In this demo you can learn how to add a digital signature to a PDF document based on a certificate from a PFX file. The digital signature is applied over an image in PDF document and you can click on that image to see the digital certificate properties.

Demo Source Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;

using HiQPdfClient;
using Microsoft.AspNetCore.Hosting.Server;

namespace HiQPdf_Demo.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)
        {
            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string serverPassword = collection["textBoxServerPassword"];

            // create a PDF document
            PdfDocument document = new PdfDocument(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                document.ServerPassword = serverPassword;

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

            // create a page in document
            PdfPage page1 = document.AddPage();

            // create the true type fonts that can be used in document text
            PdfFont pdfFontEmbed = document.CreateFontFromName("Times New Roman", 10, true);

            float crtYPos = 20;
            float crtXPos = 5;

            // add a title to PDF document
            PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos,
                    "Click the image below to open the digital signature", pdfFontEmbed);
            titleTextTransImage.ForeColor = PdfColor.Navy;
            page1.Layout(titleTextTransImage);

            crtYPos += pdfFontEmbed.Size + 10;

            // layout a PNG image with alpha transparency
            PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, m_hostingEnvironment.WebRootPath +"/DemoFiles/Images/HiQPdfLogo_small.png");
            page1.Layout(transparentPdfImage);

            // apply a digital sgnature over the image
            PdfDigitalSignature digitalSignature = new PdfDigitalSignature(m_hostingEnvironment.WebRootPath + "/DemoFiles/Pfx/hiqpdf.pfx", "hiqpdf", 0);
            digitalSignature.SigningReason = "My signing reason";
            digitalSignature.SigningLocation = "My signing location";
            digitalSignature.SignerContactInfo = "My contact info";
            document.Layout(digitalSignature);

            try
            {
                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

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

                return fileResult;
            }
            finally
            {
                document.Close();
            }
        }
    }
}
See Also

Other Resources