Protect PDF Via Digital Signature Using Spire.PDF

Background

Data security becomes more and more important and essential when the cloud service and Internet are widely used in every aspect of our daily work, especially for the most common used file formats, such as PDF, Word, Excel and PowerPoint documents that may contain confidential context or intellectual property. In this way, we need to take some effective measures to protect those documents. There are some system-level security solutions for the document database, while rarely solutions for documents themselves. And we know that we can encrypt the PDF files with the password to protect the PDF file, but the password can be decrypted easily. This article will focus on introducing a better solution with higher security level namely securing PDF via PDF digital signature using a Free Spire.PDF for .NET. With the digital signature, we can confirm the identity of the author, and ensure the integrity of the transmission process. And that’s why digital signature has become a popular way to protect PDF file from editing and tampering.

In this tutorial, I focus on introducing PDF visible digital signature with the following parts:

  1. Create a visible digital signature to PDF.
  2. Get and verify the digital signature.

Preparation

If you are interested and want to try the PDF digital signature in C#, download free Spire.PDF on E-iceblue website or Nuget and install it. It is simple and clean. When you finish with the installation, you can go to "SampleCenter" and API Help, which gives you a brief and clear information for the usage of free Spire.PDF

Tools we need

Visual Studio

Spire.PDF has a bin folder, which contains .NET2.0, .NET3.5, .NET4.0 and NET4.0 ClientProfile. Please add the correct .dll based on your projects. Here I used .NET 4.0



Figure 1: Spire PDF

Part 1: C# code snippet to create visible digital signature

Step 1: Load a PDF file and certificate.

  1. PdfDocument doc = new PdfDocument();  
  2. doc.LoadFromFile("test.pdf");  
  3. PdfCertificate cert = new PdfCertificate("Demo.pfx""e-iceblue");  
Step 2: Create a signature and set its position.
  1. var signature = new PdfSignature(doc, doc.Pages[0], cert, "Requestd1");  
  2. signature.Bounds = new RectangleF(new PointF(280, 600), new SizeF(260, 90));
Step 3: Set the property for displaying the signature.
  1. signature.IsTag = true;
Step 4: Fill the contents of the signature.
  1. signature.DigitalSignerLable = "Digitally signed by";  
  2. signature.DigitalSigner = "Harry Hu for Test";  
  3.   
  4. signature.DistinguishedName = "DN:";  
  5. signature.LocationInfoLabel = "Location:";  
  6. signature.LocationInfo = "London";  
  7.   
  8. signature.ReasonLabel = "Reason: ";  
  9. signature.Reason = "Le document est certifie";  
  10.   
  11. signature.DateLabel = "Date: ";  
  12. signature.Date = DateTime.Now;  
  13.   
  14. signature.ContactInfoLabel = "Contact: ";  
  15. signature.ContactInfo = "123456789";  
  16.   
  17. signature.Certificated = false
Step 5: Set the document permission of the signature.
  1. signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;  
Step 6: Save the document to file in PDF format.
  1. doc.SaveToFile("sample.pdf");
Please check the effective screenshot of the digital signature visible.



Figure 2: Digital Signature

Part 2: Getting and verifying the digital signature in PDF

Step 1: Load a PDF file with digital signature that created in part 1.
  1. string filename = "sample.pdf";
Step 2: Get all the signatures from the PDF.
  1. List < PdfSignature > signatures = new List < PdfSignature > ();  
  2. doc.LoadFromFile(filename);  
  3. var form = (PdfFormWidget) doc.Form;  
  4. for (int i = 0; i < form.FieldsWidget.Count; ++i)   
  5. {  
  6.     var field = form.FieldsWidget[i] as PdfSignatureFieldWidget;  
  7.     if (field != null && field.Signature != null)   
  8.     {  
  9.         PdfSignature signature1 = field.Signature;  
  10.         signatures.Add(signature);  
  11.     }  
  12. }
Step 3: Get the first signature.
  1. PdfSignature signatureOne = signatures[0];  
  2. Verify the signature: try   
  3. {  
  4.     bool bSignature = signatureOne.VerifySignature();  
  5. }   
  6. catch (Exception ex)   
  7. {  
  8.     Console.WriteLine(ex.Message);  
  9.     Console.ReadLine();  
  10. }
Step 4: Get the certification of signature.
  1. X509Certificate2 certificate = signatureOne.Certificate as X509Certificate2;
Step 5: Get the date of signature.
  1. DateTime date = signatureOne.Date;
Step 6: Get the date on which the signature starts and ends valid.
  1. DateTime validStart = certificate.NotBefore;  
  2. DateTime validEnd = certificate.NotAfter;
Step 7: Get the version of the certificate.
  1. int version = certificate.Version;  
Step 8: Get the subject distinguished name from the certificate.
  1. string subject = certificate.Subject;
Please check the effective screenshot;.



Figure 3: Screenshot

Conclusion

By now, we have set the digital signature for PDF to increase its security level using free Spire.PDF. This free version meets the limited demand of my project. It also supports other features such as converting HTML, RTF, XPS, Text and Image to PDF. If your project is not that big and you need a quick solution, you could try it.

Thank you for reading and I hope this article helps.