DigitalSignatureControl.xaml.cs 20 KB

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