FillDigitalSignatureControl.xaml.cs 16 KB

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