Search code examples
digital-signaturehtml-to-pdf

Digitally signing a PDF document resulted after HTML to PDF conversion


I'm using the HTML to PDF converter tool from Winnovative to convert a HTML to a PDF with a table of contents. I did this using the code below:

// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

// Enable the creation of a table of contents from H1 to H6 tags found in HTML
htmlToPdfConverter.TableOfContentsOptions.AutoTocItemsEnabled = autoTableOfContentsCheckBox.Checked;

// Convert the HTML page to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtmlToPdf(myBookHtml, baseUrl);

This works very well but the requirement is to also sign the PDF document which will be distributed as part of our software using a digital certificate I can export from our IIS server. How can I do this?


Solution

  • First, from IIS you have to export a certificate containing both private and public keys in a password protected PFX file. The you can use to file to sign the created PDF document using the following code:

    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
    
    htmlToPdfConverter.TableOfContentsOptions.AutoTocItemsEnabled = true;
    
    Document pdfDocument = null;
    try
    {
        string htmlWithDigitalSignatureMarker = htmlStringTextBox.Text;
        string baseUrl = baseUrlTextBox.Text;
    
        // Convert a HTML string with a marker for digital signature to a PDF document object
        pdfDocument = htmlToPdfConverter.ConvertHtmlToPdfDocumentObject(htmlWithDigitalSignatureMarker, baseUrl);
    
        // Make the HTML element with 'digital_signature_element' mapping ID a link to digital signature properties
        HtmlElementMapping digitalSignatureMapping = htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult.GetElementByMappingId("digital_signature_element");
        if (digitalSignatureMapping != null)
        {
            PdfPage digitalSignaturePage = digitalSignatureMapping.PdfRectangles[0].PdfPage;
            RectangleF digitalSignatureRectangle = digitalSignatureMapping.PdfRectangles[0].Rectangle;
    
            string certificateFilePath = Server.MapPath("~/DemoAppFiles/Input/Certificates/wnvpdf.pfx");
    
            // Get the certificate from password protected PFX file
            DigitalCertificatesCollection certificates = DigitalCertificatesStore.GetCertificates(certificateFilePath, "wnvpdf");
            DigitalCertificate certificate = certificates[0];
    
            // Create the digital signature
            DigitalSignatureElement signature = new DigitalSignatureElement(digitalSignatureRectangle, certificate);
            signature.Reason = "Protect the document from unwanted changes";
            signature.ContactInfo = "The contact email is support@winnovative-software.com";  
            signature.Location = "Development server";
            digitalSignaturePage.AddElement(signature);
        }
    
        // Save the PDF document in a memory buffer
        byte[] outPdfBuffer = pdfDocument.Save();
    
    }
    finally
    {
        // Close the PDF document
        if (pdfDocument != null)
            pdfDocument.Close();
    }