FillDigitalSignatureControl.xaml.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. using ComPDFKit.DigitalSign;
  2. using ComPDFKit.Import;
  3. using ComPDFKit.PDFAnnotation;
  4. using ComPDFKit.PDFAnnotation.Form;
  5. using ComPDFKit.PDFDocument;
  6. using ComPDFKit.PDFPage;
  7. using Compdfkit_Tools.Helper;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Drawing;
  11. using System.Drawing.Imaging;
  12. using System.IO;
  13. using System.Reflection;
  14. using System.Runtime.InteropServices;
  15. using System.Runtime.Remoting.Messaging;
  16. using System.Text;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Controls.Primitives;
  20. using System.Windows.Ink;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Imaging;
  24. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  25. using Color = System.Drawing.Color;
  26. using Pen = System.Windows.Media.Pen;
  27. using PixelFormat = System.Windows.Media.PixelFormat;
  28. using Point = System.Windows.Point;
  29. using Window = System.Windows.Window;
  30. namespace Compdfkit_Tools.PDFControl
  31. {
  32. /// <summary>
  33. /// Interaction logic for CPDFSignControl.xaml
  34. /// </summary>
  35. public partial class FillDigitalSignatureControl : UserControl
  36. {
  37. private readonly string logoPath = "Logo_opa40.png";
  38. private string imagePath = string.Empty;
  39. private string Text = string.Empty;
  40. private Dictionary<string, Border> TabDict { get; set; }
  41. private CPDFSignatureConfig tempSignatureConfig = new CPDFSignatureConfig();
  42. private CPDFSignatureCertificate signatureCertificate;
  43. public CPDFDocument Document;
  44. private string signatureName = string.Empty;
  45. private string location = string.Empty;
  46. private string reason = string.Empty;
  47. private float[] textColor = new float[] { 0, 0, 0 };
  48. private string _signaturePath = string.Empty;
  49. public string SignaturePath
  50. {
  51. get => _signaturePath;
  52. set
  53. {
  54. _signaturePath = value;
  55. }
  56. }
  57. private string _password = string.Empty;
  58. public string Password
  59. {
  60. get => _password;
  61. set
  62. {
  63. _password = value;
  64. signatureCertificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path(SignaturePath, Password);
  65. signatureName = DictionaryValueConverter.GetGrantorFromDictionary(signatureCertificate.SubjectDict);
  66. tempSignatureConfig.Text = signatureName;
  67. InitTempSignature();
  68. }
  69. }
  70. private void InitTempSignature()
  71. {
  72. NameChk.IsChecked = true;
  73. DateChk.IsChecked = true;
  74. LogoChk.IsChecked = true;
  75. TabChk.IsChecked = true;
  76. SetProperty();
  77. CreateTempSignature();
  78. KeyboardInPutTextBox.Text = signatureName;
  79. }
  80. public CPDFSignatureWidget signatureWidget { get; set; }
  81. public event EventHandler<string> AfterFillSignature;
  82. public FillDigitalSignatureControl()
  83. {
  84. InitializeComponent();
  85. TabDict = new Dictionary<string, Border>
  86. {
  87. ["Keyboard"] = KeyboardBorder,
  88. ["Trackpad"] = TrackpadBorder,
  89. ["Image"] = ImageBorder,
  90. ["None"] = NoneBorder
  91. };
  92. SetCheckedTab("Keyboard");
  93. ReasonCmb.SelectedIndex = 0;
  94. }
  95. private void CreateTempSignature()
  96. {
  97. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  98. tempDocument.InsertPage(0, 200, 200, string.Empty);
  99. CPDFPage page = tempDocument.PageAtIndex(0);
  100. CPDFSignatureWidget signatureWidget = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
  101. signatureWidget.SetRect(new CRect(0, 100, 300, 0));
  102. tempSignatureConfig.IsDrawLogo = (bool)LogoChk.IsChecked;
  103. if (tempSignatureConfig.IsDrawLogo)
  104. {
  105. using (FileStream fileData = File.OpenRead(logoPath))
  106. {
  107. BitmapFrame frame = null;
  108. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  109. if (decoder != null && decoder.Frames.Count > 0)
  110. {
  111. frame = decoder.Frames[0];
  112. }
  113. if (frame != null)
  114. {
  115. byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  116. int ImageWidth = frame.PixelWidth;
  117. int ImageHeight = frame.PixelHeight;
  118. frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  119. if(signatureWidget.IsValid())
  120. {
  121. tempSignatureConfig.LogoData = imageArray;
  122. tempSignatureConfig.LogoWidth = ImageWidth;
  123. tempSignatureConfig.LogoHeight = ImageHeight;
  124. }
  125. }
  126. }
  127. }
  128. tempSignatureConfig.Content = Text;
  129. tempSignatureConfig.TextColor = textColor;
  130. tempSignatureConfig.ContentColor = new float[] { 0, 0, 0 };
  131. signatureWidget.UpdataApWithSignature(tempSignatureConfig);
  132. byte[] signatureBitmapBytes = GetTempSignatureImage(signatureWidget, out int width, out int height);
  133. signatureWidget.ReleaseAnnot();
  134. if (signatureBitmapBytes.Length > 0)
  135. {
  136. PixelFormat fmt = PixelFormats.Bgra32;
  137. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, signatureBitmapBytes, (width * fmt.BitsPerPixel + 7) / 8);
  138. imageControl.Source = bps;
  139. }
  140. else
  141. {
  142. imageControl.Source = null;
  143. }
  144. }
  145. public static byte[] GetTempSignatureImage(CPDFSignatureWidget signatureWidget, out int width, out int height)
  146. {
  147. CRect rect = signatureWidget.GetRect();
  148. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  149. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  150. int dpi = (int)dpiProperty.GetValue(null, null);
  151. width = (int)(rect.width() * dpi / 72D * 2);
  152. height = (int)(rect.height() * dpi / 72D * 2);
  153. byte[] imageData = new byte[width * height * 4];
  154. signatureWidget.RenderAnnot(width, height, imageData, CPDFAppearanceType.Normal);
  155. return imageData;
  156. }
  157. private void TextAlignBtn_Click(object sender, RoutedEventArgs e)
  158. {
  159. ToggleButton checkBtn = sender as ToggleButton;
  160. if (checkBtn == null)
  161. {
  162. return;
  163. }
  164. checkBtn.IsChecked = true;
  165. if (checkBtn != TextAlignLeftBtn)
  166. {
  167. tempSignatureConfig.IsContentAlignLeft = true;
  168. TextAlignLeftBtn.IsChecked = false;
  169. }
  170. if (checkBtn != TextAlignRightBtn)
  171. {
  172. tempSignatureConfig.IsContentAlignLeft = false;
  173. TextAlignRightBtn.IsChecked = false;
  174. }
  175. CreateTempSignature();
  176. }
  177. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  178. {
  179. Border clickBorder = sender as Border;
  180. if (clickBorder == null || clickBorder.Tag == null)
  181. {
  182. return;
  183. }
  184. SetCheckedTab(clickBorder.Tag.ToString());
  185. ImagePickPanel.Visibility = Visibility.Hidden;
  186. if (clickBorder == NoneBorder)
  187. {
  188. tempSignatureConfig.IsDrawOnlyContent = true;
  189. }
  190. else
  191. {
  192. tempSignatureConfig.IsDrawOnlyContent = false;
  193. if (clickBorder == KeyboardBorder)
  194. {
  195. tempSignatureConfig.Text = signatureName;
  196. //tempSignatureConfig.ImageBitmap = null;
  197. KeyboardPopup.Visibility = Visibility.Visible;
  198. }
  199. else
  200. {
  201. tempSignatureConfig.Text = string.Empty;
  202. if (clickBorder == TrackpadBorder)
  203. {
  204. CanvaDrawPopup.Visibility = Visibility.Visible;
  205. }
  206. else if (clickBorder == ImageBorder)
  207. {
  208. ImagePickPanel.Visibility = Visibility.Visible;
  209. if (!string.IsNullOrEmpty(imagePath))
  210. {
  211. using (FileStream fileData = File.OpenRead(imagePath))
  212. {
  213. BitmapFrame frame = null;
  214. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  215. if (decoder != null && decoder.Frames.Count > 0)
  216. {
  217. frame = decoder.Frames[0];
  218. }
  219. if (frame != null)
  220. {
  221. byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  222. int ImageWidth = frame.PixelWidth;
  223. int ImageHeight = frame.PixelHeight;
  224. frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  225. if (signatureWidget.IsValid())
  226. {
  227. tempSignatureConfig.ImageData = imageArray;
  228. tempSignatureConfig.ImageWidth = ImageWidth;
  229. tempSignatureConfig.ImageHeight = ImageHeight;
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. SetProperty();
  238. CreateTempSignature();
  239. }
  240. private void SetCheckedTab(string tab)
  241. {
  242. if (TabDict != null && TabDict.ContainsKey(tab))
  243. {
  244. foreach (string key in TabDict.Keys)
  245. {
  246. Border checkBorder = TabDict[key];
  247. if (checkBorder == null)
  248. {
  249. continue;
  250. }
  251. checkBorder.BorderThickness = new Thickness(0);
  252. if (key == tab)
  253. {
  254. checkBorder.BorderThickness = new Thickness(0, 0, 0, 2);
  255. }
  256. }
  257. }
  258. }
  259. private void CanvasPopupClose_Click(object sender, RoutedEventArgs e)
  260. {
  261. CanvaDrawPopup.Visibility = Visibility.Collapsed;
  262. }
  263. private void CanvasClearBtn_Click(object sender, RoutedEventArgs e)
  264. {
  265. DrawInkCanvas.Strokes.Clear();
  266. }
  267. private void CanvasPopupConfirm_Click(object sender, RoutedEventArgs e)
  268. {
  269. int height = 0;
  270. int width = 0;
  271. tempSignatureConfig.ImageData = GetDrawInk(ref height,ref width);
  272. tempSignatureConfig.ImageWidth = width;
  273. tempSignatureConfig.ImageHeight = height;
  274. CanvaDrawPopup.Visibility = Visibility.Collapsed;
  275. SetProperty();
  276. CreateTempSignature();
  277. }
  278. public byte[] GetDrawInk(ref int height, ref int width)
  279. {
  280. if (DrawInkCanvas != null && DrawInkCanvas.Strokes != null && DrawInkCanvas.Strokes.Count > 0)
  281. {
  282. Rect bound = DrawInkCanvas.Strokes.GetBounds();
  283. DrawingVisual drawVisual = new DrawingVisual();
  284. DrawingContext drawContext = drawVisual.RenderOpen();
  285. foreach (Stroke drawStroke in DrawInkCanvas.Strokes)
  286. {
  287. Pen drawPen = new Pen(new SolidColorBrush(drawStroke.DrawingAttributes.Color), drawStroke.DrawingAttributes.Width);
  288. PathGeometry drawPath = new PathGeometry();
  289. PathFigureCollection Figures = new PathFigureCollection();
  290. PathFigure AddFigure = new PathFigure();
  291. Figures.Add(AddFigure);
  292. drawPath.Figures = Figures;
  293. if (drawStroke.StylusPoints.Count > 1)
  294. {
  295. StylusPoint startPoint = drawStroke.StylusPoints[0];
  296. AddFigure.StartPoint = new Point(startPoint.X - bound.X, startPoint.Y - bound.Y);
  297. for (int i = 1; i < drawStroke.StylusPoints.Count; i++)
  298. {
  299. StylusPoint drawPoint = drawStroke.StylusPoints[i];
  300. Point offsetPoint = new Point(drawPoint.X - bound.X, drawPoint.Y - bound.Y);
  301. LineSegment drawSegment = new LineSegment();
  302. drawSegment.Point = offsetPoint;
  303. AddFigure.Segments.Add(drawSegment);
  304. }
  305. }
  306. if (AddFigure.Segments.Count > 0)
  307. {
  308. drawContext.DrawGeometry(null, drawPen, drawPath);
  309. }
  310. }
  311. drawContext.Close();
  312. RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)bound.Width, (int)bound.Height, 96, 96, PixelFormats.Pbgra32);
  313. renderBitmap.Render(drawVisual);
  314. BitmapFrame newFrame = BitmapFrame.Create(renderBitmap);
  315. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  316. pngEncoder.Frames.Add(newFrame);
  317. using (MemoryStream newStream = new MemoryStream())
  318. {
  319. pngEncoder.Save(newStream);
  320. Bitmap bitmap = new Bitmap(newStream);
  321. height = bitmap.Height;
  322. width = bitmap.Width;
  323. return GetBitmapData(bitmap);
  324. }
  325. }
  326. else
  327. {
  328. return null;
  329. }
  330. }
  331. private byte[] GetBitmapData(Bitmap bitmap)
  332. {
  333. // Lock the bitmap's bits.
  334. Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
  335. BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
  336. // Get the address of the first line.
  337. IntPtr ptr = bmpData.Scan0;
  338. // Declare an array to hold the bytes of the bitmap.
  339. int bytes = Math.Abs(bmpData.Stride) * bitmap.Height;
  340. byte[] rgbValues = new byte[bytes];
  341. // Copy the RGB values into the array.
  342. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
  343. // Unlock the bits.
  344. bitmap.UnlockBits(bmpData);
  345. return rgbValues;
  346. }
  347. private void SetProperty()
  348. {
  349. Text = string.Empty;
  350. if ((bool)NameChk.IsChecked)
  351. {
  352. if ((bool)TabChk.IsChecked)
  353. {
  354. Text += "Name: ";
  355. }
  356. Text += DictionaryValueConverter.GetGrantorFromDictionary(signatureCertificate.SubjectDict) + "\n";
  357. }
  358. if ((bool)DateChk.IsChecked)
  359. {
  360. if ((bool)TabChk.IsChecked)
  361. {
  362. Text += "Date: ";
  363. }
  364. DateTime currentDateTime = DateTime.Now;
  365. string customFormat = "yyyy.MM.dd HH:mm:ss";
  366. string formattedDateTime = currentDateTime.ToString(customFormat);
  367. Text += formattedDateTime + "\n";
  368. }
  369. if ((bool)LogoChk.IsChecked)
  370. {
  371. tempSignatureConfig.IsDrawLogo = true;
  372. }
  373. else
  374. {
  375. tempSignatureConfig.IsDrawLogo = false;
  376. }
  377. if ((bool)ReasonChk.IsChecked)
  378. {
  379. if ((bool)TabChk.IsChecked)
  380. {
  381. Text += "Reason: ";
  382. }
  383. Text += (ReasonCmb.SelectedItem as ComboBoxItem).Content.ToString() + "\n";
  384. }
  385. if ((bool)DistinguishableNameChk.IsChecked)
  386. {
  387. if ((bool)TabChk.IsChecked)
  388. {
  389. Text += "DN: ";
  390. }
  391. var keyOrder = new List<string> { "CN", "O", "OU", "emailAddress", "L", "ST", "C" };
  392. var keyMapping = new Dictionary<string, string>
  393. {
  394. { "CN", "cn" },
  395. { "OU", "ou" },
  396. { "O", "o" },
  397. { "L", "l" },
  398. { "ST", "st" },
  399. { "C", "c" },
  400. { "emailAddress", "email" }
  401. };
  402. var stringBuilder = new StringBuilder();
  403. foreach (var originalKey in keyOrder)
  404. {
  405. if (keyMapping.TryGetValue(originalKey, out string newKey) &&
  406. signatureCertificate.SubjectDict.TryGetValue(originalKey, out string value) && !string.IsNullOrEmpty(value))
  407. {
  408. if (stringBuilder.Length > 0)
  409. {
  410. stringBuilder.Append(",");
  411. }
  412. stringBuilder.Append(newKey + "=" + value);
  413. }
  414. }
  415. Text += stringBuilder.ToString()+"\n";
  416. }
  417. if ((bool)ComPDFKitVersionChk.IsChecked)
  418. {
  419. Assembly assembly = Assembly.GetExecutingAssembly();
  420. Version version = assembly.GetName().Version;
  421. if ((bool)TabChk.IsChecked)
  422. {
  423. Text += "ComPDFKit Version: ";
  424. }
  425. Text += version.Major.ToString() + "." + version.Minor.ToString() + "." + version.Build.ToString() + "." + version.Revision.ToString() + "\n";
  426. }
  427. if ((bool)PositionChk.IsChecked)
  428. {
  429. if ((bool)TabChk.IsChecked)
  430. {
  431. Text += "Position: ";
  432. }
  433. Text += PositionTbx.Text + "\n";
  434. }
  435. }
  436. private void ReasonCheckBox_Click(object sender, RoutedEventArgs e)
  437. {
  438. CheckBox checkItem = sender as CheckBox;
  439. if (checkItem == null)
  440. {
  441. return;
  442. }
  443. ReasonPanel.Visibility = checkItem.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
  444. }
  445. internal static Bitmap ConvertToBitmapARGB(Bitmap bitmap)
  446. {
  447. try
  448. {
  449. if (bitmap == null || bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  450. {
  451. return bitmap;
  452. }
  453. using (MemoryStream streamData = new MemoryStream())
  454. {
  455. bitmap.Save(streamData, ImageFormat.Png);
  456. BitmapDecoder decoder = BitmapDecoder.Create(streamData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  457. if (decoder != null && decoder.Frames.Count > 0)
  458. {
  459. BitmapFrame frame = decoder.Frames[0];
  460. byte[] imageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  461. if (frame.Format != PixelFormats.Bgra32)
  462. {
  463. FormatConvertedBitmap formatCovert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
  464. formatCovert.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  465. }
  466. else
  467. {
  468. frame.CopyPixels(imageArray, frame.PixelWidth * 4, 0);
  469. }
  470. WriteableBitmap writeBitmap = new WriteableBitmap(frame.PixelWidth, frame.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
  471. writeBitmap.WritePixels(new Int32Rect(0, 0, frame.PixelWidth, frame.PixelHeight), imageArray, frame.PixelWidth * 4, 0);
  472. BitmapFrame newFrame = BitmapFrame.Create(writeBitmap);
  473. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  474. pngEncoder.Frames.Add(newFrame);
  475. using (MemoryStream newStream = new MemoryStream())
  476. {
  477. pngEncoder.Save(newStream);
  478. return new Bitmap(newStream);
  479. }
  480. }
  481. }
  482. }
  483. catch (Exception ex)
  484. {
  485. }
  486. return null;
  487. }
  488. private void BrowseTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  489. {
  490. string filter = "Image files (*.jpg, *.jpeg, *.png, *.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
  491. string pngPath = CommonHelper.GetExistedPathOrEmpty(filter);
  492. if (!string.IsNullOrEmpty(pngPath))
  493. {
  494. imagePath = pngPath;
  495. try
  496. {
  497. Bitmap bitmap = new Bitmap(imagePath);
  498. Bitmap newImageBitmap = null;
  499. BitmapData bitmapData = null;
  500. IntPtr bitmapPtr = IntPtr.Zero;
  501. if (bitmap != null)
  502. {
  503. newImageBitmap = ConvertToBitmapARGB(bitmap);
  504. Rectangle rect = new Rectangle(0, 0, newImageBitmap.Width, newImageBitmap.Height);
  505. bitmapData = newImageBitmap.LockBits(rect, ImageLockMode.ReadOnly, newImageBitmap.PixelFormat);
  506. bitmapPtr = bitmapData.Scan0;
  507. int bytes = Math.Abs(bitmapData.Stride) * bitmap.Height;
  508. byte[] rgbValues = new byte[bytes];
  509. Marshal.Copy(bitmapData.Scan0, rgbValues, 0, bytes);
  510. tempSignatureConfig.ImageData = rgbValues;
  511. tempSignatureConfig.ImageWidth = bitmapData.Width;
  512. tempSignatureConfig.ImageHeight = bitmapData.Height;
  513. }
  514. }
  515. catch (Exception exception)
  516. {
  517. MessageBox.Show("The image is invalid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  518. return;
  519. }
  520. SetProperty();
  521. CreateTempSignature();
  522. }
  523. }
  524. private void ClearTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  525. {
  526. imagePath = string.Empty;
  527. //tempSignatureConfig.ImageBitmap = null;
  528. SetProperty();
  529. CreateTempSignature();
  530. }
  531. private void NameChk_Click(object sender, RoutedEventArgs e)
  532. {
  533. SetProperty();
  534. CreateTempSignature();
  535. }
  536. private void DateChk_Click(object sender, RoutedEventArgs e)
  537. {
  538. SetProperty();
  539. CreateTempSignature();
  540. }
  541. private void LogoChk_Click(object sender, RoutedEventArgs e)
  542. {
  543. SetProperty();
  544. CreateTempSignature();
  545. }
  546. private void ReasonChk_Click(object sender, RoutedEventArgs e)
  547. {
  548. if (!(bool)ReasonChk.IsChecked)
  549. {
  550. Reasonstp.Visibility = Visibility.Collapsed;
  551. }
  552. else
  553. {
  554. Reasonstp.Visibility = Visibility.Visible;
  555. }
  556. SetProperty();
  557. CreateTempSignature();
  558. }
  559. private void DistinguishableNameChk_Click(object sender, RoutedEventArgs e)
  560. {
  561. SetProperty();
  562. CreateTempSignature();
  563. }
  564. private void PositionChk_Click(object sender, RoutedEventArgs e)
  565. {
  566. if (!(bool)PositionChk.IsChecked)
  567. {
  568. PositionStp.Visibility = Visibility.Collapsed;
  569. }
  570. else
  571. {
  572. PositionStp.Visibility = Visibility.Visible;
  573. }
  574. SetProperty();
  575. CreateTempSignature();
  576. }
  577. private void TabChk_Click(object sender, RoutedEventArgs e)
  578. {
  579. SetProperty();
  580. CreateTempSignature();
  581. }
  582. private void ReasonCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  583. {
  584. SetProperty();
  585. CreateTempSignature();
  586. }
  587. private void ComPDFKitVersionChk_Click(object sender, RoutedEventArgs e)
  588. {
  589. SetProperty();
  590. CreateTempSignature();
  591. }
  592. private void PositionTbx_TextChanged(object sender, TextChangedEventArgs e)
  593. {
  594. if (!(bool)PositionChk.IsChecked)
  595. {
  596. PositionStp.Visibility = Visibility.Collapsed;
  597. }
  598. else
  599. {
  600. PositionStp.Visibility = Visibility.Visible;
  601. }
  602. SetProperty();
  603. CreateTempSignature();
  604. }
  605. private void ContinueBtn_Click(object sender, RoutedEventArgs e)
  606. {
  607. string filePath = CommonHelper.GetGeneratePathOrEmpty("PDF files (*.pdf)|*.pdf", Document.FileName + "_Signed.pdf");
  608. if (string.IsNullOrEmpty(filePath))
  609. {
  610. return;
  611. }
  612. if (filePath.ToLower() == Document.FilePath.ToLower())
  613. {
  614. MessageBox.Show("Do not use the new file name that is the same as the current file name.");
  615. return;
  616. }
  617. if ((bool)ReasonChk.IsChecked)
  618. {
  619. reason = (ReasonCmb?.SelectedItem as ComboBoxItem)?.Content?.ToString();
  620. }
  621. else
  622. {
  623. reason = string.Empty;
  624. }
  625. if ((bool)PositionChk.IsChecked)
  626. {
  627. location = PositionTbx.Text;
  628. }
  629. else
  630. {
  631. location = string.Empty;
  632. }
  633. signatureWidget.UpdataApWithSignature(tempSignatureConfig);
  634. if (Document.WriteSignatureToFilePath(signatureWidget, filePath, SignaturePath, Password, location, reason, CPDFSignaturePermissions.CPDFSignaturePermissionsNone))
  635. {
  636. signatureCertificate.AddToTrustedCertificates();
  637. AfterFillSignature?.Invoke(sender, filePath);
  638. }
  639. CloseWindow(sender);
  640. }
  641. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  642. {
  643. CloseWindow(sender);
  644. }
  645. private void CloseWindow(object sender)
  646. {
  647. Window parentWindow = Window.GetWindow(sender as DependencyObject);
  648. parentWindow?.Close();
  649. }
  650. private void TextColorPickerControl_Loaded(object sender, RoutedEventArgs e)
  651. {
  652. TextColorPickerControl.SetIsChecked(0);
  653. TextColorPickerControl.Brush = new SolidColorBrush(Colors.Black);
  654. }
  655. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  656. {
  657. KeyboardInPutTextBox.Foreground = TextColorPickerControl.Brush;
  658. }
  659. private void KeyboardCancel_Click(object sender, RoutedEventArgs e)
  660. {
  661. KeyboardPopup.Visibility = Visibility.Collapsed;
  662. }
  663. private void KeyboardClear_Click(object sender, RoutedEventArgs e)
  664. {
  665. KeyboardInPutTextBox.Text = string.Empty;
  666. }
  667. private void KeyboardSave_Click(object sender, RoutedEventArgs e)
  668. {
  669. signatureName = KeyboardInPutTextBox.Text;
  670. tempSignatureConfig.Text = signatureName;
  671. SolidColorBrush solidColorBrush = TextColorPickerControl.Brush as SolidColorBrush;
  672. float red = solidColorBrush.Color.R;
  673. float green = solidColorBrush.Color.G;
  674. float blue = solidColorBrush.Color.B;
  675. textColor = new[] { red / 255, green / 255, blue / 255 };
  676. KeyboardPopup.Visibility = Visibility.Collapsed;
  677. SetProperty();
  678. CreateTempSignature();
  679. }
  680. private void KeyboardPopupClose_Click(object sender, RoutedEventArgs e)
  681. {
  682. KeyboardPopup.Visibility = Visibility.Collapsed;
  683. }
  684. }
  685. }