Skip to content

Commit 2f20604

Browse files
Merge pull request #449 from telerik/new-kb-verify-digital-signatures-radpdfprocessing-d6ad5a4241384dbeb612daf35a0edfcd
Added new kb article verify-digital-signatures-radpdfprocessing
2 parents 439792f + 8680c38 commit 2f20604

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

knowledge-base/signing-a-document-with-digital-signature.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,8 @@ However, there are other approaches provided by the **.Net Framework API** that
9494

9595
{{endregion}}
9696

97+
## See Also
98+
99+
* [How to Create Invisible Signatures for PDF Documents]({%slug pdf-invisible-signatures%})
100+
* [Verifying If Digital Signatures Exist in PDF Documents]({%slug verify-digital-signatures-radpdfprocessing%})
101+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to import the PDF document into a `RadFixedDocument`.
27+
28+
2. Check if the document is [digitally signed]({%slug radpdfprocessing-features-digital-signature%}) by searching for [SignatureField]({%slug radpdfprocessing-model-interactive-forms-form-fields-signaturefield%}) objects in the [AcroForm]({%slug radpdfprocessing-model-interactive-forms-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]({%slug radpdfprocessing-features-signature-validation%}) documentation.
113+
114+
## See Also
115+
116+
- [Digital Signature Overview in RadPdfProcessing]({%slug radpdfprocessing-features-digital-signature%})
117+
- [Signature Validation]({%slug radpdfprocessing-features-signature-validation%})

libraries/radpdfprocessing/features/digital-signature/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ The following example shows a full code snippet for a simple signing of a newly
8787
* [Widgets Types]({%slug radpdfprocessing-model-annotations-widgets%})
8888
* [How to Create Invisible Signatures for PDF Documents]({%slug pdf-invisible-signatures%})
8989
* [Signing a PDF Document with a SignatureWidget]({%slug sign-pdf-with-signature-widget%})
90+
* [Verifying If Digital Signatures Exist in PDF Documents]({%slug verify-digital-signatures-radpdfprocessing%})

0 commit comments

Comments
 (0)