1+ using Org . BouncyCastle . Asn1 . Pkcs ;
2+ using Org . BouncyCastle . Crypto ;
3+ using Org . BouncyCastle . OpenSsl ;
4+ using Org . BouncyCastle . Pkcs ;
5+ using Org . BouncyCastle . Security ;
6+ using Syncfusion . Drawing ;
7+ using Syncfusion . Pdf ;
8+ using Syncfusion . Pdf . Graphics ;
9+ using Syncfusion . Pdf . Security ;
10+
11+ //Creates a new PDF document.
12+ using ( PdfDocument document = new PdfDocument ( ) )
13+ {
14+ //Add a new page.
15+ PdfPageBase page = document . Pages . Add ( ) ;
16+ PdfGraphics graphics = page . Graphics ;
17+ //Get the certificate file.
18+ Org . BouncyCastle . X509 . X509CertificateParser cp = new Org . BouncyCastle . X509 . X509CertificateParser ( ) ;
19+ Org . BouncyCastle . X509 . X509Certificate cert = cp . ReadCertificate ( File . ReadAllBytes ( Path . GetFullPath ( @"Data/certificate.cer" ) ) ) ;
20+ //Read the PEM file.
21+ PemReader pmr = new PemReader ( new StringReader ( File . ReadAllText ( Path . GetFullPath ( @"Data/privateKey.pem" ) ) ) ) ;
22+ AsymmetricCipherKeyPair KeyPair = ( AsymmetricCipherKeyPair ) pmr . ReadObject ( ) ;
23+ //Get the PFX file stream.
24+ Stream pfxFile = CreatePfxFile ( cert , KeyPair . Private ) ;
25+ //Creates a certificate instance from the PFX file with a private key.
26+ PdfCertificate pdfCert = new PdfCertificate ( pfxFile , "syncfusion" ) ;
27+ //Creates a digital signature.
28+ PdfSignature signature = new PdfSignature ( document , page , pdfCert , "Signature" ) ;
29+ //Sets the signature information.
30+ signature . Bounds = new RectangleF ( new PointF ( 0 , 0 ) , new SizeF ( 300 , 100 ) ) ;
31+ signature . ContactInfo = "[email protected] " ; 32+ signature . LocationInfo = "Honolulu, Hawaii" ;
33+ signature . Reason = "I am author of this document." ;
34+ FileStream imageStream = new FileStream ( Path . GetFullPath ( @"Data/Logo.png" ) , FileMode . Open , FileAccess . Read ) ;
35+ //Load an image file.
36+ PdfBitmap image = new PdfBitmap ( imageStream ) ;
37+ //Draw an image in the signature appearance.
38+ signature . Appearance . Normal . Graphics . DrawImage ( image , new RectangleF ( 0 , 0 , 300 , 100 ) ) ;
39+ //Saves the document.
40+ document . Save ( Path . GetFullPath ( @"Output/Output.pdf" ) ) ;
41+ }
42+
43+ // Create a PFX file using the BouncyCastle.
44+ static Stream CreatePfxFile ( Org . BouncyCastle . X509 . X509Certificate certificate , AsymmetricKeyParameter privateKey )
45+ {
46+ //Create a certificate entry.
47+ X509CertificateEntry certEntry = new X509CertificateEntry ( certificate ) ;
48+ string friendlyName = certificate . SubjectDN . ToString ( ) ;
49+ //Get bytes of the private key.
50+ PrivateKeyInfo keyInfo = PrivateKeyInfoFactory . CreatePrivateKeyInfo ( privateKey ) ;
51+ byte [ ] keyBytes = keyInfo . ToAsn1Object ( ) . GetEncoded ( ) ;
52+ Pkcs12StoreBuilder builder = new Pkcs12StoreBuilder ( ) ;
53+ builder . SetUseDerEncoding ( true ) ;
54+ Pkcs12Store store = builder . Build ( ) ;
55+ //Create a store entry.
56+ store . SetKeyEntry ( "private" , new AsymmetricKeyEntry ( privateKey ) , new X509CertificateEntry [ ] { certEntry } ) ;
57+ //Save the story to the stream.
58+ MemoryStream stream = new MemoryStream ( ) ;
59+ store . Save ( stream , "syncfusion" . ToCharArray ( ) , new SecureRandom ( ) ) ;
60+ return stream ;
61+ }
0 commit comments