44using System . Security . Cryptography . X509Certificates ;
55using System . Security . Cryptography ;
66
7- namespace Externally_sign_the_PDF_document {
8- internal class Program {
9- static void Main ( string [ ] args ) {
10- //Get the stream from the document
7+ namespace Externally_sign_the_PDF_document
8+ {
9+ internal class Program
10+ {
11+ static void Main ( string [ ] args )
12+ {
13+ // Get the stream from the document
1114 FileStream documentStream = new FileStream ( Path . GetFullPath ( @"Data/Barcode.pdf" ) , FileMode . Open , FileAccess . Read ) ;
12- //Load the existing PDF document
15+
16+ // Load the existing PDF document
1317 PdfLoadedDocument loadedDocument = new PdfLoadedDocument ( documentStream ) ;
1418
15- //Creates a digital signature.
19+ // Create a digital signature
1620 PdfSignature signature = new PdfSignature ( loadedDocument , loadedDocument . Pages [ 0 ] , null , "Signature" ) ;
17- //Sets the signature information.
21+
22+ // Set the signature information
1823 signature . Bounds = new RectangleF ( new PointF ( 0 , 0 ) , new SizeF ( 100 , 30 ) ) ;
1924 signature . Settings . CryptographicStandard = CryptographicStandard . CADES ;
2025 signature . Settings . DigestAlgorithm = DigestAlgorithm . SHA1 ;
2126
22- //Create an external signer.
27+ // Create an external signer
2328 IPdfExternalSigner externalSignature = new ExternalSigner ( "SHA1" ) ;
2429
25- //Add public certificates.
30+ // Add public certificates
2631 List < X509Certificate2 > certificates = new List < X509Certificate2 > ( ) ;
27- certificates . Add ( new X509Certificate2 ( new X509Certificate2 ( Path . GetFullPath ( @"Data/PDF.pfx" ) , "password123" ) ) ) ;
32+ certificates . Add ( new X509Certificate2 ( Path . GetFullPath ( @"Data/PDF.pfx" ) , "password123" ) ) ;
2833 signature . AddExternalSigner ( externalSignature , certificates , null ) ;
2934
30- //Create file stream.
31- using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Output.pdf" ) , FileMode . Create , FileAccess . ReadWrite ) ) {
32- //Save the PDF document to file stream.
35+ // Create file stream
36+ using ( FileStream outputFileStream = new FileStream ( Path . GetFullPath ( @"Output/Output.pdf" ) , FileMode . Create , FileAccess . ReadWrite ) )
37+ {
38+ // Save the PDF document to file stream
3339 loadedDocument . Save ( outputFileStream ) ;
3440 }
35- //Close the document.
41+
42+ // Close the document
3643 loadedDocument . Close ( true ) ;
3744 }
38- //Create the external signer class and sign the document hash.
39- class ExternalSigner : IPdfExternalSigner {
45+
46+ // Create the external signer class and sign the document hash
47+ class ExternalSigner : IPdfExternalSigner
48+ {
4049 private string _hashAlgorithm ;
41- public string HashAlgorithm {
50+
51+ public string HashAlgorithm
52+ {
4253 get { return _hashAlgorithm ; }
4354 }
4455
45- public ExternalSigner ( string hashAlgorithm ) {
56+ public ExternalSigner ( string hashAlgorithm )
57+ {
4658 _hashAlgorithm = hashAlgorithm ;
4759 }
48- public byte [ ] Sign ( byte [ ] message , out byte [ ] timeStampResponse ) {
60+
61+ public byte [ ] Sign ( byte [ ] message , out byte [ ] timeStampResponse )
62+ {
4963 timeStampResponse = null ;
50- X509Certificate2 digitalID = new X509Certificate2 ( new X509Certificate2 ( Path . GetFullPath ( @"Data/PDF.pfx" ) , "password123" ) ) ;
51- if ( digitalID . PrivateKey is System . Security . Cryptography . RSACryptoServiceProvider ) {
52- System . Security . Cryptography . RSACryptoServiceProvider rsa = ( System . Security . Cryptography . RSACryptoServiceProvider ) digitalID . PrivateKey ;
53- return rsa . SignData ( message , HashAlgorithm ) ;
64+
65+ FileStream certificateStream = new FileStream ( "PDF.pfx" , FileMode . Open , FileAccess . Read ) ;
66+ X509Certificate2 digitalID = new X509Certificate2 ( certificateStream , "password123" ) ;
67+
68+ if ( digitalID . PrivateKey is System . Security . Cryptography . RSACryptoServiceProvider rsaProvider )
69+ {
70+ return rsaProvider . SignData ( message , HashAlgorithm ) ;
5471 }
55- else if ( digitalID . PrivateKey is RSACng ) {
56- RSACng rsa = ( RSACng ) digitalID . PrivateKey ;
57- return rsa . SignData ( message , System . Security . Cryptography . HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
72+ else if ( digitalID . PrivateKey is RSACng rsaCng )
73+ {
74+ return rsaCng . SignData ( message , System . Security . Cryptography . HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
5875 }
76+ else if ( digitalID . PrivateKey is System . Security . Cryptography . RSAOpenSsl rsaOpenSsl )
77+ {
78+ return rsaOpenSsl . SignData ( message , System . Security . Cryptography . HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
79+ }
80+
5981 return null ;
6082 }
6183 }
6284 }
63- }
85+ }
0 commit comments