|
| 1 | +--- |
| 2 | +title: Verifying If Digital Signatures Exist in PDF Documents |
| 3 | +description: Learn how to check for digital signatures in a PDF document and retrieve their signing dates using RadPdfProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Verify Digital Signatures and Retrieve Signing Dates in PDFs with RadPdfProcessing |
| 6 | +slug: verify-digital-signatures-radpdfprocessing |
| 7 | +tags: pdfprocessing, document, processing, digital, signature, verification, sign, date |
| 8 | +res_type: kb |
| 9 | +ticketid: 1659606 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.2.426| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +When working with PDF documents, it might be necessary to verify whether the document is digitally signed. This includes checking if one or more digital signatures exist and determining the dates they were signed. This KB article provides guidance on achieving this with the RadPdfProcessing library. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To verify digital signatures in a PDF document and extract their signing dates, follow the steps below: |
| 25 | + |
| 26 | +1. Use the `PdfFormatProvider` to import the PDF document into a `RadFixedDocument`. |
| 27 | + |
| 28 | +2. Check if the document is digitally signed by searching for `SignatureField` objects in the `AcroForm` of the document. |
| 29 | + |
| 30 | +3. For each `SignatureField` found, access the `Signature` property and then the `Properties` to retrieve the `TimeOfSigning`. |
| 31 | + |
| 32 | +Here is a code snippet demonstrating these steps and including the creation of a document with digital signature as well: |
| 33 | + |
| 34 | +```csharp |
| 35 | + static void Main(string[] args) |
| 36 | + { |
| 37 | + |
| 38 | + PdfFormatProvider provider = new PdfFormatProvider(); |
| 39 | + RadFixedDocument document = provider.Import(File.ReadAllBytes("unsigned.pdf")); |
| 40 | + |
| 41 | + bool isSigned = CheckSignedDocument(document); |
| 42 | + Debug.WriteLine(isSigned.ToString()); |
| 43 | + FormSource formSource = new FormSource(); |
| 44 | + formSource.Size = new Size(420, 150); |
| 45 | + |
| 46 | + X509Certificate2 certificate = new X509Certificate2("JohnDoe.pfx", "johndoe"); |
| 47 | + SignatureField signatureField = document.AcroForm.FormFields.Where(f => f.FieldType == FormFieldType.Signature).FirstOrDefault() as SignatureField; |
| 48 | + if (signatureField != null) |
| 49 | + { |
| 50 | + signatureField.Signature = new Signature(certificate); |
| 51 | + SignatureWidget widget = signatureField.Widgets.FirstOrDefault(); |
| 52 | + if (widget != null) |
| 53 | + { |
| 54 | + formSource = widget.Content.NormalContentSource; |
| 55 | + FixedContentEditor ed = new FixedContentEditor(formSource); |
| 56 | + ed.TextProperties.FontSize = 60; |
| 57 | + ed.Position.Translate(30, 0); |
| 58 | + ed.DrawText("John Doe"); |
| 59 | + ed.Position.Translate(0, 90); |
| 60 | + ed.TextProperties.FontSize = 20; |
| 61 | + ed.DrawText("Digitally signed on: " + DateTime.Now.ToString()); |
| 62 | + ed.Position.Translate(40, 120); |
| 63 | + ed.TextProperties.FontSize = 20; |
| 64 | + ed.DrawText("(Click here to view the signature info)"); |
| 65 | + } |
| 66 | + |
| 67 | + document.Pages[0].Annotations.Add(widget); |
| 68 | + |
| 69 | + string signedDocumentFilePath = "Signed.pdf"; |
| 70 | + File.Delete(signedDocumentFilePath); |
| 71 | + using (Stream output = new FileStream(signedDocumentFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) |
| 72 | + { |
| 73 | + provider.Export(document, output); |
| 74 | + } |
| 75 | + |
| 76 | + isSigned = CheckSignedDocument(document); |
| 77 | + Debug.WriteLine(isSigned.ToString()); |
| 78 | + Process.Start(new ProcessStartInfo() { FileName = signedDocumentFilePath, UseShellExecute = true }); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static bool CheckSignedDocument(RadFixedDocument document) |
| 83 | + { |
| 84 | + bool isSigned = false; |
| 85 | + var signatureFields = document.AcroForm.FormFields.Where(field => field.FieldType == FormFieldType.Signature).ToList(); |
| 86 | + if (signatureFields!=null) |
| 87 | + { |
| 88 | + foreach (var signatureField in signatureFields) |
| 89 | + { |
| 90 | + SignatureField field = (SignatureField)signatureField; |
| 91 | + |
| 92 | + if (field != null && field.Signature != null) |
| 93 | + { |
| 94 | + if (field.Signature==null) |
| 95 | + { |
| 96 | + isSigned = false; |
| 97 | + break; |
| 98 | + } |
| 99 | + SignatureDataProperties properties = field.Signature.Properties; |
| 100 | + |
| 101 | + Debug.WriteLine("Signed on: "+properties.TimeOfSigning.ToString()); |
| 102 | + isSigned = true; |
| 103 | + break; |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return isSigned; |
| 109 | + } |
| 110 | +``` |
| 111 | + |
| 112 | +To validate a signature, utilize the `Validate()` or `TryValidate()` methods available within the RadPdfProcessing library. Detailed information on signature validation can be found in the [Signature Validation](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/features/digital-signature/signature-validation) documentation. |
| 113 | + |
| 114 | +## See Also |
| 115 | + |
| 116 | +- [Digital Signature Overview in RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/features/digital-signature/overview) |
| 117 | +- [Signature Validation in RadPdfProcessing](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/features/digital-signature/signature-validation) |
0 commit comments