|
4 | 4 | using Syncfusion.Pdf.Security; |
5 | 5 | using System.Security.Cryptography.X509Certificates; |
6 | 6 |
|
7 | | -//Get the stream from the document. |
8 | | -FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read); |
| 7 | +// Load the input PDF document stream from the specified file path |
| 8 | +using (FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) |
| 9 | +{ |
9 | 10 |
|
10 | | -//Load an existing signed PDF document. |
11 | | -PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream); |
| 11 | + // Load the signed PDF document using the stream |
| 12 | + using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(documentStream)) |
| 13 | + { |
12 | 14 |
|
13 | | -//Get signature field. |
14 | | -PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; |
| 15 | + // Retrieve the first signature field from the PDF form |
| 16 | + PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField; |
15 | 17 |
|
16 | | -//X509Certificate2Collection to check the signer's identity using root certificates. |
17 | | -X509CertificateCollection collection = new X509CertificateCollection(); |
| 18 | + // Create a certificate collection to hold trusted root certificates for validation |
| 19 | + X509CertificateCollection collection = new X509CertificateCollection(); |
18 | 20 |
|
19 | | -//Creates a certificate instance from PFX file with private key. |
20 | | -FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); |
21 | | -byte[] data = new byte[certificateStream.Length]; |
22 | | -certificateStream.Read(data, 0, data.Length); |
| 21 | + // Load the root certificate from a PFX file (includes private key) |
| 22 | + FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read); |
| 23 | + byte[] data = new byte[certificateStream.Length]; |
| 24 | + certificateStream.Read(data, 0, data.Length); |
23 | 25 |
|
24 | | -//Create new X509Certificate2 with the root certificate. |
25 | | -X509Certificate2 certificate = new X509Certificate2(data, "syncfusion"); |
| 26 | + // Create an X509Certificate2 instance using the loaded certificate data and password |
| 27 | + X509Certificate2 certificate = new X509Certificate2(data, "syncfusion"); |
26 | 28 |
|
27 | | -//Add the certificate to the collection. |
28 | | -collection.Add(certificate); |
| 29 | + // Add the certificate to the validation collection |
| 30 | + collection.Add(certificate); |
29 | 31 |
|
30 | | -//Validate signature and get the validation result. |
31 | | -PdfSignatureValidationResult result = signatureField.ValidateSignature(collection); |
| 32 | + // Validate the signature using the provided certificate collection |
| 33 | + PdfSignatureValidationResult result = signatureField.ValidateSignature(collection); |
32 | 34 |
|
33 | | -//Checks whether the signature is valid or not. |
34 | | -SignatureStatus status = result.SignatureStatus; |
| 35 | + // Check if the signature is valid |
| 36 | + SignatureStatus status = result.SignatureStatus; |
35 | 37 |
|
36 | | -//Checks whether the document is modified or not. |
37 | | -bool isModified = result.IsDocumentModified; |
| 38 | + // Check if the document has been modified after signing |
| 39 | + bool isModified = result.IsDocumentModified; |
38 | 40 |
|
39 | | -Console.WriteLine("Document modified: " + isModified); |
| 41 | + // Check if Long-Term Validation (LTV) is enabled in the signature |
| 42 | + bool isLtvEnabled = result.LtvVerificationInfo.IsLtvEnabled; |
40 | 43 |
|
41 | | -//Signature details. |
42 | | -string issuerName = signatureField.Signature.Certificate.IssuerName; |
43 | | -DateTime validFrom = signatureField.Signature.Certificate.ValidFrom; |
44 | | -DateTime validTo = signatureField.Signature.Certificate.ValidTo; |
45 | | -string signatureAlgorithm = result.SignatureAlgorithm; |
46 | | -DigestAlgorithm digestAlgorithm = result.DigestAlgorithm; |
| 44 | + // Check if Certificate Revocation List (CRL) data is embedded in the PDF |
| 45 | + bool isCrlEmbedded = result.LtvVerificationInfo.IsCrlEmbedded; |
47 | 46 |
|
48 | | -Console.WriteLine("Issuer Name: " + issuerName); |
49 | | -Console.WriteLine("Valid From: " + validFrom); |
50 | | -Console.WriteLine("Valid To: " + validTo); |
51 | | -Console.WriteLine("Signature Algorithm: " + signatureAlgorithm); |
52 | | -Console.WriteLine("Digest Algorithm: " + digestAlgorithm); |
| 47 | + // Check if Online Certificate Status Protocol (OCSP) data is embedded in the PDF |
| 48 | + bool isOcspEmbedded = result.LtvVerificationInfo.IsOcspEmbedded; |
53 | 49 |
|
54 | | -//Revocation validation details. |
55 | | -RevocationResult revocationDetails = result.RevocationResult; |
56 | | -RevocationStatus revocationStatus = revocationDetails.OcspRevocationStatus; |
57 | | -bool isRevokedCRL = revocationDetails.IsRevokedCRL; |
| 50 | + // Output the validation results to the console |
| 51 | + Console.WriteLine("Document modified: " + isModified); |
| 52 | + Console.WriteLine("LTV enabled: " + isLtvEnabled); |
| 53 | + Console.WriteLine("CRL embedded: " + isCrlEmbedded); |
| 54 | + Console.WriteLine("OCSP embedded: " + isOcspEmbedded); |
58 | 55 |
|
59 | | -Console.WriteLine("Revocation Status: " + revocationStatus); |
60 | | -Console.WriteLine("Is Revoked CRL: " + isRevokedCRL); |
| 56 | + // Extract and display signature certificate details |
| 57 | + string issuerName = signatureField.Signature.Certificate.IssuerName; |
| 58 | + DateTime validFrom = signatureField.Signature.Certificate.ValidFrom; |
| 59 | + DateTime validTo = signatureField.Signature.Certificate.ValidTo; |
| 60 | + string signatureAlgorithm = result.SignatureAlgorithm; |
| 61 | + DigestAlgorithm digestAlgorithm = result.DigestAlgorithm; |
61 | 62 |
|
62 | | -//Close the document. |
63 | | -loadedDocument.Close(true); |
| 63 | + Console.WriteLine("Issuer Name: " + issuerName); |
| 64 | + Console.WriteLine("Valid From: " + validFrom); |
| 65 | + Console.WriteLine("Valid To: " + validTo); |
| 66 | + Console.WriteLine("Signature Algorithm: " + signatureAlgorithm); |
| 67 | + Console.WriteLine("Digest Algorithm: " + digestAlgorithm); |
| 68 | + |
| 69 | + // Extract and display revocation validation details |
| 70 | + RevocationResult revocationDetails = result.RevocationResult; |
| 71 | + RevocationStatus revocationStatus = revocationDetails.OcspRevocationStatus; |
| 72 | + bool isRevokedCRL = revocationDetails.IsRevokedCRL; |
| 73 | + |
| 74 | + Console.WriteLine("Revocation Status: " + revocationStatus); |
| 75 | + Console.WriteLine("Is Revoked CRL: " + isRevokedCRL); |
| 76 | + |
| 77 | + // Close the loaded PDF document and release resources |
| 78 | + loadedDocument.Close(true); |
| 79 | + } |
| 80 | +} |
0 commit comments