-
Notifications
You must be signed in to change notification settings - Fork 41
Added new kb article verify-digital-signatures-radpdfprocessing #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dessyordanova
merged 2 commits into
master
from
new-kb-verify-digital-signatures-radpdfprocessing-d6ad5a4241384dbeb612daf35a0edfcd
Jul 31, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
knowledge-base/verify-digital-signatures-radpdfprocessing.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| --- | ||
| title: Verifying If Digital Signatures Exist in PDF Documents | ||
| description: Learn how to check for digital signatures in a PDF document and retrieve their signing dates using RadPdfProcessing. | ||
| type: how-to | ||
| page_title: How to Verify Digital Signatures and Retrieve Signing Dates in PDFs with RadPdfProcessing | ||
| slug: verify-digital-signatures-radpdfprocessing | ||
| tags: pdfprocessing, document, processing, digital, signature, verification, sign, date | ||
| res_type: kb | ||
| ticketid: 1659606 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| | Version | Product | Author | | ||
| | --- | --- | ---- | | ||
| | 2024.2.426| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| | ||
|
|
||
| ## Description | ||
|
|
||
| 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. | ||
|
|
||
| ## Solution | ||
|
|
||
| To verify digital signatures in a PDF document and extract their signing dates, follow the steps below: | ||
|
|
||
| 1. Use the [PdfFormatProvider]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to import the PDF document into a `RadFixedDocument`. | ||
|
|
||
| 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. | ||
|
|
||
| 3. For each `SignatureField` found, access the `Signature` property and then the `Properties` to retrieve the `TimeOfSigning`. | ||
|
|
||
| Here is a code snippet demonstrating these steps and including the creation of a document with digital signature as well: | ||
|
|
||
| ```csharp | ||
| 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); | ||
dessyordanova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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; | ||
|
|
||
dessyordanova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
dessyordanova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return isSigned; | ||
| } | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Digital Signature Overview in RadPdfProcessing]({%slug radpdfprocessing-features-digital-signature%}) | ||
| - [Signature Validation]({%slug radpdfprocessing-features-signature-validation%}) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.