2X faster development
The ultimate ASP.NET Core UI toolkit to boost your development speed.
Syncfusion Essential PDF is a .NET Core PDF library used to create, read, and edit PDF documents. Using this library, you can apply one or more digital signature to a PDF document using C# and VB.NET. Steps to apply one or more digital signature to PDF document programmatically:
C# using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using Syncfusion.Pdf.Graphics;
VB.NET Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Security Imports Syncfusion.Pdf.Graphics
C# //Load the PDF document FileStream docStream = new FileStream("SignatureFields.pdf", FileMode.Open, FileAccess.Read); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); //Gets the first page of the document PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; //Gets the first signature field of the PDF document PdfLoadedSignatureField signatureField1 = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; //Creates a certificate FileStream certificateStream1 = new FileStream("PDF.pfx", FileMode.Open, FileAccess.Read); PdfCertificate certificate1 = new PdfCertificate(certificateStream1, "syncfusion"); signatureField1.Signature = new PdfSignature(loadedDocument, page, certificate1, "Signature", signatureField1); FileStream imageStream = new FileStream("Student Signature.jpg", FileMode.Open, FileAccess.Read); //Draw image PdfBitmap signatureImage = new PdfBitmap(imageStream); signatureField1.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage, 0, 0, 90, 20); //Save the document into stream MemoryStream stream = new MemoryStream(); loadedDocument.Save(stream); //Load the signed PDF document PdfLoadedDocument signedDocument = new PdfLoadedDocument(stream); //Load the PDF page PdfLoadedPage loadedPage = signedDocument.Pages[0] as PdfLoadedPage; //Gets the first signature field of the PDF document PdfLoadedSignatureField signatureField2 = signedDocument.Form.Fields[1] as PdfLoadedSignatureField; signatureField1.Signature = new PdfSignature(signedDocument, loadedPage, certificate1, "Signature", signatureField2); FileStream imageStream1 = new FileStream("Teacher Signature.png", FileMode.Open, FileAccess.Read); PdfBitmap signatureImage1 = new PdfBitmap(imageStream1); //Draw image signatureField1.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage1, 0, 0, 90, 20); //Saving the PDF to the MemoryStream MemoryStream signedStream = new MemoryStream(); signedDocument.Save(signedStream); //Set the position as '0'. signedStream.Position = 0; //Download the PDF document in the browser FileStreamResult fileStreamResult = new FileStreamResult(signedStream, "application/pdf"); fileStreamResult.FileDownloadName = "DigitalSignatureSample.pdf"; return fileStreamResult;
VB.NET 'Load the PDF document Dim docStream As FileStream = New FileStream("SignatureFields.pdf", FileMode.Open, FileAccess.Read) Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(docStream) 'Gets the first page of the document Dim page As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage) 'Gets the first page of the document Dim signatureField1 As PdfLoadedSignatureField = TryCast(loadedDocument.Form.Fields(0), PdfLoadedSignatureField) 'Creates a certificate Dim certificateStream1 As FileStream = New FileStream("PDF.pfx", FileMode.Open, FileAccess.Read) Dim certificate1 As PdfCertificate = New PdfCertificate(certificateStream1, "syncfusion") signatureField1.Signature = New PdfSignature(loadedDocument, page, certificate1, "Signature", signatureField1) Dim imageStream As FileStream = New FileStream("Student Signature.jpg", FileMode.Open, FileAccess.Read) 'Draw image Dim signatureImage As PdfBitmap = New PdfBitmap(imageStream) signatureField1.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage, 0, 0, 90, 20) 'Save the document into stream Dim stream As MemoryStream = New MemoryStream() loadedDocument.Save(stream) 'Load the signed PDF document Dim signedDocument As PdfLoadedDocument = New PdfLoadedDocument(stream) 'Load the PDF page Dim loadedPage As PdfLoadedPage = TryCast(signedDocument.Pages(0), PdfLoadedPage) Dim signatureField2 As PdfLoadedSignatureField = TryCast(signedDocument.Form.Fields(1), PdfLoadedSignatureField) signatureField1.Signature = New PdfSignature(signedDocument, loadedPage, certificate1, "Signature", signatureField2) Dim imageStream1 As FileStream = New FileStream("Teacher Signature.png", FileMode.Open, FileAccess.Read) 'Draw image Dim signatureImage1 As PdfBitmap = New PdfBitmap(imageStream1) signatureField1.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage1, 0, 0, 90, 20) 'Saving the PDF to the MemoryStream Dim signedStream As MemoryStream = New MemoryStream() signedDocument.Save(signedStream) 'Set the position as '0'. signedStream.Position = 0 'Download the PDF document in the browser Dim fileStreamResult As FileStreamResult = New FileStreamResult(signedStream, "application/pdf") fileStreamResult.FileDownloadName = "DigitalSignatureSample.pdf" Return fileStreamResult
A complete working sample can be downloaded from DigitalSignatureSample.zip. By executing the program, you will get the PDF document as follows. Take a moment to peruse the documentation, where you can find other options like adding a digital signature using stream, signing an existing document, adding a timestamp in digital signature and features like redacting PDF document and protect PDF document with code examples. An online sample link to digitally sign a PDF document. Refer here to learn more about digitally sign PDF files of Syncfusion Essential PDF. Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
|
2X faster development
The ultimate ASP.NET Core UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.
Hi,
This is not working.Signature validity is unknown, Document has not been modified since this signature was applied.
Hi Faisal,
We have analyzed the reported issue “One digital signature becomes invalid while adding multiple digital signature to PDF document” and suspect that the issue may occurs due to using of Trial License. Because while using the trial license, the watermark will be added to the resultant PDF and this will invalidate the existing signature. So kindly run the sample using licensed assemblies and this will results the valid signature document.
Regards,
Sowmiya Loganathan