DigitalSignatureControl.xaml.cs 21 KB

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