EVO HTML to PDF Converter allows you to add digital signatures to the generated PDF document. In order to add digital signatures you need a certificate with private and public keys. These certificates are usually stored in a .pfx or a .p12 file in PKCS#12 format and they can be password protected. A digital signature is represented by an object of the DigitalSignatureElement type which can be placed in any position in a PDF page. Below is a complete example of adding a digital signature to a PDF document.
Code Sample - Adding a Digital Signature to a PDF Document
| C# | |
|---|---|
protected void btnCreatePDF_Click(object sender, EventArgs e) { // create a PDF document Document document = new Document(); // set the license key document.LicenseKey = "B4mYiJubiJiInIaYiJuZhpmahpGRkZE="; // add a page to the PDF document PdfPage firstPage = document.AddPage(); string logoImagePath = System.IO.Path.Combine(Server.MapPath("~"), @"images\evologo-250.png"); string certificateFilePath = System.IO.Path.Combine(Server.MapPath("~"), @"certificates\evopdf.pfx"); PdfFont pdfFont = document.Fonts.Add(new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point)); TextElement descriptionTextElement = new TextElement(0, 0, "A digital signature was applied on the logo image below. Click on the image to see the signature details", pdfFont); AddElementResult addResult = firstPage.AddElement(descriptionTextElement); // create the area where the digital signature will be displayed in the PDF document // in this sample the area is a logo image but it could be anything else ImageElement logoElement = new ImageElement(0, addResult.EndPageBounds.Bottom + 10, 100, logoImagePath); addResult = firstPage.AddElement(logoElement); //get the #PKCS 12 certificate from file DigitalCertificatesCollection certificates = DigitalCertificatesStore.GetCertificates(certificateFilePath, "evopdf"); DigitalCertificate certificate = certificates[0]; // create the digital signature over the logo image element DigitalSignatureElement signature = new DigitalSignatureElement(addResult.EndPageBounds, certificate); signature.Reason = "Protect the document from unwanted changes"; signature.ContactInfo = "The contact email is support@evopdf.com"; signature.Location = "Development server"; firstPage.AddElement(signature); try { // get the PDF document bytes byte[] pdfBytes = document.Save(); // send the generated PDF document to client browser // get the object representing the HTTP response to browser HttpResponse httpResponse = HttpContext.Current.Response; // add the Content-Type and Content-Disposition HTTP headers httpResponse.AddHeader("Content-Type", "application/pdf"); httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename=DigitalSignature.pdf; size={0}", pdfBytes.Length.ToString())); // write the PDF document bytes as attachment to HTTP response httpResponse.BinaryWrite(pdfBytes); // Note: it is important to end the response, otherwise the ASP.NET // web page will render its content to PDF document stream httpResponse.End(); } finally { // close the PDF document to release the resources document.Close(); } } | |