@@ -18,11 +18,12 @@ namespace WPF_Sample_FW
18
18
/// </summary>
19
19
public partial class MainWindow : Window
20
20
{
21
+ string filePath = "../../../Data/" ;
21
22
bool addSignature = false ;
22
23
public MainWindow ( )
23
24
{
24
25
InitializeComponent ( ) ;
25
- PDFViewer . Load ( "../../../Data/ Ink signature.pdf") ;
26
+ PDFViewer . Load ( filePath + " Ink signature.pdf") ;
26
27
PDFViewer . Loaded += PDFViewer_Loaded ;
27
28
PDFViewer . ZoomMode = ZoomMode . FitPage ;
28
29
}
@@ -40,20 +41,20 @@ private void PDFViewer_Loaded(object sender, RoutedEventArgs e)
40
41
41
42
// Get the first button from the stack to copy its style and icon.
42
43
Button button = stack . Children [ 0 ] as Button ;
43
- Path path1 = button . Content as Path ;
44
+ Path oldStylePath = button . Content as Path ;
44
45
45
- Button eSignButton = GetButton ( path1 , button ) ;
46
+ Button eSignButton = GetButton ( oldStylePath , button ) ;
46
47
47
48
// Add the new eSign button to the toolbar stack.
48
49
stackPanel . Children . Add ( eSignButton ) ;
49
50
}
50
51
51
52
private void eSignButton_Click ( object sender , RoutedEventArgs e )
52
53
{
53
- addSignature = true ;
54
+ addSignature = true ;
54
55
}
55
56
56
- private Button GetButton ( System . Windows . Shapes . Path path1 , Button button )
57
+ private Button GetButton ( System . Windows . Shapes . Path oldPath , Button button )
57
58
{
58
59
// Create a new custom button for eSign functionality.
59
60
Button eSignButton = new Button ( ) ;
@@ -63,15 +64,15 @@ private Button GetButton(System.Windows.Shapes.Path path1, Button button)
63
64
path . Data = System . Windows . Media . Geometry . Parse ( "M218.17 424.14c-2.95-5.92-8.09-6.52-10.17-6.52s-7.22.59-10.02 6.19l-7.67 15.34c-6.37 12.78-25.03 11.37-29.48-2.09L144 386.59l-10.61 31.88c-5.89 17.66-22.38 29.53-41 29.53H80c-8.84 0-16-7.16-16-16s7.16-16 16-16h12.39c4.83 0 9.11-3.08 10.64-7.66l18.19-54.64c3.3-9.81 12.44-16.41 22.78-16.41s19.48 6.59 22.77 16.41l13.88 41.64c19.75-16.19 54.06-9.7 66 14.16 1.89 3.78 5.49 5.95 9.36 6.26v-82.12l128-127.09V160H248c-13.2 0-24-10.8-24-24V0H24C10.7 0 0 10.7 0 24v464c0 13.3 10.7 24 24 24h336c13.3 0 24-10.7 24-24v-40l-128-.11c-16.12-.31-30.58-9.28-37.83-23.75zM384 121.9c0-6.3-2.5-12.4-7-16.9L279.1 7c-4.5-4.5-10.6-7-17-7H256v128h128v-6.1zm-96 225.06V416h68.99l161.68-162.78-67.88-67.88L288 346.96zm280.54-179.63l-31.87-31.87c-9.94-9.94-26.07-9.94-36.01 0l-27.25 27.25 67.88 67.88 27.25-27.25c9.95-9.94 9.95-26.07 0-36.01z" ) ;
64
65
65
66
// Copy the fill color from the existing button's icon.
66
- path . Fill = path1 . Fill ;
67
+ path . Fill = oldPath . Fill ;
67
68
68
69
// Set the icon as the content of the new button.
69
70
eSignButton . Content = path ;
70
71
71
72
// Match the icon's size and layout to the original button.
72
73
path . Stretch = Stretch . Uniform ;
73
- path . Height = path1 . Height ;
74
- path . Width = path1 . Width ;
74
+ path . Height = oldPath . Height ;
75
+ path . Width = oldPath . Width ;
75
76
76
77
// Match the button's background and layout properties to the original button.
77
78
eSignButton . Height = button . Height ;
@@ -86,52 +87,10 @@ private Button GetButton(System.Windows.Shapes.Path path1, Button button)
86
87
87
88
private void PDFViewer_PageClicked ( object sender , PageClickedEventArgs args )
88
89
{
89
-
90
- int pageIndex = PDFViewer . CurrentPageIndex - 1 ;
91
- if ( addSignature && pageIndex >= 0 )
90
+ if ( addSignature )
92
91
{
93
- int width = 200 ;
94
- int height = 100 ;
95
- string signerName = "John" ;
96
- string dateTime = DateTime . Now . ToString ( "yyyy.MM.dd\n HH:mm:ss zzz" ) ;
97
- string text = $ "Digitally signed by { signerName } \n Date: { dateTime } \n \n ";
98
- string outputPath = "../../../../Data/DigitalSignatureBlock.png" ;
99
- Directory . CreateDirectory ( System . IO . Path . GetDirectoryName ( outputPath ) ) ;
100
- using ( Bitmap bitmap = new Bitmap ( width , height ) )
101
- {
102
- using ( Graphics graphics = System . Drawing . Graphics . FromImage ( bitmap ) )
103
- {
104
- using ( Font font = new Font ( "Arial" , 9 ) )
105
- using ( SolidBrush backgroundBrush = new SolidBrush ( System . Drawing . Color . White ) )
106
- {
107
- graphics . FillRectangle ( backgroundBrush , 0 , 0 , width , height ) ;
108
- RectangleF layoutRect = new RectangleF ( 10 , 10 , width - 20 , height - 20 ) ;
109
- graphics . DrawString ( text , font , System . Drawing . Brushes . Black , layoutRect ) ;
110
- bitmap . Save ( outputPath , System . Drawing . Imaging . ImageFormat . Png ) ;
111
- }
112
- }
113
- }
114
- string inputPath = "../../../Data/John.png" ; // Replace with your image path
115
- string outputPath1 = "../../../Data/Out.png" ; // Desired output path
116
- ResizeImage ( inputPath , outputPath1 , 200 , 100 ) ;
117
- Console . WriteLine ( "Image resized successfully." ) ;
118
- // Load the two images
119
- using ( System . Drawing . Image image1 = System . Drawing . Image . FromFile ( "../../../Data/Out.png" ) )
120
- using ( System . Drawing . Image image2 = System . Drawing . Image . FromFile ( "../../../Data/DigitalSignatureBlock.png" ) )
121
- {
122
- // Create a new bitmap with combined width and max height
123
- int width1 = image1 . Width + image2 . Width ;
124
- int height1 = Math . Max ( image1 . Height , image2 . Height ) ;
125
- using ( Bitmap combinedImage = new Bitmap ( width1 , height1 ) )
126
- using ( Graphics g = System . Drawing . Graphics . FromImage ( combinedImage ) )
127
- {
128
- // Draw both images side by side
129
- g . DrawImage ( image1 , 0 , 0 ) ;
130
- g . DrawImage ( image2 , image1 . Width , 0 ) ;
131
- // Save the result
132
- combinedImage . Save ( "../../../Data/ESign.png" , System . Drawing . Imaging . ImageFormat . Png ) ;
133
- }
134
- }
92
+ int pageIndex = PDFViewer . CurrentPageIndex - 1 ;
93
+ CreateSignatureImage ( ) ;
135
94
136
95
//Gets the first page of the document
137
96
PdfLoadedPage page = PDFViewer . LoadedDocument . Pages [ pageIndex ] as PdfLoadedPage ;
@@ -145,18 +104,18 @@ private void PDFViewer_PageClicked(object sender, PageClickedEventArgs args)
145
104
double y = pagePoint . Y ;
146
105
147
106
//Creates a certificate instance from PFX file with private key.
148
- PdfCertificate pdfCert = new PdfCertificate ( "../../../Data/ PDF.pfx", "password123" ) ;
107
+ PdfCertificate pdfCert = new PdfCertificate ( filePath + " PDF.pfx", "password123" ) ;
149
108
150
109
//Creates a digital signature
151
110
PdfSignature Signature = new PdfSignature ( PDFViewer . LoadedDocument , page , pdfCert , "Signature" ) ;
152
111
153
112
//Sets an image for signature field
154
- PdfBitmap signatureImage = new PdfBitmap ( "../../../Data/ ESign.png") ;
113
+ PdfBitmap signatureImage = new PdfBitmap ( filePath + " ESign.png") ;
155
114
156
115
// Center the signature on the click position using dimensions in points.
157
- float sigWidth = signatureImage . PhysicalDimension . Width ;
158
- float sigHeight = signatureImage . PhysicalDimension . Height ;
159
- Signature . Bounds = new System . Drawing . RectangleF ( ( float ) ( x ) , ( float ) ( y ) , sigWidth * 2 , sigHeight * 2 ) ;
116
+ float signWidth = signatureImage . PhysicalDimension . Width ;
117
+ float signHeight = signatureImage . PhysicalDimension . Height ;
118
+ Signature . Bounds = new System . Drawing . RectangleF ( ( float ) ( x ) , ( float ) ( y ) , signWidth , signHeight ) ;
160
119
161
120
Signature . ContactInfo = "[email protected] " ;
162
121
Signature . LocationInfo = "Honolulu, Hawaii" ;
@@ -172,28 +131,54 @@ private void PDFViewer_PageClicked(object sender, PageClickedEventArgs args)
172
131
173
132
//Reloads the document
174
133
PDFViewer . Load ( stream ) ;
175
- PDFViewer . GoToPageAtIndex ( pageIndex + 1 ) ;
176
134
addSignature = false ;
177
135
}
178
136
179
137
}
180
- private void ResizeImage ( string inputPath , string outputPath , int width , int height )
181
- {
182
138
183
- using ( System . Drawing . Image originalImage = System . Drawing . Image . FromFile ( inputPath ) )
139
+ private void CreateSignatureImage ( )
140
+ {
141
+ int pageIndex = PDFViewer . CurrentPageIndex - 1 ;
142
+ if ( pageIndex < 0 )
143
+ return ;
144
+ int width = 200 ;
145
+ int height = 100 ;
146
+ string signerName = "John" ;
147
+ string dateTime = DateTime . Now . ToString ( "yyyy.MM.dd\n HH:mm:ss zzz" ) ;
148
+ string text = $ "Digitally signed by { signerName } \n Date: { dateTime } \n \n ";
149
+ string outputPath = filePath + "DigitalSignatureBlock.png" ;
150
+ Directory . CreateDirectory ( System . IO . Path . GetDirectoryName ( outputPath ) ) ;
151
+ using ( Bitmap bitmap = new Bitmap ( width , height ) )
184
152
{
185
- using ( Bitmap resizedImage = new Bitmap ( width , height ) )
153
+ using ( Graphics graphics = System . Drawing . Graphics . FromImage ( bitmap ) )
186
154
{
187
- using ( Graphics graphics = Graphics . FromImage ( resizedImage ) )
155
+ using ( Font font = new Font ( "Arial" , 9 ) )
156
+ using ( SolidBrush backgroundBrush = new SolidBrush ( System . Drawing . Color . White ) )
188
157
{
189
- graphics . CompositingQuality = System . Drawing . Drawing2D . CompositingQuality . HighQuality ;
190
- graphics . InterpolationMode = System . Drawing . Drawing2D . InterpolationMode . HighQualityBicubic ;
191
- graphics . SmoothingMode = System . Drawing . Drawing2D . SmoothingMode . HighQuality ;
192
-
193
- graphics . DrawImage ( originalImage , 0 , 0 , width , height ) ;
158
+ graphics . FillRectangle ( backgroundBrush , 0 , 0 , width , height ) ;
159
+ RectangleF layoutRect = new RectangleF ( 10 , 10 , width - 20 , height - 20 ) ;
160
+ graphics . DrawString ( text , font , System . Drawing . Brushes . Black , layoutRect ) ;
161
+ bitmap . Save ( outputPath , System . Drawing . Imaging . ImageFormat . Png ) ;
194
162
}
163
+ }
164
+ }
165
+ string inputPath = filePath + "John.png" ;
195
166
196
- resizedImage . Save ( outputPath ) ;
167
+ // Load the two images
168
+ using ( System . Drawing . Image nameImage = System . Drawing . Image . FromFile ( filePath + "John.png" ) )
169
+ using ( System . Drawing . Image signImage = System . Drawing . Image . FromFile ( filePath + "DigitalSignatureBlock.png" ) )
170
+ {
171
+ // Create a new bitmap with combined width and max height
172
+ int signatureWidth = nameImage . Width + signImage . Width ;
173
+ int signatureHeight = Math . Max ( nameImage . Height , signImage . Height ) ;
174
+ using ( Bitmap combinedImage = new Bitmap ( signatureWidth , signatureHeight ) )
175
+ using ( Graphics g = System . Drawing . Graphics . FromImage ( combinedImage ) )
176
+ {
177
+ // Draw both images side by side
178
+ g . DrawImage ( nameImage , 0 , 0 ) ;
179
+ g . DrawImage ( signImage , nameImage . Width , 0 ) ;
180
+ // Save the result
181
+ combinedImage . Save ( filePath + "ESign.png" , System . Drawing . Imaging . ImageFormat . Png ) ;
197
182
}
198
183
}
199
184
}
0 commit comments