Skip to content

Commit f9fbb9b

Browse files
Feedback changes
1 parent adb221a commit f9fbb9b

File tree

4 files changed

+55
-70
lines changed

4 files changed

+55
-70
lines changed
-2.61 KB
Binary file not shown.

Signature/Add_ESign/Data/ESign.png

-5.55 KB
Binary file not shown.

Signature/Add_ESign/Data/John.png

4 Bytes
Loading

Signature/Add_ESign/MainWindow.xaml.cs

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ namespace WPF_Sample_FW
1818
/// </summary>
1919
public partial class MainWindow : Window
2020
{
21+
string filePath = "../../../Data/";
2122
bool addSignature = false;
2223
public MainWindow()
2324
{
2425
InitializeComponent();
25-
PDFViewer.Load("../../../Data/Ink signature.pdf");
26+
PDFViewer.Load(filePath + "Ink signature.pdf");
2627
PDFViewer.Loaded += PDFViewer_Loaded;
2728
PDFViewer.ZoomMode = ZoomMode.FitPage;
2829
}
@@ -40,20 +41,20 @@ private void PDFViewer_Loaded(object sender, RoutedEventArgs e)
4041

4142
// Get the first button from the stack to copy its style and icon.
4243
Button button = stack.Children[0] as Button;
43-
Path path1 = button.Content as Path;
44+
Path oldStylePath = button.Content as Path;
4445

45-
Button eSignButton = GetButton(path1, button);
46+
Button eSignButton = GetButton(oldStylePath, button);
4647

4748
// Add the new eSign button to the toolbar stack.
4849
stackPanel.Children.Add(eSignButton);
4950
}
5051

5152
private void eSignButton_Click(object sender, RoutedEventArgs e)
5253
{
53-
addSignature = true;
54+
addSignature = true;
5455
}
5556

56-
private Button GetButton(System.Windows.Shapes.Path path1, Button button)
57+
private Button GetButton(System.Windows.Shapes.Path oldPath, Button button)
5758
{
5859
// Create a new custom button for eSign functionality.
5960
Button eSignButton = new Button();
@@ -63,15 +64,15 @@ private Button GetButton(System.Windows.Shapes.Path path1, Button button)
6364
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");
6465

6566
// Copy the fill color from the existing button's icon.
66-
path.Fill = path1.Fill;
67+
path.Fill = oldPath.Fill;
6768

6869
// Set the icon as the content of the new button.
6970
eSignButton.Content = path;
7071

7172
// Match the icon's size and layout to the original button.
7273
path.Stretch = Stretch.Uniform;
73-
path.Height = path1.Height;
74-
path.Width = path1.Width;
74+
path.Height = oldPath.Height;
75+
path.Width = oldPath.Width;
7576

7677
// Match the button's background and layout properties to the original button.
7778
eSignButton.Height = button.Height;
@@ -86,52 +87,10 @@ private Button GetButton(System.Windows.Shapes.Path path1, Button button)
8687

8788
private void PDFViewer_PageClicked(object sender, PageClickedEventArgs args)
8889
{
89-
90-
int pageIndex = PDFViewer.CurrentPageIndex - 1;
91-
if (addSignature && pageIndex >= 0)
90+
if (addSignature)
9291
{
93-
int width = 200;
94-
int height = 100;
95-
string signerName = "John";
96-
string dateTime = DateTime.Now.ToString("yyyy.MM.dd\nHH:mm:ss zzz");
97-
string text = $"Digitally signed by {signerName}\nDate: {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();
13594

13695
//Gets the first page of the document
13796
PdfLoadedPage page = PDFViewer.LoadedDocument.Pages[pageIndex] as PdfLoadedPage;
@@ -145,18 +104,18 @@ private void PDFViewer_PageClicked(object sender, PageClickedEventArgs args)
145104
double y = pagePoint.Y;
146105

147106
//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");
149108

150109
//Creates a digital signature
151110
PdfSignature Signature = new PdfSignature(PDFViewer.LoadedDocument, page, pdfCert, "Signature");
152111

153112
//Sets an image for signature field
154-
PdfBitmap signatureImage = new PdfBitmap("../../../Data/ESign.png");
113+
PdfBitmap signatureImage = new PdfBitmap( filePath + "ESign.png");
155114

156115
// 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);
160119

161120
Signature.ContactInfo = "[email protected]";
162121
Signature.LocationInfo = "Honolulu, Hawaii";
@@ -172,28 +131,54 @@ private void PDFViewer_PageClicked(object sender, PageClickedEventArgs args)
172131

173132
//Reloads the document
174133
PDFViewer.Load(stream);
175-
PDFViewer.GoToPageAtIndex(pageIndex + 1);
176134
addSignature = false;
177135
}
178136

179137
}
180-
private void ResizeImage(string inputPath, string outputPath, int width, int height)
181-
{
182138

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\nHH:mm:ss zzz");
148+
string text = $"Digitally signed by {signerName}\nDate: {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))
184152
{
185-
using (Bitmap resizedImage = new Bitmap(width, height))
153+
using (Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
186154
{
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))
188157
{
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);
194162
}
163+
}
164+
}
165+
string inputPath = filePath + "John.png";
195166

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);
197182
}
198183
}
199184
}

0 commit comments

Comments
 (0)