DigitalSignatureControl.xaml.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Controls.Primitives;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using Compdfkit_Tools.Helper;
  12. using ComPDFKit.DigitalSign;
  13. using ComPDFKit.PDFAnnotation;
  14. using ComPDFKit.PDFAnnotation.Form;
  15. using ComPDFKit.Tool;
  16. using ComPDFKit.Tool.Help;
  17. using ComPDFKitViewer;
  18. using ComPDFKitViewer.Widget;
  19. namespace Compdfkit_Tools.PDFControl
  20. {
  21. public partial class DigitalSignatureControl : UserControl, INotifyPropertyChanged
  22. {
  23. #region Properties
  24. private bool isFirstLoad = true;
  25. public PDFViewControl PDFViewControl = new PDFViewControl();
  26. private SignatureStatusBarControl signatureStatusBarControl;
  27. private PanelState panelState = PanelState.GetInstance();
  28. private CPDFDisplaySettingsControl displaySettingsControl = null;
  29. private CPDFSignatureWidget currentSignatureWidget;
  30. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  31. public event EventHandler<bool> OnCanSaveChanged;
  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. /// <summary>
  40. /// Whether the undo operation can be performed.
  41. /// </summary>
  42. public bool CanUndo
  43. {
  44. get
  45. {
  46. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  47. {
  48. return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo;
  49. }
  50. return false;
  51. }
  52. }
  53. /// <summary>
  54. /// Whether the redo operation can be performed.
  55. /// </summary>
  56. public bool CanRedo
  57. {
  58. get
  59. {
  60. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  61. {
  62. return PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo;
  63. }
  64. return false;
  65. }
  66. }
  67. /// <summary>
  68. /// Whether the save operation can be performed.
  69. /// </summary>
  70. public bool CanSave
  71. {
  72. get
  73. {
  74. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null)
  75. {
  76. if (PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  77. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  78. {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. }
  85. #endregion
  86. /// <summary>
  87. /// A digital signature control should be used in a window, and it should be initialized with a PDFViewer.
  88. /// Certificates will be saved in the TrustedFolder.
  89. /// </summary>
  90. public DigitalSignatureControl()
  91. {
  92. InitializeComponent();
  93. DataContext = this;
  94. string trustedFolder = AppDomain.CurrentDomain.BaseDirectory + @"\TrustedFolder\";
  95. if (!Directory.Exists(trustedFolder))
  96. {
  97. Directory.CreateDirectory(trustedFolder);
  98. }
  99. CPDFSignature.SignCertTrustedFolder = trustedFolder;
  100. }
  101. #region Public Method
  102. /// <summary>
  103. /// Disconnect all the specified elements that are already the logical children of another element. And reset bar control status.
  104. /// </summary>
  105. public void ClearViewerControl()
  106. {
  107. PDFGrid.Child = null;
  108. BotaContainer.Child = null;
  109. PropertyContainer.Child = null;
  110. displaySettingsControl = null;
  111. SignatureStatusBorder.Child = null;
  112. DigitalSignatureBarControl.ClearAllToolState();
  113. }
  114. /// <summary>
  115. /// Init controls with pdfViewer, and load events.
  116. /// </summary>
  117. /// <param name="pdfViewer"></param>
  118. public void InitWithPDFViewer(PDFViewControl pdfViewer)
  119. {
  120. PDFViewControl = pdfViewer;
  121. PDFGrid.Child = PDFViewControl;
  122. FloatPageTool.InitWithPDFViewer(pdfViewer);
  123. PDFViewControl.PDFToolManager.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
  124. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager.ClearHistory();
  125. DigitalSignatureBarControl.DigitalSignatureActionChanged -= DigitalSignatureBarControl_DigitalSignatureActionChanged;
  126. DigitalSignatureBarControl.DigitalSignatureActionChanged += DigitalSignatureBarControl_DigitalSignatureActionChanged;
  127. //PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  128. //PDFViewControl.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler;
  129. //PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  130. //PDFViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  131. PDFViewControl.PDFViewTool.MouseLeftButtonDownHandler -= PDFViewControl_MouseLeftButtonDownHandler;
  132. PDFViewControl.PDFViewTool.MouseLeftButtonDownHandler += PDFViewControl_MouseLeftButtonDownHandler;
  133. panelState.PropertyChanged -= PanelState_PropertyChanged;
  134. panelState.PropertyChanged += PanelState_PropertyChanged;
  135. }
  136. private void PDFViewControl_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  137. {
  138. ContextMenu ContextMenu = PDFViewControl.GetRightMenu();
  139. if (ContextMenu == null)
  140. {
  141. ContextMenu = new ContextMenu();
  142. }
  143. if (PDFViewControl.PDFViewTool.GetViewerModel() == MouseModes.WidgetEditTool && e.annotType== C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  144. {
  145. BaseWidget baseWidget = PDFViewControl.GetCacheHitTestWidget();
  146. if(baseWidget == null)
  147. {
  148. return;
  149. }
  150. var widget = baseWidget.GetAnnotData().Annot as CPDFWidget;
  151. if (widget == null || widget.WidgetType != C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  152. {
  153. return;
  154. }
  155. var signatureWidget = widget as CPDFSignatureWidget;
  156. CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.GetCPDFViewer().GetDocument());
  157. if (signatureWidget.IsSigned() && sig != null && sig?.SignerList.Count > 0)
  158. {
  159. MenuItem deleteMenu = new MenuItem()
  160. { Header = LanguageHelper.CommonManager.GetString("Menu_Delete") };
  161. deleteMenu.Click += (o, args) =>
  162. {
  163. PDFViewControl.GetCPDFViewer().GetDocument().RemoveSignature(sig, true);
  164. signatureWidget.ResetForm();
  165. signatureWidget.SetIsLocked(false);
  166. //PDFViewControl.PDFView.ReloadVisibleAnnots();
  167. //PDFViewControl.GetCPDFViewer().UpdateRenderFrame();
  168. PDFViewControl.PDFViewTool.IsDocumentModified = true;
  169. SignatureStatusChanged?.Invoke(this, null);
  170. };
  171. ContextMenu.Items.Add(deleteMenu);
  172. }
  173. else
  174. {
  175. ContextMenu.Items.Add(new MenuItem()
  176. { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  177. ContextMenu.Items.Add(new MenuItem()
  178. { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  179. ContextMenu.Items.Add(new MenuItem()
  180. { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  181. }
  182. }
  183. else if(e.hitTestType == MouseHitTestType.Text)
  184. {
  185. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  186. PDFViewControl.SetRightMenu(ContextMenu);
  187. }
  188. else
  189. {
  190. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  191. PDFViewControl.CreateViewerMenu(sender, ref ContextMenu);
  192. }
  193. PDFViewControl.SetRightMenu(ContextMenu);
  194. }
  195. private void PDFViewControl_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  196. {
  197. if (PDFViewControl.PDFViewTool.GetViewerModel() == MouseModes.Viewer && e.annotType== C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  198. {
  199. BaseWidget baseWidget = PDFViewControl.GetCacheHitTestWidget();
  200. if(baseWidget == null)
  201. {
  202. return;
  203. }
  204. var widget = baseWidget.GetAnnotData().Annot as CPDFWidget;
  205. if (widget == null)
  206. {
  207. return;
  208. }
  209. if (widget.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  210. {
  211. var signatureWidget = widget as CPDFSignatureWidget;
  212. CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.GetCPDFViewer().GetDocument());
  213. if (signatureWidget.IsSigned() && sig != null && sig?.SignerList.Count > 0)
  214. {
  215. ViewSignatureEvent(sender, sig);
  216. }
  217. else
  218. {
  219. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  220. AddCertificationDialog addCertificationControl = new AddCertificationDialog
  221. {
  222. Owner = parentWindow
  223. };
  224. currentSignatureWidget = signatureWidget;
  225. addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  226. addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  227. addCertificationControl.ShowDialog();
  228. }
  229. }
  230. }
  231. }
  232. /// <summary>
  233. /// Separately, init PDFView and load undo manager event. Only use for ensuring SaveBtn is enabled after deleting digital signature on Viewer mode.
  234. /// </summary>
  235. /// <param name="pdfViewer"></param>
  236. public void LoadUndoManagerEvent(CPDFViewer pdfViewer)
  237. {
  238. //PDFViewControl.PDFView = pdfViewer;
  239. //PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  240. //PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  241. }
  242. /// <summary>
  243. /// Set child for BOTAContainer with BOTABarControl.
  244. /// </summary>
  245. /// <param name="botaControl"></param>
  246. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  247. {
  248. BotaContainer.Child = botaControl;
  249. }
  250. /// <summary>
  251. /// Create a certificate info dialog with signature.
  252. /// </summary>
  253. /// <param name="sender"></param>
  254. /// <param name="e">Signature to init certificate</param>
  255. public void ViewCertificateEvent(object sender, CPDFSignature e)
  256. {
  257. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  258. ViewCertificateDialog dialog = new ViewCertificateDialog()
  259. {
  260. Owner = parentWindow
  261. };
  262. dialog.InitCertificateList(e);
  263. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  264. {
  265. SignatureStatusChanged?.Invoke(this, null);
  266. };
  267. if (parentWindow is VerifyDigitalSignatureControl verifyControl)
  268. {
  269. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  270. {
  271. verifyControl.InitWithSignature(e);
  272. };
  273. }
  274. dialog.ShowDialog();
  275. }
  276. /// <summary>
  277. /// Set display settings control.
  278. /// </summary>
  279. /// <param name="displaySettingsControl"></param>
  280. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  281. {
  282. this.displaySettingsControl = displaySettingsControl;
  283. }
  284. /// <summary>
  285. /// Set visibility of SignatureStatusBarControl according its status.
  286. /// </summary>
  287. /// <param name="signatureStatusBarControl"></param>
  288. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  289. {
  290. this.signatureStatusBarControl = signatureStatusBarControl;
  291. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  292. if (signatureStatusBarControl.Status != SignatureStatus.None)
  293. {
  294. SignatureStatusBorder.Visibility = Visibility.Visible;
  295. }
  296. else
  297. {
  298. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  299. }
  300. }
  301. /// <summary>
  302. /// Create a signature info dialog with signature.
  303. /// </summary>
  304. /// <param name="sender"></param>
  305. /// <param name="e">Signature to be displayed</param>
  306. public void ViewSignatureEvent(object sender, CPDFSignature e)
  307. {
  308. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  309. VerifyDigitalSignatureControl dialog = new VerifyDigitalSignatureControl()
  310. {
  311. Owner = parentWindow
  312. };
  313. dialog.ViewCertificateEvent -= ViewCertificateEvent;
  314. dialog.ViewCertificateEvent += ViewCertificateEvent;
  315. dialog.InitWithSignature(e);
  316. dialog.ShowDialog();
  317. }
  318. #endregion
  319. #region Private Method
  320. /// <summary>
  321. /// Get current time as a string.
  322. /// </summary>
  323. /// <returns></returns>
  324. private string GetTime()
  325. {
  326. DateTime dateTime = DateTime.Now;
  327. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  328. }
  329. /// <summary>
  330. /// Create a signature field.
  331. /// </summary>
  332. private void CreateSign()
  333. {
  334. PDFViewControl.PDFToolManager.SetToolType(CPDFToolManager.ToolType.WidgetEdit);
  335. PDFViewControl.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS);
  336. SignatureParam signatureParam = new SignatureParam
  337. {
  338. FieldName = "Signature" + GetTime(),
  339. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET,
  340. WidgetType = C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS,
  341. LineWidth = 1,
  342. FontName = "Helvetica",
  343. LineColor =new byte[] {0,0,0 },
  344. FontColor = new byte[] { 0, 0, 0 },
  345. Transparency = 255,
  346. HasLineColor = true,
  347. };
  348. PDFViewControl.SetAnnotParam(signatureParam);
  349. }
  350. /// <summary>
  351. /// Expand or collapse left panel.
  352. /// </summary>
  353. /// <param name="isExpand"></param>
  354. private void ExpandLeftPanel(bool isExpand)
  355. {
  356. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  357. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  358. if (isExpand)
  359. {
  360. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  361. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  362. }
  363. else
  364. {
  365. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  366. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  367. }
  368. }
  369. /// <summary>
  370. /// Expand or collapse right panel.
  371. /// </summary>
  372. /// <param name="propertytPanel"></param>
  373. /// <param name="visible"></param>
  374. private void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  375. {
  376. PropertyContainer.Width = 260;
  377. PropertyContainer.Child = propertytPanel;
  378. PropertyContainer.Visibility = visible;
  379. }
  380. #endregion
  381. #region Private Command Event
  382. /// <summary>
  383. /// Click event of signature field.
  384. /// </summary>
  385. /// <param name="sender"></param>
  386. /// <param name="e"></param>
  387. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  388. //{
  389. // var signatureWidget = (e as WidgetSignArgs).Sign;
  390. // CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.PDFView.Document);
  391. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  392. // {
  393. // ViewSignatureEvent(sender, sig);
  394. // }
  395. // else
  396. // {
  397. // Window parentWindow = Window.GetWindow((DependencyObject)sender);
  398. // AddCertificationDialog addCertificationControl = new AddCertificationDialog
  399. // {
  400. // Owner = parentWindow
  401. // };
  402. // currentSignatureWidget = signatureWidget;
  403. // addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  404. // addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  405. // addCertificationControl.ShowDialog();
  406. // }
  407. //}
  408. /// <summary>
  409. /// Event of filling a signature.
  410. /// </summary>
  411. /// <param name="sender"></param>
  412. /// <param name="e"></param>
  413. private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
  414. {
  415. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
  416. {
  417. FilePath = e.filePath,
  418. Password = e.password,
  419. SignatureWidget = currentSignatureWidget,
  420. Document = PDFViewControl.GetCPDFViewer().GetDocument(),
  421. Owner = Window.GetWindow(this)
  422. };
  423. fillDigitalSignatureDialog.AfterFillSignature -= FillDigitalSignatureDialog_AfterFillSignature; ;
  424. fillDigitalSignatureDialog.AfterFillSignature += FillDigitalSignatureDialog_AfterFillSignature; ;
  425. fillDigitalSignatureDialog.ShowDialog();
  426. }
  427. /// <summary>
  428. /// Event of after filling a signature.
  429. /// </summary>
  430. /// <param name="sender"></param>
  431. /// <param name="e"></param>
  432. private void FillDigitalSignatureDialog_AfterFillSignature(object sender, string e)
  433. {
  434. AfterFillSignature?.Invoke(this, e);
  435. }
  436. /// <summary>
  437. /// Click event of buttons in digital SignatureBarControl.
  438. /// </summary>
  439. /// <param name="sender"></param>
  440. /// <param name="e"></param>
  441. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  442. {
  443. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatureField)
  444. {
  445. CreateSign();
  446. }
  447. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  448. {
  449. PDFViewControl.PDFToolManager.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
  450. }
  451. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  452. {
  453. ToggleButton button = sender as ToggleButton;
  454. button.IsChecked = false;
  455. SignatureStatusChanged?.Invoke(this, null);
  456. }
  457. }
  458. /// <summary>
  459. /// Property changed event of PanelState.
  460. /// </summary>
  461. /// <param name="sender"></param>
  462. /// <param name="e"></param>
  463. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  464. {
  465. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  466. {
  467. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  468. }
  469. else if (e.PropertyName == nameof(PanelState.RightPanel))
  470. {
  471. if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  472. {
  473. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  474. }
  475. else
  476. {
  477. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  478. }
  479. }
  480. }
  481. /// <summary>
  482. /// Event of UndoManager property changed.
  483. /// </summary>
  484. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  485. {
  486. OnPropertyChanged(e.PropertyName);
  487. if (e.PropertyName == "CanSave")
  488. {
  489. OnCanSaveChanged?.Invoke(this, CanSave);
  490. }
  491. }
  492. /// <summary>
  493. /// Command event of undo operation.
  494. /// </summary>
  495. /// <param name="sender"></param>
  496. /// <param name="e"></param>
  497. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  498. {
  499. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  500. {
  501. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  502. }
  503. }
  504. /// <summary>
  505. /// Command event of redo operation.
  506. /// </summary>
  507. /// <param name="sender"></param>
  508. /// <param name="e"></param>
  509. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  510. {
  511. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  512. {
  513. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  514. }
  515. }
  516. /// <summary>
  517. /// Click event of undo button.
  518. /// </summary>
  519. /// <param name="sender"></param>
  520. /// <param name="e"></param>
  521. private void UndoButton_Click(object sender, RoutedEventArgs e)
  522. {
  523. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  524. {
  525. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  526. }
  527. }
  528. /// <summary>
  529. /// Click event of redo button.
  530. /// </summary>
  531. /// <param name="sender"></param>
  532. /// <param name="e"></param>
  533. private void RedoButton_Click(object sender, RoutedEventArgs e)
  534. {
  535. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanRedo)
  536. {
  537. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  538. }
  539. }
  540. #endregion
  541. #region ContextMenu
  542. /// <summary>
  543. /// Right click context menu
  544. /// </summary>
  545. /// <param name="sender"></param>
  546. /// <param name="e"></param>
  547. private void PDFView_AnnotCommandHandler(object sender, MouseEventObject e)
  548. {
  549. ContextMenu ContextMenu = PDFViewControl.GetRightMenu();
  550. if (ContextMenu == null)
  551. {
  552. ContextMenu = new ContextMenu();
  553. }
  554. switch (e.hitTestType)
  555. {
  556. case MouseHitTestType.Annot:
  557. case MouseHitTestType.SelectRect:
  558. break;
  559. case MouseHitTestType.Text:
  560. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  561. break;
  562. default:
  563. PDFViewControl.CreateViewerMenu(sender, ref ContextMenu);
  564. break;
  565. }
  566. PDFViewControl.SetRightMenu(ContextMenu);
  567. }
  568. #endregion
  569. #region Load Unload Event
  570. /// <summary>
  571. /// Load event
  572. /// </summary>
  573. /// <param name="sender"></param>
  574. /// <param name="e"></param>
  575. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  576. {
  577. PDFViewControl.PDFViewTool.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
  578. PDFViewControl.PDFViewTool.MouseRightButtonDownHandler += PDFViewControl_MouseRightButtonDownHandler;
  579. }
  580. /// <summary>
  581. /// Unload event
  582. /// </summary>
  583. /// <param name="sender"></param>
  584. /// <param name="e"></param>
  585. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  586. {
  587. //PDFViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  588. //PDFViewControl.PDFView.WidgetClickHandler -= PDFView_WidgetClickHandler;
  589. PDFViewControl.PDFViewTool.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
  590. }
  591. #endregion
  592. }
  593. }