Verifying If Digital Signatures Exist in PDF Documents
Environment
| Version | Product | Author |
|---|---|---|
| 2024.2.426 | RadPdfProcessing | Desislava Yordanova |
Description
When working with PDF documents, you may need 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 article shows how to achieve this with the RadPdfProcessing library.
Solution
To verify digital signatures in a PDF document and extract their signing dates, follow the steps below:
-
Use the PdfFormatProvider to import the PDF document into a
RadFixedDocument. -
Check if the document is digitally signed by searching for
SignatureFieldobjects in theAcroFormof the document. -
For each
SignatureFieldfound, access theSignatureproperty and then thePropertiesto retrieve theTimeOfSigning.
Here is a code snippet that demonstrates these steps and includes creating a document with a digital signature:
static void Main(string[] args)
{
PdfFormatProvider provider = new PdfFormatProvider();
RadFixedDocument document = provider.Import(File.ReadAllBytes("unsigned.pdf"));
bool isSigned = CheckSignedDocument(document);
Debug.WriteLine(isSigned.ToString());
FormSource formSource = new FormSource();
formSource.Size = new Size(420, 150);
X509Certificate2 certificate = new X509Certificate2("JohnDoe.pfx", "johndoe");
SignatureField signatureField = document.AcroForm.FormFields.Where(f => f.FieldType == FormFieldType.Signature).FirstOrDefault() as SignatureField;
if (signatureField != null)
{
signatureField.Signature = new Signature(certificate);
SignatureWidget widget = signatureField.Widgets.FirstOrDefault();
if (widget != null)
{
formSource = widget.Content.NormalContentSource;
FixedContentEditor ed = new FixedContentEditor(formSource);
ed.TextProperties.FontSize = 60;
ed.Position.Translate(30, 0);
ed.DrawText("John Doe");
ed.Position.Translate(0, 90);
ed.TextProperties.FontSize = 20;
ed.DrawText("Digitally signed on: " + DateTime.Now.ToString());
ed.Position.Translate(40, 120);
ed.TextProperties.FontSize = 20;
ed.DrawText("(Click here to view the signature info)");
}
document.Pages[0].Annotations.Add(widget);
string signedDocumentFilePath = "Signed.pdf";
File.Delete(signedDocumentFilePath);
using (Stream output = new FileStream(signedDocumentFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
provider.Export(document, output);
}
isSigned = CheckSignedDocument(document);
Debug.WriteLine(isSigned.ToString());
Process.Start(new ProcessStartInfo() { FileName = signedDocumentFilePath, UseShellExecute = true });
}
}
private static bool CheckSignedDocument(RadFixedDocument document)
{
bool isSigned = false;
var signatureFields = document.AcroForm.FormFields.Where(field => field.FieldType == FormFieldType.Signature).ToList();
if (signatureFields!=null)
{
foreach (var signatureField in signatureFields)
{
SignatureField field = (SignatureField)signatureField;
if (field != null && field.Signature != null)
{
if (field.Signature==null)
{
isSigned = false;
break;
}
SignatureDataProperties properties = field.Signature.Properties;
Debug.WriteLine("Signed on: "+properties.TimeOfSigning.ToString());
isSigned = true;
break;
}
}
}
return isSigned;
}
To validate a signature, use the Validate() or TryValidate() methods available in the RadPdfProcessing library. For more information, see the Signature Validation documentation.