HiQPdf HTML to PDF Converter allows you to add digital signatures to the generated PDF document using a certificate that contains both 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.NextPdfDigitalSignature object. An object of this type is exposed by the PdfDocumentControlDigitalSignature property, while an object of the PdfDocumentControl class is exposed by the Document property of the HiQPdf.NextHtmlToPdf class.
The digital signature can have an optional appearance on a page of the generated PDF document, which may include an image and text.
The digital signature also encapsulates information such as the reason, location and contact details. This information is displayed in the Signatures panel in Adobe Reader and can also be displayed in the signature appearance in the PDF when the signature text is not set.
The digital signatures created by HiQPdf HTML to PDF Converter conform to the PAdES (PDF Advanced Electronic Signatures) standard. Two PAdES profiles are supported: PAdES-BASELINE-B and PAdES-BASELINE-T. The PAdES-BASELINE-B profile represents a basic signature that embeds the signing certificate and the signature value, providing the integrity and authenticity of the signed PDF document. The PAdES-BASELINE-T profile additionally embeds a trusted timestamp obtained from a Time Stamping Authority (TSA), which provides a reliable proof of the time when the document was signed. To produce a PAdES-BASELINE-T signature, set the PdfDigitalSignatureTimestampServerUrl property with the URL of a certified timestamp server, as shown in the code sample below. When this property is not set, the resulting signature conforms to the PAdES-BASELINE-B profile.
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class DigitalSignaturesController : Controller
{
IWebHostEnvironment m_hostingEnvironment;
public DigitalSignaturesController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
// GET: DigitalSignatures
public ActionResult Index()
{
var model = new DigitalSignaturesViewModel();
return View(model);
}
[HttpPost]
public ActionResult CreatePdf(DigitalSignaturesViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Set the serial number received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.SerialNumber = "your-serial-number";
// Create an HTML to PDF converter object with default settings
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
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
htmlToPdfConverter.Document.DigitalSignature.Reason = model.SignatureReason;
htmlToPdfConverter.Document.DigitalSignature.Location = model.SignatureLocation;
htmlToPdfConverter.Document.DigitalSignature.ContactInfo = model.SignatureContact;
// Uncomment the line below to optionally set a timestamp from a certified timestamp server
//htmlToPdfConverter.Document.DigitalSignature.TimestampServerUrl = "http://tsa.belgium.be/connect";
// enable or disable digital signature appearance in PDF page
htmlToPdfConverter.Document.DigitalSignature.AppearanceEnabled = model.EnableAppearance;
if (htmlToPdfConverter.Document.DigitalSignature.AppearanceEnabled)
{
// set digital signature appearance position in PDF document
if (model.DisplayOnLastPage)
{
// 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 (model.DisplayText)
htmlToPdfConverter.Document.DigitalSignature.Appearance.Text = model.SignatureText;
// set an image for signature appearance
if (model.DisplayImage)
{
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 = model.Url;
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "PdfDigitalSignature.pdf";
return fileResult;
}
}
}