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!
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:
Thank you very much, I will try it!
Thanks for the update. Please check and let us know, if you have any concerns.