Securing PDFs with digital signatures is essential for ensuring the integrity and non-repudiation of the documents. With this in mind, the ability to verify the digital signatures is equally important. A valid signature means that the document hasn't been altered since it was signed and that it is indeed originated from the claimed source.
While dealing with the digital signatures, there are also times when you may want to get the certificates of the signatures to learn its issuer Information, subject information, serial number, and validity period, etc. In this article, you will learn how to verify or get the digital signatures in PDF in C# using Spire.PDF for .NET.
- Verify Digital Signatures in PDF Using C#
- Detect Whether a Signed PDF Has Been Modified Using C#
- Get the Certificates of Digital Signatures in PDF Using C#
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Verify Digital Signatures in PDF Using C#
Spire.PDF for .NET provides the PdfSignature.VerifySignature() method to check the validity of the digital signatures in a PDF document directly. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get the form in the PDF file using PdfDocument.Form property, and then get a collection of form fields using PdfFormWidget.FieldsWidget property.
- Iterate through all fields and get the signature fields.
- Get PDF signatures using PdfSignatureFieldWidget.Signature property.
- Check the validity of the PDF signatures using PdfSignature.VerifySignature() method.
- C#
using Spire.Pdf; using Spire.Pdf.Security; using Spire.Pdf.Widget; namespace GetSignatureCertificate { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a PDF file pdf.LoadFromFile("PDFSignature.pdf"); //Get a collection of form fields in the PDF file PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form; PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget; //Iterate through all fields for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++) { //Get the signature fields if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget) { PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i]; //Get the signatures PdfSignature signature = signatureFieldWidget.Signature; //Verify signatures bool valid = signature.VerifySignature(); if (valid) { Console.WriteLine("Valid signatures"); } else { Console.WriteLine("Invalid signatures"); } } } } } }
Detect Whether a Signed PDF Has Been Modified Using C#
To verify if a PDF document has been modified after signing, you can use the PdfSignature.VerifyDocModified() method. If the result shows that document has been tampered with, this means that the signature will become invalid and the integrity of the document will be compromised. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the form in the PDF document using PdfDocument.Form property, and then get a collection of form fields using PdfFormWidget.FieldsWidget property.
- Iterate through all fields and get the signature fields.
- Get PDF signatures using PdfSignatureFieldWidget.Signature property.
- Verify if the document has been modified after signing using PdfSignature.VerifyDocModified() method.
- C#
using Spire.Pdf; using Spire.Pdf.Security; using Spire.Pdf.Widget; namespace GetSignatureCertificate { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.LoadFromFile("PDFSignature.pdf"); //Get a collection of form fields in the PDF file PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form; PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget; for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++) { //Get the signature fields if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget) { PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i]; //Get the signatures PdfSignature signature = signatureFieldWidget.Signature; //Check if the document has been modified after signing bool modified = signature.VerifyDocModified(); if (modified) { Console.WriteLine("The document has been modified."); } else { Console.WriteLine("The document has not been modified."); } } } } } }
Get the Certificates of Digital Signatures in PDF Using C#
The digital certificates used to sign PDF files typically contain various pieces of information that verifies the identity of the issuer. With Spire.PDF for .NET, you can get the certificates in a PDF file through the PdfSignatureFieldWidget.Signature.Certificate property. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the form in the PDF document using PdfDocument.Form property, and then get a collection of form fields using PdfFormWidget.FieldsWidget property.
- Iterate through all fields and get the signature fields.
- Get the certificate of the signature using PdfSignatureFieldWidget.Signature.Certificate property.
- Set to display the certificate in text format using PdfCertificate.ToString() method.
- Get the format of the certificate using PdfCertificate.GetFormat() method.
- Output the obtained certificate information.
- C#
using Spire.Pdf; using Spire.Pdf.Widget; namespace GetSignatureCertificate { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a PDF file pdf.LoadFromFile("PDFSignature.pdf"); //Get a collection of form fields in the PDF file PdfFormWidget pdfFormWidget = (PdfFormWidget)pdf.Form; PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.FieldsWidget; //Iterate through all fields for (int i = 0; i < pdfFormFieldWidgetCollection.Count; i++) { //Get the signature fields if (pdfFormFieldWidgetCollection[i] is PdfSignatureFieldWidget) { PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget)pdfFormFieldWidgetCollection[i]; //Get the certificate of the signature string certificateInfo = signatureFieldWidget.Signature.Certificate.ToString(); //Get the format of the certificate string format = signatureFieldWidget.Signature.Certificate.GetFormat(); //Output the certificate information Console.WriteLine(certificateInfo + "\n" + "[CertificateFormat]\n " + format); } } Console.ReadKey(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.