1+ using Syncfusion . Pdf . Parsing ;
2+ using Syncfusion . Pdf . Security ;
3+ using System . Security . Cryptography . X509Certificates ;
4+ using System . Text ;
5+
6+ string dataPath = Path . GetFullPath ( @"Data/" ) ;
7+
8+ // Load signed PDF document
9+ using PdfLoadedDocument ldoc = new PdfLoadedDocument ( Path . GetFullPath ( @"Data/Input.pdf" ) ) ;
10+
11+ // Get signature field
12+ PdfLoadedSignatureField lSigFld = ldoc . Form . Fields [ 0 ] as PdfLoadedSignatureField ;
13+
14+ // Root and Intermediate Certificates
15+ X509CertificateCollection collection = new X509CertificateCollection ( ) ;
16+
17+ string [ ] certificates =
18+ {
19+ "Root.cer" ,
20+ "Intermediate0.cer" ,
21+ "Intermediate1.cer"
22+ } ;
23+
24+ foreach ( string certFile in certificates )
25+ {
26+ byte [ ] certData = File . ReadAllBytes ( Path . Combine ( dataPath , certFile ) ) ;
27+
28+ X509Certificate2 certificate = new X509Certificate2 ( certData ) ;
29+
30+ collection . Add ( certificate ) ;
31+ }
32+
33+ // Validate signature
34+ PdfSignatureValidationResult result = lSigFld . ValidateSignature ( collection ) ;
35+
36+ StringBuilder builder = new StringBuilder ( ) ;
37+
38+ builder . AppendLine ( "Signature is " + result . SignatureStatus ) ;
39+ builder . AppendLine ( ) ;
40+ builder . AppendLine ( "----------Validation Summary----------" ) ;
41+ builder . AppendLine ( ) ;
42+
43+ // Modified check
44+ if ( result . IsDocumentModified )
45+ {
46+ builder . AppendLine ( "The document has been altered or corrupted since the signature was applied." ) ;
47+ }
48+ else
49+ {
50+ builder . AppendLine ( "The document has not been modified since the signature was applied." ) ;
51+ }
52+
53+ // Certificate details
54+ builder . AppendLine ( "Digitally signed by: " + lSigFld . Signature . Certificate . IssuerName ) ;
55+
56+ builder . AppendLine ( "Valid From: " + lSigFld . Signature . Certificate . ValidFrom ) ;
57+
58+ builder . AppendLine ( "Valid To: " + lSigFld . Signature . Certificate . ValidTo ) ;
59+
60+ builder . AppendLine ( "Signature Algorithm: " + result . SignatureAlgorithm ) ;
61+
62+ builder . AppendLine ( "Hash Algorithm: " + result . DigestAlgorithm ) ;
63+
64+ // Revocation details
65+ builder . AppendLine ( "OCSP Revocation Status: " + result . RevocationResult . OcspRevocationStatus ) ;
66+
67+ if ( result . RevocationResult . OcspRevocationStatus == RevocationStatus . None && result . RevocationResult . IsRevokedCRL )
68+ {
69+ builder . AppendLine ( "CRL is revoked." ) ;
70+ }
71+
72+ builder . AppendLine ( ) ;
73+ builder . AppendLine ( "--------Revocation Information---------" ) ;
74+ builder . AppendLine ( ) ;
75+
76+ foreach ( PdfSignerCertificate signerCertificate in result . SignerCertificates )
77+ {
78+ if ( signerCertificate . OcspCertificate != null )
79+ {
80+ builder . AppendLine ( "------------OCSP Certificate-------------" ) ;
81+
82+ foreach ( X509Certificate2 item in signerCertificate . OcspCertificate . Certificates )
83+ {
84+ builder . AppendLine ( "The OCSP Response was signed by: " + item . SubjectName . Name ) ;
85+ }
86+
87+ builder . AppendLine ( "Is Embedded: " + signerCertificate . OcspCertificate . IsEmbedded ) ;
88+
89+ builder . AppendLine ( "Valid From: " + signerCertificate . OcspCertificate . ValidFrom ) ;
90+
91+ builder . AppendLine ( "Valid To: " + signerCertificate . OcspCertificate . ValidTo ) ;
92+
93+ builder . AppendLine ( ) ;
94+ }
95+
96+ if ( signerCertificate . CrlCertificate != null )
97+ {
98+ builder . AppendLine ( "------------CRL Certificate--------------" ) ;
99+
100+ foreach ( X509Certificate2 item in signerCertificate . CrlCertificate . Certificates )
101+ {
102+ builder . AppendLine ( "The CRL was signed by: " + item . SubjectName . Name ) ;
103+ }
104+
105+ builder . AppendLine ( "Is Embedded: " + signerCertificate . CrlCertificate . IsEmbedded ) ;
106+ builder . AppendLine ( "Valid From: " + signerCertificate . CrlCertificate . ValidFrom ) ;
107+ builder . AppendLine ( "Valid To: " + signerCertificate . CrlCertificate . ValidTo ) ;
108+ builder . AppendLine ( ) ;
109+ }
110+ }
111+ //Save to file
112+ File . WriteAllText ( Path . GetFullPath ( @"Output/Output.txt" ) , builder . ToString ( ) ) ;
0 commit comments