FillDigitalSignatureControl.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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.Reflection;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows.Ink;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  20. using Pen = System.Windows.Media.Pen;
  21. using Point = System.Windows.Point;
  22. using Window = System.Windows.Window;
  23. namespace Compdfkit_Tools.PDFControl
  24. {
  25. /// <summary>
  26. /// CPDFSignControl.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class FillDigitalSignatureControl : UserControl
  29. {
  30. private Dictionary<string, Border> TabDict { get; set; }
  31. private SignatureConfig tempSignatureConfig = new SignatureConfig();
  32. private CPDFSignatureCertificate signatureCertificate;
  33. public CPDFDocument Document;
  34. private string location = string.Empty;
  35. private string reason = string.Empty;
  36. private string _filePath = string.Empty;
  37. public string signaturePath
  38. {
  39. get => _filePath;
  40. set
  41. {
  42. _filePath = value;
  43. }
  44. }
  45. private string _password = string.Empty;
  46. public string Password
  47. {
  48. get => _password;
  49. set
  50. {
  51. _password = value;
  52. signatureCertificate = CPDFPKCS12CertHelper.GetCertificateWithPKCS12Path(signaturePath, Password);
  53. }
  54. }
  55. public CPDFSignatureWidget signatureWidget { get; set; }
  56. private readonly string logoPath = "Logo.png";
  57. private string imagePath = string.Empty;
  58. private string Text = string.Empty;
  59. public FillDigitalSignatureControl()
  60. {
  61. InitializeComponent();
  62. TabDict = new Dictionary<string, Border>
  63. {
  64. ["Keyboard"] = KeyboardBorder,
  65. ["Trackpad"] = TrackpadBorder,
  66. ["Image"] = ImageBorder,
  67. ["None"] = NoneBorder
  68. };
  69. SetCheckedTab("Keyboard");
  70. CreateTempSignature();
  71. }
  72. private void CreateTempSignature()
  73. {
  74. CPDFDocument tempDocument = CPDFDocument.CreateDocument();
  75. tempDocument.InsertPage(0, 200, 200, string.Empty);
  76. CPDFPage page = tempDocument.PageAtIndex(0);
  77. CPDFSignatureWidget signatureWidget = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
  78. signatureWidget.SetRect(new CRect(0, 100, 300, 0));
  79. tempSignatureConfig.IsDrawLogo = (bool)LogoChk.IsChecked;
  80. tempSignatureConfig.LogoBitmap = new Bitmap(logoPath);
  81. tempSignatureConfig.Content = Text;
  82. signatureWidget.UpdataApWithSignature(tempSignatureConfig);
  83. byte[] signatureBitmapBytes = GetTempSignatureImage(signatureWidget, out int width, out int height);
  84. signatureWidget.ReleaseAnnot();
  85. if (signatureBitmapBytes.Length > 0)
  86. {
  87. PixelFormat fmt = PixelFormats.Bgra32;
  88. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, signatureBitmapBytes, (width * fmt.BitsPerPixel + 7) / 8);
  89. imageControl.Source = bps;
  90. }
  91. else
  92. {
  93. imageControl.Source = null;
  94. }
  95. }
  96. public static byte[] GetTempSignatureImage(CPDFSignatureWidget signatureWidget, out int width, out int height)
  97. {
  98. CRect rect = signatureWidget.GetRect();
  99. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  100. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  101. int dpi = (int)dpiProperty.GetValue(null, null);
  102. width = (int)(rect.width() * dpi / 72D * 2);
  103. height = (int)(rect.height() * dpi / 72D * 2);
  104. byte[] imageData = new byte[width * height * 4];
  105. signatureWidget.RenderAnnot(width, height, imageData, CPDFAppearanceType.Normal);
  106. return imageData;
  107. }
  108. private void TextAlignBtn_Click(object sender, RoutedEventArgs e)
  109. {
  110. ToggleButton checkBtn = sender as ToggleButton;
  111. if (checkBtn == null)
  112. {
  113. return;
  114. }
  115. checkBtn.IsChecked = true;
  116. if (checkBtn != TextAlignLeftBtn)
  117. {
  118. tempSignatureConfig.IsContentAlginLeft = true;
  119. TextAlignLeftBtn.IsChecked = false;
  120. }
  121. if (checkBtn != TextAlignRightBtn)
  122. {
  123. tempSignatureConfig.IsContentAlginLeft = false;
  124. TextAlignRightBtn.IsChecked = false;
  125. }
  126. CreateTempSignature();
  127. }
  128. private void Border_MouseDown(object sender, MouseButtonEventArgs e)
  129. {
  130. Border clickBorder = sender as Border;
  131. if (clickBorder == null || clickBorder.Tag == null)
  132. {
  133. return;
  134. }
  135. SetCheckedTab(clickBorder.Tag.ToString());
  136. ImagePickPanel.Visibility = Visibility.Hidden;
  137. if (clickBorder == NoneBorder)
  138. {
  139. tempSignatureConfig.IsDrawOnlyContent = true;
  140. }
  141. else
  142. {
  143. if (clickBorder == KeyboardBorder)
  144. {
  145. tempSignatureConfig.ImageBitmap = null;
  146. }
  147. else
  148. {
  149. if (clickBorder == TrackpadBorder)
  150. {
  151. CanvaDrawPopup.Visibility = Visibility.Visible;
  152. }
  153. else if (clickBorder == ImageBorder)
  154. {
  155. if (!string.IsNullOrEmpty(imagePath))
  156. {
  157. tempSignatureConfig.ImageBitmap = new Bitmap(imagePath);
  158. }
  159. tempSignatureConfig.Content = string.Empty;
  160. }
  161. }
  162. }
  163. CreateTempSignature();
  164. }
  165. private void SetCheckedTab(string tab)
  166. {
  167. if (TabDict != null && TabDict.ContainsKey(tab))
  168. {
  169. foreach (string key in TabDict.Keys)
  170. {
  171. Border checkBorder = TabDict[key];
  172. if (checkBorder == null)
  173. {
  174. continue;
  175. }
  176. checkBorder.BorderThickness = new Thickness(0);
  177. if (key == tab)
  178. {
  179. checkBorder.BorderThickness = new Thickness(0, 0, 0, 2);
  180. }
  181. }
  182. }
  183. }
  184. private void CanvasPopupClose_Click(object sender, RoutedEventArgs e)
  185. {
  186. CanvaDrawPopup.Visibility = Visibility.Collapsed;
  187. }
  188. private void CanvasClearBtn_Click(object sender, RoutedEventArgs e)
  189. {
  190. DrawInkCanvas.Strokes.Clear();
  191. }
  192. public void GetDrawInk()
  193. {
  194. if (DrawInkCanvas != null && DrawInkCanvas.Strokes != null && DrawInkCanvas.Strokes.Count > 0)
  195. {
  196. Rect bound = DrawInkCanvas.Strokes.GetBounds();
  197. DrawingVisual drawVisual = new DrawingVisual();
  198. DrawingContext drawContext = drawVisual.RenderOpen();
  199. foreach (Stroke drawStroke in DrawInkCanvas.Strokes)
  200. {
  201. Pen drawPen = new Pen(new SolidColorBrush(drawStroke.DrawingAttributes.Color), drawStroke.DrawingAttributes.Width);
  202. PathGeometry drawPath = new PathGeometry();
  203. PathFigureCollection Figures = new PathFigureCollection();
  204. PathFigure AddFigure = new PathFigure();
  205. Figures.Add(AddFigure);
  206. drawPath.Figures = Figures;
  207. if (drawStroke.StylusPoints.Count > 1)
  208. {
  209. StylusPoint startPoint = drawStroke.StylusPoints[0];
  210. AddFigure.StartPoint = new Point(startPoint.X - bound.X, startPoint.Y - bound.Y);
  211. for (int i = 1; i < drawStroke.StylusPoints.Count; i++)
  212. {
  213. StylusPoint drawPoint = drawStroke.StylusPoints[i];
  214. Point offsetPoint = new Point(drawPoint.X - bound.X, drawPoint.Y - bound.Y);
  215. LineSegment drawSegment = new LineSegment();
  216. drawSegment.Point = offsetPoint;
  217. AddFigure.Segments.Add(drawSegment);
  218. }
  219. }
  220. if (AddFigure.Segments.Count > 0)
  221. {
  222. drawContext.DrawGeometry(null, drawPen, drawPath);
  223. }
  224. }
  225. drawContext.Close();
  226. RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)bound.Width, (int)bound.Height, 96, 96, PixelFormats.Pbgra32);
  227. renderBitmap.Render(drawVisual);
  228. }
  229. }
  230. private void SetProperty()
  231. {
  232. Text = string.Empty;
  233. if ((bool)NameChk.IsChecked)
  234. {
  235. if ((bool)TabChk.IsChecked)
  236. {
  237. Text += "Name: ";
  238. }
  239. Text += DictionaryValueConverter.GetGrantorFormDictionary(signatureCertificate.SubjectDict) + "\n";
  240. }
  241. if ((bool)DateChk.IsChecked)
  242. {
  243. if ((bool)TabChk.IsChecked)
  244. {
  245. Text += "Date: ";
  246. }
  247. DateTime currentDateTime = DateTime.Now;
  248. string customFormat = "yyyy.MM.dd HH:mm:ss";
  249. string formattedDateTime = currentDateTime.ToString(customFormat);
  250. Text += formattedDateTime + "\n";
  251. }
  252. if ((bool)LogoChk.IsChecked)
  253. {
  254. tempSignatureConfig.IsDrawLogo = true;
  255. }
  256. else
  257. {
  258. tempSignatureConfig.IsDrawLogo = false;
  259. }
  260. if ((bool)ReasonChk.IsChecked)
  261. {
  262. if ((bool)TabChk.IsChecked)
  263. {
  264. Text += "Reason: ";
  265. }
  266. Text += (ReasonCmb.SelectedItem as ComboBoxItem).Content.ToString() + "\n";
  267. }
  268. if ((bool)DistinguishableNameChk.IsChecked)
  269. {
  270. if ((bool)TabChk.IsChecked)
  271. {
  272. Text += "DN: ";
  273. }
  274. Text += DictionaryValueConverter.GetDNFromDictionary(signatureCertificate.SubjectDict) + "\n";
  275. }
  276. if ((bool)ComPDFKitVersionChk.IsChecked)
  277. {
  278. Assembly assembly = Assembly.GetExecutingAssembly();
  279. Version version = assembly.GetName().Version;
  280. if ((bool)TabChk.IsChecked)
  281. {
  282. Text += "ComPDFKit Version: ";
  283. }
  284. Text += version.Major.ToString() + "." + version.Minor.ToString() + "." + version.Build.ToString() + "." + version.Revision.ToString() + "\n";
  285. }
  286. if ((bool)PositionChk.IsChecked)
  287. {
  288. if ((bool)TabChk.IsChecked)
  289. {
  290. Text += "Location: ";
  291. }
  292. Text += PositionTbx.Text + "\n";
  293. }
  294. }
  295. private void ReasonCheckBox_Click(object sender, RoutedEventArgs e)
  296. {
  297. CheckBox checkItem = sender as CheckBox;
  298. if (checkItem == null)
  299. {
  300. return;
  301. }
  302. ReasonPanel.Visibility = checkItem.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
  303. }
  304. private void BrowseTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  305. {
  306. string pngPath = CommonHelper.GetExistedPathOrEmpty("PNG files (*.png)|*.png");
  307. if (pngPath != string.Empty)
  308. {
  309. imagePath = CommonHelper.GetExistedPathOrEmpty(pngPath);
  310. }
  311. else
  312. {
  313. }
  314. }
  315. private void ClearTxt_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  316. {
  317. }
  318. private void NameChk_Click(object sender, RoutedEventArgs e)
  319. {
  320. SetProperty();
  321. CreateTempSignature();
  322. }
  323. private void DateChk_Click(object sender, RoutedEventArgs e)
  324. {
  325. SetProperty();
  326. CreateTempSignature();
  327. }
  328. private void LogoChk_Click(object sender, RoutedEventArgs e)
  329. {
  330. SetProperty();
  331. CreateTempSignature();
  332. }
  333. private void ReasonChk_Click(object sender, RoutedEventArgs e)
  334. {
  335. if (!(bool)ReasonChk.IsChecked)
  336. {
  337. Reasonstp.Visibility = Visibility.Collapsed;
  338. }
  339. else
  340. {
  341. Reasonstp.Visibility = Visibility.Visible;
  342. }
  343. SetProperty();
  344. CreateTempSignature();
  345. }
  346. private void DistinguishableNameChk_Click(object sender, RoutedEventArgs e)
  347. {
  348. SetProperty();
  349. CreateTempSignature();
  350. }
  351. private void PositionChk_Click(object sender, RoutedEventArgs e)
  352. {
  353. if (!(bool)PositionChk.IsChecked)
  354. {
  355. PositionStp.Visibility = Visibility.Collapsed;
  356. }
  357. else
  358. {
  359. PositionStp.Visibility = Visibility.Visible;
  360. }
  361. SetProperty();
  362. CreateTempSignature();
  363. }
  364. private void TabChk_Click(object sender, RoutedEventArgs e)
  365. {
  366. SetProperty();
  367. CreateTempSignature();
  368. }
  369. private void ReasonCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  370. {
  371. SetProperty();
  372. CreateTempSignature();
  373. }
  374. private void ComPDFKitVersionChk_Click(object sender, RoutedEventArgs e)
  375. {
  376. SetProperty();
  377. CreateTempSignature();
  378. }
  379. private void PositionTbx_TextChanged(object sender, TextChangedEventArgs e)
  380. {
  381. if (!(bool)PositionChk.IsChecked)
  382. {
  383. PositionStp.Visibility = Visibility.Collapsed;
  384. }
  385. else
  386. {
  387. PositionStp.Visibility = Visibility.Visible;
  388. }
  389. SetProperty();
  390. CreateTempSignature();
  391. }
  392. private void ContinueBtn_Click(object sender, RoutedEventArgs e)
  393. {
  394. string filePath = CommonHelper.GetGeneratePathOrEmpty("PDF files (*.pdf)|*.pdf");
  395. if (!string.IsNullOrEmpty(filePath))
  396. {
  397. if ((bool)ReasonChk.IsChecked)
  398. {
  399. reason = (ReasonCmb?.SelectedItem as ComboBoxItem)?.Content?.ToString();
  400. }
  401. else
  402. {
  403. reason = string.Empty;
  404. }
  405. if ((bool)PositionChk.IsChecked)
  406. {
  407. location = PositionTbx.Text;
  408. }
  409. else
  410. {
  411. location = string.Empty;
  412. }
  413. signatureWidget.UpdataApWithSignature(tempSignatureConfig);
  414. Document.WriteSignatureToFilePath(signatureWidget, filePath, signaturePath, Password, location, reason, false);
  415. }
  416. }
  417. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  418. {
  419. Window parentWindow = Window.GetWindow(sender as DependencyObject);
  420. parentWindow?.Close();
  421. }
  422. }
  423. }