DigitalSignatureControl.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. Owner = Window.GetWindow(this)
  135. };
  136. fillDigitalSignatureDialog.AfterFillSignature += FillDigitalSignatureDialog_AfterFillSignature; ;
  137. fillDigitalSignatureDialog.ShowDialog();
  138. }
  139. private void FillDigitalSignatureDialog_AfterFillSignature(object sender, string e)
  140. {
  141. AfterFillSignature?.Invoke(this, e);
  142. }
  143. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  144. {
  145. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatureField)
  146. {
  147. CreateSign();
  148. }
  149. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  150. {
  151. PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  152. }
  153. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  154. {
  155. ToggleButton button = sender as ToggleButton;
  156. button.IsChecked = false;
  157. SignatureStatusChanged?.Invoke(this, null);
  158. }
  159. }
  160. private string GetTime()
  161. {
  162. DateTime dateTime = DateTime.Now;
  163. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  164. }
  165. private void CreateSign()
  166. {
  167. PDFViewControl.PDFView.SetMouseMode(MouseModes.FormEditTool);
  168. WidgetSignArgs signArgs = new WidgetSignArgs
  169. {
  170. LineWidth = 1,
  171. LineColor = Colors.Black,
  172. FieldName = "Signature" + GetTime()
  173. };
  174. PDFViewControl.PDFView.SetToolParam(signArgs);
  175. }
  176. private void InitialControl()
  177. {
  178. PDFViewControl.PDFView?.SetMouseMode(MouseModes.Viewer);
  179. //PDFViewControl.PDFView?.Load();
  180. PDFViewControl.PDFView?.SetShowLink(true);
  181. PDFGrid.Child = PDFViewControl;
  182. PDFViewControl.PDFView.SetFormFieldHighlight(true);
  183. }
  184. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  185. {
  186. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  187. {
  188. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  189. }
  190. else if (e.PropertyName == nameof(PanelState.RightPanel))
  191. {
  192. if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  193. {
  194. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  195. }
  196. else
  197. {
  198. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  199. }
  200. }
  201. }
  202. public void ExpandLeftPanel(bool isExpand)
  203. {
  204. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  205. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  206. if (isExpand)
  207. {
  208. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  209. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  210. }
  211. else
  212. {
  213. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  214. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  215. }
  216. }
  217. public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  218. {
  219. PropertyContainer.Width = 260;
  220. PropertyContainer.Child = propertytPanel;
  221. PropertyContainer.Visibility = visible;
  222. }
  223. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  224. {
  225. BotaContainer.Child = botaControl;
  226. }
  227. public void ViewCertificateEvent(object sender, CPDFSignature e)
  228. {
  229. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  230. ViewCertificateDialog dialog = new ViewCertificateDialog()
  231. {
  232. Owner = parentWindow
  233. };
  234. dialog.InitCertificateList(e);
  235. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  236. {
  237. SignatureStatusChanged?.Invoke(this, null);
  238. };
  239. VerifyDigitalSignatureControl verifyControl = parentWindow as VerifyDigitalSignatureControl;
  240. if (verifyControl != null)
  241. {
  242. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  243. {
  244. verifyControl.InitWithSignature(e);
  245. };
  246. }
  247. dialog.ShowDialog();
  248. }
  249. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  250. {
  251. this.displaySettingsControl = displaySettingsControl;
  252. }
  253. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  254. {
  255. this.signatureStatusBarControl = signatureStatusBarControl;
  256. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  257. if (signatureStatusBarControl.Status != SignatureStatus.None)
  258. {
  259. SignatureStatusBorder.Visibility = Visibility.Visible;
  260. }
  261. else
  262. {
  263. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  264. }
  265. }
  266. /// <summary>
  267. /// Undo Redo Event Noitfy
  268. /// </summary>
  269. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  270. {
  271. OnPropertyChanged(e.PropertyName);
  272. if (e.PropertyName == "CanSave")
  273. {
  274. OnCanSaveChanged?.Invoke(this, CanSave);
  275. }
  276. }
  277. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  278. {
  279. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  280. {
  281. PDFViewControl.PDFView.UndoManager?.Undo();
  282. }
  283. }
  284. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  285. {
  286. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  287. {
  288. PDFViewControl.PDFView.UndoManager?.Redo();
  289. }
  290. }
  291. private void UndoButton_Click(object sender, RoutedEventArgs e)
  292. {
  293. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanUndo)
  294. {
  295. PDFViewControl.PDFView.UndoManager?.Undo();
  296. }
  297. }
  298. private void RedoButton_Click(object sender, RoutedEventArgs e)
  299. {
  300. if (PDFViewControl != null && PDFViewControl.PDFView != null && CanRedo)
  301. {
  302. PDFViewControl.PDFView.UndoManager?.Redo();
  303. }
  304. }
  305. public void ViewSignatureEvent(object sender, CPDFSignature e)
  306. {
  307. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  308. VerifyDigitalSignatureControl dialog = new VerifyDigitalSignatureControl()
  309. {
  310. Owner = parentWindow
  311. };
  312. dialog.ViewCertificateEvent -= ViewCertificateEvent;
  313. dialog.ViewCertificateEvent += ViewCertificateEvent;
  314. dialog.InitWithSignature(e);
  315. dialog.ShowDialog();
  316. }
  317. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  318. {
  319. if (e != null && e.CommandType == CommandType.Context)
  320. {
  321. if (e.CommandTarget == TargetType.WidgetView)
  322. {
  323. e.Handle = true;
  324. e.PopupMenu = new ContextMenu();
  325. var sign = e.Sign.GetSignature();
  326. if (!e.Sign.IsSigned())
  327. {
  328. e.PopupMenu.Items.Add(new MenuItem()
  329. { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  330. e.PopupMenu.Items.Add(new MenuItem()
  331. { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  332. e.PopupMenu.Items.Add(new MenuItem()
  333. { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  334. }
  335. else
  336. {
  337. MenuItem DeleteMenu = new MenuItem()
  338. { Header = "Delete" };
  339. DeleteMenu.Click += (o, args) =>
  340. {
  341. var widget = sign.GetSignatureWidget(PDFViewControl.PDFView.Document);
  342. int index = e.Sign.Page.PageIndex;
  343. PDFViewControl.PDFView.Document.RemoveSignature(sign, true);
  344. SignatureStatusChanged?.Invoke(this, null);
  345. bool res = widget.ResetForm();
  346. };
  347. e.PopupMenu.Items.Add(DeleteMenu);
  348. }
  349. }
  350. else
  351. {
  352. e.Handle = true;
  353. e.PopupMenu = new ContextMenu();
  354. e.PopupMenu.Items.Add(new MenuItem()
  355. { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  356. e.PopupMenu.Items.Add(new Separator());
  357. MenuItem fitWidthMenu = new MenuItem();
  358. fitWidthMenu.Header = "Automatically Resize";
  359. fitWidthMenu.Click += (o, p) =>
  360. {
  361. if (PDFViewControl != null)
  362. {
  363. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  364. }
  365. };
  366. e.PopupMenu.Items.Add(fitWidthMenu);
  367. MenuItem fitSizeMenu = new MenuItem();
  368. fitSizeMenu.Header = "Actual Size";
  369. fitSizeMenu.Click += (o, p) =>
  370. {
  371. if (PDFViewControl != null)
  372. {
  373. PDFViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  374. }
  375. };
  376. e.PopupMenu.Items.Add(fitSizeMenu);
  377. MenuItem zoomInMenu = new MenuItem();
  378. zoomInMenu.Header = "Zoom In";
  379. zoomInMenu.Click += (o, p) =>
  380. {
  381. if (PDFViewControl != null)
  382. {
  383. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,
  384. PDFViewControl.PDFView.ZoomFactor + 0.01, true);
  385. PDFViewControl.PDFView?.Zoom(newZoom);
  386. }
  387. };
  388. e.PopupMenu.Items.Add(zoomInMenu);
  389. MenuItem zoomOutMenu = new MenuItem();
  390. zoomOutMenu.Header = "Zoom Out";
  391. zoomOutMenu.Click += (o, p) =>
  392. {
  393. if (PDFViewControl != null)
  394. {
  395. double newZoom = CommandHelper.CheckZoomLevel(zoomLevelList,
  396. PDFViewControl.PDFView.ZoomFactor - 0.01, false);
  397. PDFViewControl.PDFView?.Zoom(newZoom);
  398. }
  399. };
  400. e.PopupMenu.Items.Add(zoomOutMenu);
  401. e.PopupMenu.Items.Add(new Separator());
  402. MenuItem singleView = new MenuItem();
  403. singleView.Header = "Single Page";
  404. singleView.Click += (o, p) =>
  405. {
  406. if (PDFViewControl != null)
  407. {
  408. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  409. }
  410. };
  411. e.PopupMenu.Items.Add(singleView);
  412. MenuItem singleContinuousView = new MenuItem();
  413. singleContinuousView.Header = "Single Page Continuous";
  414. singleContinuousView.Click += (o, p) =>
  415. {
  416. if (PDFViewControl != null)
  417. {
  418. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  419. }
  420. };
  421. e.PopupMenu.Items.Add(singleContinuousView);
  422. MenuItem doubleView = new MenuItem();
  423. doubleView.Header = "Two Pages";
  424. doubleView.Click += (o, p) =>
  425. {
  426. if (PDFViewControl != null)
  427. {
  428. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  429. }
  430. };
  431. e.PopupMenu.Items.Add(doubleView);
  432. MenuItem doubleContinuousView = new MenuItem();
  433. doubleContinuousView.Header = "Two Pages Continuous";
  434. doubleContinuousView.Click += (o, p) =>
  435. {
  436. if (PDFViewControl != null)
  437. {
  438. PDFViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  439. }
  440. };
  441. e.PopupMenu.Items.Add(doubleContinuousView);
  442. MenuItem resetFormMenu = new MenuItem();
  443. resetFormMenu.Header = "Reset Forms";
  444. resetFormMenu.Click += (o, p) =>
  445. {
  446. if (PDFViewControl != null)
  447. {
  448. PDFViewControl.PDFView?.ResetForm(null);
  449. }
  450. };
  451. e.PopupMenu.Items.Add(new Separator());
  452. e.PopupMenu.Items.Add(resetFormMenu);
  453. }
  454. }
  455. else
  456. {
  457. e.DoCommand();
  458. }
  459. }
  460. private void CopyImage_Click(object sender, RoutedEventArgs e)
  461. {
  462. CommandHelper.CopyImage_Click(PDFViewControl.PDFView.GetSelectedImages());
  463. }
  464. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  465. {
  466. CommandHelper.ExtraImage_Click(PDFViewControl.PDFView.GetSelectedImages());
  467. }
  468. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  469. {
  470. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  471. PDFViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  472. }
  473. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  474. {
  475. PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  476. }
  477. }
  478. }