44using Syncfusion . Pdf ;
55
66//Load an existing PDF document.
7- using ( PdfLoadedDocument loadedDocument = new PdfLoadedDocument ( Path . GetFullPath ( @"Data/SignatureFields.pdf" ) ) )
8- {
9-
10- //Get the first page of the document.
11- PdfLoadedPage page = loadedDocument . Pages [ 0 ] as PdfLoadedPage ;
12- //Get the first signature field of the PDF document.
13- PdfLoadedSignatureField signatureField1 = loadedDocument . Form . Fields [ 0 ] as PdfLoadedSignatureField ;
14- //Create a certificate instance from a PFX file with a private key.
15- FileStream certificateStream = new FileStream ( Path . GetFullPath ( @"Data/PDF.pfx" ) , FileMode . Open , FileAccess . Read ) ;
16- PdfCertificate certificate1 = new PdfCertificate ( certificateStream , "syncfusion" ) ;
17- //Add a signature to the signature field.
18- signatureField1 . Signature = new PdfSignature ( loadedDocument , page , certificate1 , "Signature" , signatureField1 ) ;
19- //Set an image for the signature field.
20- FileStream imageStream1 = new FileStream ( Path . GetFullPath ( @"Data/Student Signature.jpg" ) , FileMode . Open , FileAccess . Read ) ;
21- PdfBitmap signatureImage = new PdfBitmap ( imageStream1 ) ;
22- //Insert an image in the signature appearance.
23- signatureField1 . Signature . Appearance . Normal . Graphics . DrawImage ( signatureImage , 0 , 0 , 90 , 20 ) ;
24- //Save the document into the stream.
25- MemoryStream stream = new MemoryStream ( ) ;
26- loadedDocument . Save ( stream ) ;
27-
28- //Load the signed PDF document.
29- using ( PdfLoadedDocument signedDocument = new PdfLoadedDocument ( stream ) )
30- {
31- //Load the PDF page.
32- PdfLoadedPage loadedPage = signedDocument . Pages [ 0 ] as PdfLoadedPage ;
33-
34- //Get the first signature field of the PDF document.
35- PdfLoadedSignatureField signatureField2 = signedDocument . Form . Fields [ 1 ] as PdfLoadedSignatureField ;
36-
37- //Add a signature to the signature field.
38- signatureField2 . Signature = new PdfSignature ( signedDocument , loadedPage , certificate1 , "Signature" , signatureField2 ) ;
39-
40- //Set an image for the signature field.
41- FileStream imageStream2 = new FileStream ( Path . GetFullPath ( @"Data/Teacher Signature.png" ) , FileMode . Open , FileAccess . Read ) ;
42- PdfBitmap signatureImage1 = new PdfBitmap ( imageStream2 ) ;
43-
44- //Draw an image in the signature appearance.
45- signatureField2 . Signature . Appearance . Normal . Graphics . DrawImage ( signatureImage1 , 0 , 0 , 90 , 20 ) ;
46-
47- //Save the PDF document
48- signedDocument . Save ( Path . GetFullPath ( @"Output/Output.pdf" ) ) ;
49- }
50- }
7+ PdfLoadedDocument loadedDocument = new PdfLoadedDocument ( Path . GetFullPath ( @"Data/SignatureFields.pdf" ) ) ;
8+ //Get the first page of the document.
9+ PdfLoadedPage page = loadedDocument . Pages [ 0 ] as PdfLoadedPage ;
10+ //Get the first signature field of the PDF document.
11+ PdfLoadedSignatureField signatureField1 = loadedDocument . Form . Fields [ 0 ] as PdfLoadedSignatureField ;
12+ //Create a certificate instance from a PFX file with a private key.
13+ PdfCertificate certificate1 = new PdfCertificate ( Path . GetFullPath ( @"Data/PDF.pfx" ) , "syncfusion" ) ;
14+ //Add a signature to the signature field.
15+ signatureField1 . Signature = new PdfSignature ( loadedDocument , page , certificate1 , "Signature" , signatureField1 ) ;
16+ //Set an image for the signature field.
17+ FileStream imageStream1 = new FileStream ( Path . GetFullPath ( @"Data/Student Signature.jpg" ) , FileMode . Open , FileAccess . Read ) ;
18+ PdfBitmap signatureImage = new PdfBitmap ( imageStream1 ) ;
19+ imageStream1 . Position = 0 ;
20+ //Insert an image in the signature appearance.
21+ signatureField1 . Signature . Appearance . Normal . Graphics . DrawImage ( signatureImage , 0 , 0 , signatureField1 . Bounds . Width , signatureField1 . Bounds . Height ) ;
22+ //Save the document into the stream.
23+ MemoryStream stream = new MemoryStream ( ) ;
24+ loadedDocument . Save ( stream ) ;
25+ //Close the PDF document
26+ loadedDocument . Close ( true ) ;
27+ stream . Position = 0 ;
28+ //Load the signed PDF document.
29+ PdfLoadedDocument signedDocument = new PdfLoadedDocument ( stream ) ;
30+ //Load the PDF page.
31+ PdfLoadedPage loadedPage = signedDocument . Pages [ 0 ] as PdfLoadedPage ;
32+ //Get the first signature field of the PDF document.
33+ PdfLoadedSignatureField signatureField2 = signedDocument . Form . Fields [ 1 ] as PdfLoadedSignatureField ;
34+ //Add a signature to the signature field.
35+ signatureField2 . Signature = new PdfSignature ( signedDocument , loadedPage , certificate1 , "Signature" , signatureField2 ) ;
36+ //Set an image for the signature field.
37+ FileStream imageStream2 = new FileStream ( Path . GetFullPath ( @"Data/Teacher Signature.png" ) , FileMode . Open , FileAccess . Read ) ;
38+ PdfBitmap signatureImage1 = new PdfBitmap ( imageStream2 ) ;
39+ imageStream2 . Position = 0 ;
40+ //Draw an image in the signature appearance.
41+ signatureField2 . Signature . Appearance . Normal . Graphics . DrawImage ( signatureImage1 , 0 , 0 , signatureField2 . Bounds . Width , signatureField2 . Bounds . Height ) ;
42+ //Save the PDF document
43+ signedDocument . Save ( Path . GetFullPath ( @"Output/Output.pdf" ) ) ;
44+ //Close the PDF document
45+ signedDocument . Close ( true ) ;
0 commit comments