DigitalSignatureControl.xaml.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. using Compdfkit_Tools.PDFControl;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.ComponentModel;
  6. using System.Dynamic;
  7. using System.IO;
  8. using System.Runtime.CompilerServices;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using Compdfkit_Tools.Helper;
  15. using ComPDFKit.DigitalSign;
  16. using ComPDFKit.PDFAnnotation.Form;
  17. using ComPDFKitViewer;
  18. namespace Compdfkit_Tools.PDFControl
  19. {
  20. public partial class DigitalSignatureControl : UserControl, INotifyPropertyChanged
  21. {
  22. private bool isFirstLoad = true;
  23. public PDFViewControl PDFViewControl = new PDFViewControl();
  24. private SignatureStatusBarControl signatureStatusBarControl;
  25. private PanelState panelState = PanelState.GetInstance();
  26. private CPDFDisplaySettingsControl displaySettingsControl = null;
  27. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  28. public event EventHandler<bool> OnCanSaveChanged;
  29. private CPDFSignatureWidget currentSignatureWidget;
  30. public event EventHandler<string> AfterFillSignature;
  31. public event EventHandler SignatureStatusChanged;
  32. public event PropertyChangedEventHandler PropertyChanged;
  33. protected void OnPropertyChanged([CallerMemberName] string name = null)
  34. {
  35. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  36. }
  37. public bool CanUndo
  38. {
  39. get
  40. {
  41. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  42. {
  43. return PDFViewControl.PDFView.UndoManager.CanUndo;
  44. }
  45. return false;
  46. }
  47. }
  48. public bool CanRedo
  49. {
  50. get
  51. {
  52. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  53. {
  54. return PDFViewControl.PDFView.UndoManager.CanRedo;
  55. }
  56. return false;
  57. }
  58. }
  59. private bool CanSave
  60. {
  61. get
  62. {
  63. if (PDFViewControl != null && PDFViewControl.PDFView != null)
  64. {
  65. return PDFViewControl.PDFView.UndoManager.CanSave;
  66. }
  67. return false;
  68. }
  69. }
  70. public DigitalSignatureControl()
  71. {
  72. InitializeComponent();
  73. DataContext = this;
  74. string trustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
  75. if (!Directory.Exists(trustedFolder))
  76. {
  77. Directory.CreateDirectory(trustedFolder);
  78. }
  79. CPDFSignature.SignCertTrustedFolder = trustedFolder;
  80. }
  81. public void ClearViewerControl()
  82. {
  83. PDFGrid.Child = null;
  84. BotaContainer.Child = null;
  85. PropertyContainer.Child = null;
  86. displaySettingsControl = null;
  87. SignatureStatusBorder.Child = null;
  88. DigitalSignatureBarControl.ClearAllToolState();
  89. }
  90. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  91. {
  92. PDFViewControl.PDFView = pdfViewer;
  93. PDFGrid.Child = PDFViewControl;
  94. FloatPageTool.InitWithPDFViewer(pdfViewer);
  95. InitialControl();
  96. DigitalSignatureBarControl.DigitalSignatureActionChanged -= DigitalSignatureBarControl_DigitalSignatureActionChanged;
  97. DigitalSignatureBarControl.DigitalSignatureActionChanged += DigitalSignatureBarControl_DigitalSignatureActionChanged;
  98. PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  99. PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  100. PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  101. PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  102. panelState.PropertyChanged -= PanelState_PropertyChanged;
  103. panelState.PropertyChanged += PanelState_PropertyChanged;
  104. }
  105. private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  106. {
  107. var signatureWidget = (e as WidgetSignArgs).Sign;
  108. CPDFSignature sig = signatureWidget.GetSignature();
  109. if (signatureWidget.IsSigned() && sig.SignerList.Count > 0)
  110. {
  111. ViewSignatureEvent(sender, sig);
  112. }
  113. else
  114. {
  115. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  116. AddCertificationDialog addCertificationControl = new AddCertificationDialog
  117. {
  118. Owner = parentWindow
  119. };
  120. currentSignatureWidget = signatureWidget;
  121. addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  122. addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  123. addCertificationControl.ShowDialog();
  124. }
  125. }
  126. private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
  127. {
  128. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
  129. {
  130. FilePath = e.filePath,
  131. Password = e.password,
  132. SignatureWidget = currentSignatureWidget,
  133. Document = PDFViewControl.PDFView.Document
  134. };
  135. fillDigitalSignatureDialog.AfterFillSignature += FillDigitalSignatureDialog_AfterFillSignature; ;
  136. fillDigitalSignatureDialog.ShowDialog();
  137. }
  138. private void FillDigitalSignatureDialog_AfterFillSignature(object sender, string e)
  139. {
  140. AfterFillSignature?.Invoke(this, e);
  141. }
  142. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  143. {
  144. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatureField)
  145. {
  146. CreateSign();
  147. }
  148. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  149. {
  150. PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  151. }
  152. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  153. {
  154. ToggleButton button = sender as ToggleButton;
  155. button.IsChecked = false;
  156. SignatureStatusChanged?.Invoke(this, null);
  157. }
  158. }
  159. private string GetTime()
  160. {
  161. DateTime dateTime = DateTime.Now;
  162. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  163. }
  164. private void CreateSign()
  165. {
  166. PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
  167. WidgetSignArgs signArgs = new WidgetSignArgs
  168. {
  169. LineWidth = 1,
  170. LineColor = Colors.Black,
  171. FieldName = "Signature" + GetTime()
  172. };
  173. PDFViewControl.PDFView.SetToolParam(signArgs);
  174. }
  175. private void InitialControl()
  176. {
  177. PDFViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  178. //PDFViewControl.PDFView?.Load();
  179. PDFViewControl.PDFView?.SetShowLink(true);
  180. PDFGrid.Child = PDFViewControl;
  181. PDFViewControl.PDFView.SetFormFieldHighlight(true);
  182. }
  183. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  184. {
  185. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  186. {
  187. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  188. }
  189. else if (e.PropertyName == nameof(PanelState.RightPanel))
  190. {
  191. if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  192. {
  193. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  194. }
  195. else
  196. {
  197. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  198. }
  199. }
  200. }
  201. public void ExpandLeftPanel(bool isExpand)
  202. {
  203. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  204. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  205. if (isExpand)
  206. {
  207. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  208. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  209. }
  210. else
  211. {
  212. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  213. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  214. }
  215. }
  216. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  217. {
  218. PropertyContainer.Width = 260;
  219. PropertyContainer.Child = propertytPanel;
  220. PropertyContainer.Visibility = visible;
  221. }
  222. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  223. {
  224. BotaContainer.Child = botaControl;
  225. }
  226. public void ViewCertificateEvent(object sender, CPDFSignature e)
  227. {
  228. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  229. ViewCertificateDialog dialog = new ViewCertificateDialog()
  230. {
  231. Owner = parentWindow
  232. };
  233. dialog.InitCertificateList(e);
  234. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  235. {
  236. SignatureStatusChanged?.Invoke(this, null);
  237. };
  238. VerifyDigitalSignatureControl verifyControl = parentWindow as VerifyDigitalSignatureControl;
  239. if (verifyControl != null)
  240. {
  241. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  242. {
  243. verifyControl.InitWithSignature(e);
  244. };
  245. }
  246. dialog.ShowDialog();
  247. }
  248. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  249. {
  250. this.displaySettingsControl = displaySettingsControl;
  251. }
  252. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  253. {
  254. this.signatureStatusBarControl = signatureStatusBarControl;
  255. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  256. if (signatureStatusBarControl.Status != SignatureStatus.None)
  257. {
  258. SignatureStatusBorder.Visibility = Visibility.Visible;
  259. }
  260. else
  261. {
  262. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  263. }
  264. }
  265. /// <summary>
  266. /// Undo Redo Event Noitfy
  267. /// </summary>
  268. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  269. {
  270. OnPropertyChanged(e.PropertyName);
  271. if (e.PropertyName == "CanSave")
  272. {
  273. OnCanSaveChanged?.Invoke(this, CanSave);
  274. }
  275. }
  276. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  277. {
  278. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  279. {
  280. PDFViewControl.PDFView.UndoManager?.Undo();
  281. }
  282. }
  283. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  284. {
  285. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  286. {
  287. PDFViewControl.PDFView.UndoManager?.Redo();
  288. }
  289. }
  290. private void UndoButton_Click(object sender, RoutedEventArgs e)
  291. {
  292. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  293. {
  294. PDFViewControl.PDFView.UndoManager?.Undo();
  295. }
  296. }
  297. private void RedoButton_Click(object sender, RoutedEventArgs e)
  298. {
  299. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanRedo)
  300. {
  301. PDFViewControl.PDFView.UndoManager?.Redo();
  302. }
  303. }
  304. public void ViewSignatureEvent(object sender, CPDFSignature e)
  305. {
  306. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  307. VerifyDigitalSignatureControl dialog = new VerifyDigitalSignatureControl()
  308. {
  309. Owner = parentWindow
  310. };
  311. dialog.ViewCertificateEvent -= ViewCertificateEvent;
  312. dialog.ViewCertificateEvent += ViewCertificateEvent;
  313. dialog.InitWithSignature(e);
  314. dialog.ShowDialog();
  315. }
  316. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  317. {
  318. if (e != null && e.CommandType == CommandType.Context)
  319. {
  320. if (e.CommandTarget == TargetType.WidgetView)
  321. {
  322. e.Handle = true;
  323. e.PopupMenu = new ContextMenu();
  324. var sign = e.Sign.GetSignature();
  325. if (!e.Sign.IsSigned())
  326. {
  327. e.PopupMenu.Items.Add(new MenuItem()
  328. { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  329. e.PopupMenu.Items.Add(new MenuItem()
  330. { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  331. e.PopupMenu.Items.Add(new MenuItem()
  332. { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  333. }
  334. else
  335. {
  336. MenuItem DeleteMenu = new MenuItem()
  337. { Header = "Delete" };
  338. DeleteMenu.Click += (o, args) =>
  339. {
  340. if (e.Sign.IsSigned())
  341. {
  342. PDFViewControl.PDFView.Document.RemoveSignature(sign, true);
  343. SignatureStatusChanged?.Invoke(this, null);
  344. }
  345. e.Sign.RemoveAnnot();
  346. PDFViewControl.PDFView.ReloadDocument();
  347. };
  348. e.PopupMenu.Items.Add(DeleteMenu);
  349. }
  350. }
  351. else
  352. {
  353. e.Handle = true;
  354. e.PopupMenu = new ContextMenu();
  355. e.PopupMenu.Items.Add(new MenuItem()
  356. { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  357. e.PopupMenu.Items.Add(new Separator());
  358. MenuItem fitWidthMenu = new MenuItem();
  359. fitWidthMenu.Header = "Automatically Resize";
  360. fitWidthMenu.Click += (o, p) =>
  361. {
  362. if (PDFViewControl != null)
  363. {
  364. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  365. }
  366. };
  367. e.PopupMenu.Items.Add(fitWidthMenu);
  368. MenuItem fitSizeMenu = new MenuItem();
  369. fitSizeMenu.Header = "Actual Size";
  370. fitSizeMenu.Click += (o, p) =>
  371. {
  372. if (PDFViewControl != null)
  373. {
  374. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  375. }
  376. };
  377. e.PopupMenu.Items.Add(fitSizeMenu);
  378. MenuItem zoomInMenu = new MenuItem();
  379. zoomInMenu.Header = "Zoom In";
  380. zoomInMenu.Click += (o, p) =>
  381. {
  382. if (PDFViewControl != null)
  383. {
  384. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,
  385. PDFViewControl.PDFView.ZoomFactor + 0.01, true);
  386. PDFViewControl.PDFView?.Zoom(newZoom);
  387. }
  388. };
  389. e.PopupMenu.Items.Add(zoomInMenu);
  390. MenuItem zoomOutMenu = new MenuItem();
  391. zoomOutMenu.Header = "Zoom Out";
  392. zoomOutMenu.Click += (o, p) =>
  393. {
  394. if (PDFViewControl != null)
  395. {
  396. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,
  397. PDFViewControl.PDFView.ZoomFactor - 0.01, false);
  398. PDFViewControl.PDFView?.Zoom(newZoom);
  399. }
  400. };
  401. e.PopupMenu.Items.Add(zoomOutMenu);
  402. e.PopupMenu.Items.Add(new Separator());
  403. MenuItem singleView = new MenuItem();
  404. singleView.Header = "Single Page";
  405. singleView.Click += (o, p) =>
  406. {
  407. if (PDFViewControl != null)
  408. {
  409. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  410. }
  411. };
  412. e.PopupMenu.Items.Add(singleView);
  413. MenuItem singleContinuousView = new MenuItem();
  414. singleContinuousView.Header = "Single Page Continuous";
  415. singleContinuousView.Click += (o, p) =>
  416. {
  417. if (PDFViewControl != null)
  418. {
  419. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  420. }
  421. };
  422. e.PopupMenu.Items.Add(singleContinuousView);
  423. MenuItem doubleView = new MenuItem();
  424. doubleView.Header = "Two Pages";
  425. doubleView.Click += (o, p) =>
  426. {
  427. if (PDFViewControl != null)
  428. {
  429. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  430. }
  431. };
  432. e.PopupMenu.Items.Add(doubleView);
  433. MenuItem doubleContinuousView = new MenuItem();
  434. doubleContinuousView.Header = "Two Pages Continuous";
  435. doubleContinuousView.Click += (o, p) =>
  436. {
  437. if (PDFViewControl != null)
  438. {
  439. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  440. }
  441. };
  442. e.PopupMenu.Items.Add(doubleContinuousView);
  443. MenuItem resetFormMenu = new MenuItem();
  444. resetFormMenu.Header = "Reset Forms";
  445. resetFormMenu.Click += (o, p) =>
  446. {
  447. if (PDFViewControl != null)
  448. {
  449. PDFViewControl.PDFView?.ResetForm(null);
  450. }
  451. };
  452. e.PopupMenu.Items.Add(new Separator());
  453. e.PopupMenu.Items.Add(resetFormMenu);
  454. }
  455. }
  456. else
  457. {
  458. e.DoCommand();
  459. }
  460. }
  461. private void CopyImage_Click(object sender, RoutedEventArgs e)
  462. {
  463. CommandHelper.CopyImage_Click(PDFViewControl.PDFView.GetSelectedImages());
  464. }
  465. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  466. {
  467. CommandHelper.ExtraImage_Click(PDFViewControl.PDFView.GetSelectedImages());
  468. }
  469. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  470. {
  471. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  472. PDFViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  473. }
  474. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  475. {
  476. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  477. }
  478. }
  479. }