How can I add a digital certificate to the PDF?

Hi!

I have an intranet where users fill out a PDF form and then sign it with their finger in the Signature field. How can I add a digital certificate to the PDF in order to certify that the PDF was created from my intranet and not from somewhere else? (I have the certificate in .pfx format)

Thank you very much!


3 Replies

CK Chinnamunia Karthik Chinna Thambi Syncfusion Team June 30, 2023 12:36 PM UTC

Currently, we do not provide support for adding certificates at the UI level. However, you can accomplish this through code behind. We have shared a code snippet and sample for your reference.


Code snippet:


 

    public IActionResult Download([FromBody] Dictionary<string, string> jsonObject)

    {

        //Initialize the PDF Viewer object with memory cache object

        PdfRenderer pdfviewer = new PdfRenderer(_cache);

        string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);

        string convertedBase = documentBase.Substring(documentBase.LastIndexOf(',') + 1);

        byte[] bytes = Convert.FromBase64String(convertedBase);

        PdfLoadedDocument loadedDocument = new PdfLoadedDocument(bytes);

        PdfPageBase loadedPage = loadedDocument.Pages[0];

        //Get the loaded form.

        PdfLoadedForm loadedForm = loadedDocument.Form;

        var count = loadedDocument.Form.Fields.Count;

        for (var i = 0; i < count; i++)

        {

            //Create PDF Certificate.

            FileStream certificateStream = new FileStream("PDF.pfx", FileMode.Open, FileAccess.Read);

            PdfCertificate certificate = new PdfCertificate(certificateStream, "syncfusion");

            //Load the signature field from field collection and fill this with certificate.

            PdfLoadedSignatureField loadedSignatureField = loadedForm.Fields[i] as PdfLoadedSignatureField;

            if (loadedSignatureField != null)

            {

                loadedSignatureField.Signature = new PdfSignature(loadedDocument, loadedPage, certificate, "Signature", loadedSignatureField);

                loadedSignatureField.Signature.Certificate = certificate;

                loadedSignatureField.Signature.Reason = "Reason";

            }

        }

        //Save the document into stream

        MemoryStream stream = new MemoryStream();

        loadedDocument.Save(stream);

        stream.Position = 0;

        //Close the document

        loadedDocument.Close(true);

        string updatedDocumentBase = Convert.ToBase64String(stream.ToArray());

        return Content("data:application/pdf;base64," + updatedDocumentBase);

    }


Javascript Sample: https://stackblitz.com/edit/d8e8v8-ib6dkd?file=index.js,index.html


Web service sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DigitalSignature139632750.zip


To add a certificate using code behind, please follow these steps:


  • Run the web service sample.
  • And then run the JavaScript sample.
  • Add a signature field.
  • Add the signature to the signature field.
  • Download the document. (When downloading the document using the PDF library, a certificate will be added to the document).
  • By following these steps, the downloaded document will now include a certificate.




PA Pere Argelich Romà replied to Chinnamunia Karthik Chinna Thambi June 30, 2023 01:09 PM UTC

Thank you very much, I will try it!



CK Chinnamunia Karthik Chinna Thambi Syncfusion Team July 3, 2023 05:21 AM UTC

Thanks for the update. Please check and let us know, if you have any concerns.


Loader.
Up arrow icon