How to sign PDF document from local installed certificate in WinForms PDF?
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can sign PDF document from the locally installed certificate using C# and VB.NET.
Steps to sign PDF document from locally installed certificate programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Security; using System.Security.Cryptography.X509Certificates;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Security Imports System.Security.Cryptography.X509Certificates
- Use the following code snippet in the click event of the Get Certificates button to get locally installed certificates.
C#
private void GetCertificates(object sender, EventArgs e) { TxtMy.Text = ""; TxtCa.Text = ""; TxtRoot.Text = ""; TxtSpc.Text = ""; PdfCertificate _certificado = null; string type = null; foreach (PdfCertificate cert in GetPDFCertificates()) { _certificado = PdfCertificate.FindByIssuer(StoreType.MY, cert.IssuerName); if (_certificado == null) { _certificado = PdfCertificate.FindByIssuer(StoreType.CA, cert.IssuerName); if (_certificado == null) { _certificado = PdfCertificate.FindByIssuer(StoreType.ROOT, cert.IssuerName); if (_certificado == null) { _certificado = PdfCertificate.FindByIssuer(StoreType.SPC, cert.IssuerName); if ((_certificado != null)) { type = "SPC"; } } else { type = "ROOT"; } } else { type = "CA"; } } else { type = "MY"; } switch (type) { case "MY": TxtMy.Text = TxtMy.Text + cert.SubjectName + "\n"; break; case "CA": TxtCa.Text = TxtCa.Text + cert.SubjectName + "\n"; break; case "ROOT": TxtRoot.Text = TxtRoot.Text + cert.SubjectName + "\n"; break; case "SPC": TxtSpc.Text = TxtSpc.Text + cert.SubjectName + "\n"; break; } } } //Method to get all the PdfCertificate list public PdfCertificate[] GetPDFCertificates() { //Get the certificate collection List<PdfCertificate> certCollection = new List<PdfCertificate>(); GetStoreCertificates(StoreName.My, certCollection); GetStoreCertificates(StoreName.CertificateAuthority, certCollection); GetStoreCertificates(StoreName.Root, certCollection); return certCollection.ToArray(); } private void GetStoreCertificates(StoreName name, List<PdfCertificate> certList) { //Set the store location to local machine X509Store store = new X509Store(name, StoreLocation.LocalMachine); store.Open(OpenFlags.MaxAllowed); foreach (X509Certificate2 x509Cert in store.Certificates) { //PdfCertificate constructor to load the X509Certificate directly PdfCertificate cert = new PdfCertificate(x509Cert); certList.Add(cert); } }
VB.NET
Private Sub GetCertificates(ByVal sender As Object, ByVal e As System.EventArgs) TxtMy.Text = "" TxtCa.Text = "" TxtRoot.Text = "" TxtSpc.Text = "" Dim _certificado As PdfCertificate = Nothing Dim type As String = Nothing For Each cert As PdfCertificate In GetPDFCertificates() _certificado = PdfCertificate.FindByIssuer(StoreType.MY, cert.IssuerName) If _certificado Is Nothing Then _certificado = PdfCertificate.FindByIssuer(StoreType.CA, cert.IssuerName) If _certificado Is Nothing Then _certificado = PdfCertificate.FindByIssuer(StoreType.ROOT, cert.IssuerName) If _certificado Is Nothing Then _certificado = PdfCertificate.FindByIssuer(StoreType.SPC, cert.IssuerName) If (_certificado IsNot Nothing) Then type = "SPC" End If Else type = "ROOT" End If Else type = "CA" End If Else type = "MY" End If Select Case type Case "MY" TxtMy.Text = TxtMy.Text + cert.SubjectName & vbLf Case "CA" TxtCa.Text = TxtCa.Text + cert.SubjectName & vbLf Case "ROOT" TxtRoot.Text = TxtRoot.Text + cert.SubjectName & vbLf Case "SPC" TxtSpc.Text = TxtSpc.Text + cert.SubjectName & vbLf End Select Next End Sub Public Function GetPDFCertificates() As PdfCertificate() Dim certCollection As List(Of PdfCertificate) = New List(Of PdfCertificate)() GetStoreCertificates(StoreName.My, certCollection) GetStoreCertificates(StoreName.CertificateAuthority, certCollection) GetStoreCertificates(StoreName.Root, certCollection) Return certCollection.ToArray() End Function Private Sub GetStoreCertificates(ByVal name As StoreName, ByVal certList As List(Of PdfCertificate)) 'Set the store location to local machine Dim store As X509Store = New X509Store(name, StoreLocation.LocalMachine) store.Open(OpenFlags.MaxAllowed) For Each x509Cert As X509Certificate2 In store.Certificates 'PdfCertificate constructor to load the X509Certificate directly Dim cert As PdfCertificate = New PdfCertificate(x509Cert) certList.Add(cert) Next End Sub
- Use the following code in the event of the GeneratePDF button to sign PDF document from locally installed certificate.
C#
//Create a new PDF document PdfDocument document = new PdfDocument(); //Create a page PdfPage page = document.Pages.Add(); // Creating Certificate //LOCAL PdfCertificate pdfCert = PdfCertificate.FindBySubject(StoreType.ROOT, txtSub.Text); MessageBox.Show("Serial:" + pdfCert.IssuerName + "\n" + "Subject:" + pdfCert.SubjectName + "\n" + "Valid to:" + pdfCert.ValidTo); //Create the graphics from the page PdfGraphics graphics = page.Graphics; //Creates a digital signature PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature 1"); //Load an image from disk PdfBitmap bmp = new PdfBitmap("syncfusion_logo.gif"); //Sets the signature information signature.Bounds = new RectangleF(new PointF(5, 5), new SizeF(bmp.Width, bmp.Height + 100)); signature.ContactInfo = "johndoe@owned.us"; signature.LocationInfo = "Honolulu, Hawaii"; signature.Reason = "I am author of this document."; string validto = "Valid To: " + signature.Certificate.ValidTo.ToString(); string validfrom = "Valid From: " + signature.Certificate.ValidFrom.ToString(); //Set default appearance to form if (document.Form != null) document.Form.SetDefaultAppearance(false); PdfSolidBrush brush = new PdfSolidBrush(new PdfColor(1, 1, 255)); PdfPen pen = new PdfPen(brush, 1); PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 12, PdfFontStyle.Regular); //Create graphics from signature appearance PdfGraphics g = signature.Appearence.Normal.Graphics; //Draw image g.DrawImage(bmp, 0, 0); //Draw string g.DrawString(validfrom, font, pen, brush, 0,bmp.Height); g.DrawString(validto, font, pen, brush, 0, bmp.Height+50); //Save the document document.Save("Signature.pdf"); //Close the document document.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Signature.pdf");
VB.NET
'Create a new PDF document Dim document As PdfDocument = New PdfDocument() 'Create a page Dim page As PdfPage = document.Pages.Add() 'Creating Certificate 'LOCAL Dim pdfCert As PdfCertificate = PdfCertificate.FindBySubject(StoreType.ROOT, txtSub.Text) MessageBox.Show("Serial:" & pdfCert.IssuerName & vbLf & "Subject:" + pdfCert.SubjectName & vbLf & "Valid to:" + pdfCert.ValidTo) 'Create the graphics from the page Dim graphics As PdfGraphics = page.Graphics 'Creates a digital signature Dim signature As PdfSignature = New PdfSignature(document, page, pdfCert, "Signature 1") 'Load an image from disk Dim bmp As PdfBitmap = New PdfBitmap("syncfusion_logo.gif") 'Sets the signature information signature.Bounds = New RectangleF(New PointF(5, 5), New SizeF(bmp.Width, bmp.Height + 100)) signature.ContactInfo = "johndoe@owned.us" signature.LocationInfo = "Honolulu, Hawaii" signature.Reason = "I am author of this document." Dim validto As String = "Valid To: " & signature.Certificate.ValidTo.ToString() Dim validfrom As String = "Valid From: " & signature.Certificate.ValidFrom.ToString() 'Set default appearance to form If document.Form IsNot Nothing Then document.Form.SetDefaultAppearance(False) Dim brush As PdfSolidBrush = New PdfSolidBrush(New PdfColor(1, 1, 255)) Dim pen As PdfPen = New PdfPen(brush, 1) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Courier, 12, PdfFontStyle.Regular) 'Create graphics from signature appearance Dim g As PdfGraphics = signature.Appearence.Normal.Graphics 'Draw image g.DrawImage(bmp, 0, 0) 'Draw string g.DrawString(validfrom, font, pen, brush, 0, bmp.Height) g.DrawString(validto, font, pen, brush, 0, bmp.Height + 50) 'Save the document document.Save("Signature.pdf") 'Close the document document.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Signature.pdf")
A complete working sample can be downloaded from DigitalSignatureSample.zip.
Run the application and click Get Certificates button to get the list of certificates.
Copy the name showed in the text box and paste it to the Subject field.
After that you will get the output PDF document as follows,
Take a moment to peruse the documentation, where you can find options like adding a digital signature (load the certificate from disk) in PDF, singing an existing PDF document, adding a signature validation appearance based on the signature, adding a timestamp in digital signature and features like redacting PDF document and protect PDF document with code examples.
Refer here to learn more about PDF digital signature and timestamp in Essential® PDF.
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.
Conclusion
I hope you enjoyed learning about how to sign PDF document from local installed certificate in WinForms PDF.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!