Articles in this section
Category / Section

How to apply one or more digital signatures to a PDF using C# and VB.NET?

5 mins read

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:

  1. Create a new C# ASP.NET Core Web application project. Create a ASP.NET Core Web application project
  2. Select Web application pattern (Model-View-Controller) for the project. Web Application pattern screenshot
  3. Install the Syncfusion.Pdf.Net.Core NuGet package as reference to your .NET Standard application from NuGet.org. NuGet package reference screenshot
  4. A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.

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

 

  1. A default action method named Index will be present in HomeController.cs. Right-click the Index method and select Go To View where you will be directed to its associated view pageIndex.cshtml.
  2. Add a new button in the Index.cshtml as follows.
    <h2>Click the button to generate PDF</h2>
    @using (Html.BeginForm("GeneratePDF", "Home", FormMethod.Post))
    {
        <input type="submit" value="GeneratePDF" />
    }
    

 

  1. Add a new action method GeneratePDF in HomeController.cs and include the following code snippet to create a PDF file and download it.

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;
signatureField2.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
signatureField2.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)
 signatureField2.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)
 signatureField2.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. Output document screenshot

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied