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. PDFViewControl.PDFViewTool.MouseRightButtonDownHandler -= PDFViewControl_MouseRightButtonDownHandler;
  134. PDFViewControl.PDFViewTool.MouseRightButtonDownHandler += PDFViewControl_MouseRightButtonDownHandler;
  135. panelState.PropertyChanged -= PanelState_PropertyChanged;
  136. panelState.PropertyChanged += PanelState_PropertyChanged;
  137. }
  138. private void PDFViewControl_MouseRightButtonDownHandler(object sender, MouseEventObject e)
  139. {
  140. ContextMenu ContextMenu = PDFViewControl.GetRightMenu();
  141. if (ContextMenu == null)
  142. {
  143. ContextMenu = new ContextMenu();
  144. }
  145. if (PDFViewControl.PDFViewTool.GetViewerModel() == MouseModes.WidgetEditTool && e.annotType== C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  146. {
  147. BaseWidget baseWidget = PDFViewControl.GetCacheHitTestWidget();
  148. if(baseWidget == null)
  149. {
  150. return;
  151. }
  152. var widget = baseWidget.GetAnnotData().Annot as CPDFWidget;
  153. if (widget == null || widget.WidgetType != C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  154. {
  155. return;
  156. }
  157. var signatureWidget = widget as CPDFSignatureWidget;
  158. CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.GetCPDFViewer().GetDocument());
  159. if (signatureWidget.IsSigned() && sig != null && sig?.SignerList.Count > 0)
  160. {
  161. MenuItem deleteMenu = new MenuItem()
  162. { Header = LanguageHelper.CommonManager.GetString("Menu_Delete") };
  163. deleteMenu.Click += (o, args) =>
  164. {
  165. PDFViewControl.GetCPDFViewer().GetDocument().RemoveSignature(sig, true);
  166. signatureWidget.ResetForm();
  167. signatureWidget.SetIsLocked(false);
  168. //PDFViewControl.PDFView.ReloadVisibleAnnots();
  169. //PDFViewControl.GetCPDFViewer().UpdateRenderFrame();
  170. PDFViewControl.PDFViewTool.IsDocumentModified = true;
  171. SignatureStatusChanged?.Invoke(this, null);
  172. };
  173. ContextMenu.Items.Add(deleteMenu);
  174. }
  175. else
  176. {
  177. ContextMenu.Items.Add(new MenuItem()
  178. { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  179. ContextMenu.Items.Add(new MenuItem()
  180. { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  181. ContextMenu.Items.Add(new MenuItem()
  182. { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  183. }
  184. }
  185. else if(e.hitTestType == MouseHitTestType.Text)
  186. {
  187. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  188. PDFViewControl.SetRightMenu(ContextMenu);
  189. }
  190. else
  191. {
  192. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  193. PDFViewControl.CreateViewerMenu(sender, ref ContextMenu);
  194. }
  195. PDFViewControl.SetRightMenu(ContextMenu);
  196. }
  197. private void PDFViewControl_MouseLeftButtonDownHandler(object sender, MouseEventObject e)
  198. {
  199. if (PDFViewControl.PDFViewTool.GetViewerModel() == MouseModes.Viewer && e.annotType== C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  200. {
  201. BaseWidget baseWidget = PDFViewControl.GetCacheHitTestWidget();
  202. if(baseWidget == null)
  203. {
  204. return;
  205. }
  206. var widget = baseWidget.GetAnnotData().Annot as CPDFWidget;
  207. if (widget == null)
  208. {
  209. return;
  210. }
  211. if (widget.WidgetType == C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS)
  212. {
  213. var signatureWidget = widget as CPDFSignatureWidget;
  214. CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.GetCPDFViewer().GetDocument());
  215. if (signatureWidget.IsSigned() && sig != null && sig?.SignerList.Count > 0)
  216. {
  217. ViewSignatureEvent(sender, sig);
  218. }
  219. else
  220. {
  221. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  222. AddCertificationDialog addCertificationControl = new AddCertificationDialog
  223. {
  224. Owner = parentWindow
  225. };
  226. currentSignatureWidget = signatureWidget;
  227. addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  228. addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  229. addCertificationControl.ShowDialog();
  230. }
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// Separately, init PDFView and load undo manager event. Only use for ensuring SaveBtn is enabled after deleting digital signature on Viewer mode.
  236. /// </summary>
  237. /// <param name="pdfViewer"></param>
  238. public void LoadUndoManagerEvent(CPDFViewer pdfViewer)
  239. {
  240. //PDFViewControl.PDFView = pdfViewer;
  241. //PDFViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  242. //PDFViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  243. }
  244. /// <summary>
  245. /// Set child for BOTAContainer with BOTABarControl.
  246. /// </summary>
  247. /// <param name="botaControl"></param>
  248. public void SetBOTAContainer(CPDFBOTABarControl botaControl)
  249. {
  250. BotaContainer.Child = botaControl;
  251. }
  252. /// <summary>
  253. /// Create a certificate info dialog with signature.
  254. /// </summary>
  255. /// <param name="sender"></param>
  256. /// <param name="e">Signature to init certificate</param>
  257. public void ViewCertificateEvent(object sender, CPDFSignature e)
  258. {
  259. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  260. ViewCertificateDialog dialog = new ViewCertificateDialog()
  261. {
  262. Owner = parentWindow
  263. };
  264. dialog.InitCertificateList(e);
  265. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  266. {
  267. SignatureStatusChanged?.Invoke(this, null);
  268. };
  269. if (parentWindow is VerifyDigitalSignatureControl verifyControl)
  270. {
  271. dialog.CertificateInfoControl.TrustCertificateEvent += (o, args) =>
  272. {
  273. verifyControl.InitWithSignature(e);
  274. };
  275. }
  276. dialog.ShowDialog();
  277. }
  278. /// <summary>
  279. /// Set display settings control.
  280. /// </summary>
  281. /// <param name="displaySettingsControl"></param>
  282. public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl)
  283. {
  284. this.displaySettingsControl = displaySettingsControl;
  285. }
  286. /// <summary>
  287. /// Set visibility of SignatureStatusBarControl according its status.
  288. /// </summary>
  289. /// <param name="signatureStatusBarControl"></param>
  290. public void SetSignatureStatusBarControl(SignatureStatusBarControl signatureStatusBarControl)
  291. {
  292. this.signatureStatusBarControl = signatureStatusBarControl;
  293. SignatureStatusBorder.Child = this.signatureStatusBarControl;
  294. if (signatureStatusBarControl.Status != SignatureStatus.None)
  295. {
  296. SignatureStatusBorder.Visibility = Visibility.Visible;
  297. }
  298. else
  299. {
  300. SignatureStatusBorder.Visibility = Visibility.Collapsed;
  301. }
  302. }
  303. /// <summary>
  304. /// Create a signature info dialog with signature.
  305. /// </summary>
  306. /// <param name="sender"></param>
  307. /// <param name="e">Signature to be displayed</param>
  308. public void ViewSignatureEvent(object sender, CPDFSignature e)
  309. {
  310. Window parentWindow = Window.GetWindow((DependencyObject)sender);
  311. VerifyDigitalSignatureControl dialog = new VerifyDigitalSignatureControl()
  312. {
  313. Owner = parentWindow
  314. };
  315. dialog.ViewCertificateEvent -= ViewCertificateEvent;
  316. dialog.ViewCertificateEvent += ViewCertificateEvent;
  317. dialog.InitWithSignature(e);
  318. dialog.ShowDialog();
  319. }
  320. #endregion
  321. #region Private Method
  322. /// <summary>
  323. /// Get current time as a string.
  324. /// </summary>
  325. /// <returns></returns>
  326. private string GetTime()
  327. {
  328. DateTime dateTime = DateTime.Now;
  329. return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
  330. }
  331. /// <summary>
  332. /// Create a signature field.
  333. /// </summary>
  334. private void CreateSign()
  335. {
  336. PDFViewControl.PDFToolManager.SetToolType(CPDFToolManager.ToolType.WidgetEdit);
  337. PDFViewControl.SetCreateWidgetType(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS);
  338. SignatureParam signatureParam = new SignatureParam
  339. {
  340. FieldName = "Signature" + GetTime(),
  341. CurrentType = C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET,
  342. WidgetType = C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS,
  343. LineWidth = 1,
  344. FontName = "Helvetica",
  345. LineColor =new byte[] {0,0,0 },
  346. FontColor = new byte[] { 0, 0, 0 },
  347. Transparency = 255,
  348. HasLineColor = true,
  349. };
  350. PDFViewControl.SetAnnotParam(signatureParam);
  351. }
  352. /// <summary>
  353. /// Expand or collapse left panel.
  354. /// </summary>
  355. /// <param name="isExpand"></param>
  356. private void ExpandLeftPanel(bool isExpand)
  357. {
  358. BotaContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  359. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  360. if (isExpand)
  361. {
  362. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  363. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  364. }
  365. else
  366. {
  367. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  368. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  369. }
  370. }
  371. /// <summary>
  372. /// Expand or collapse right panel.
  373. /// </summary>
  374. /// <param name="propertytPanel"></param>
  375. /// <param name="visible"></param>
  376. private void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
  377. {
  378. PropertyContainer.Width = 260;
  379. PropertyContainer.Child = propertytPanel;
  380. PropertyContainer.Visibility = visible;
  381. }
  382. #endregion
  383. #region Private Command Event
  384. /// <summary>
  385. /// Click event of signature field.
  386. /// </summary>
  387. /// <param name="sender"></param>
  388. /// <param name="e"></param>
  389. //private void PDFView_WidgetClickHandler(object sender, WidgetArgs e)
  390. //{
  391. // var signatureWidget = (e as WidgetSignArgs).Sign;
  392. // CPDFSignature sig = signatureWidget.GetSignature(PDFViewControl.PDFView.Document);
  393. // if (signatureWidget.IsSigned() && sig!=null && sig?.SignerList.Count > 0)
  394. // {
  395. // ViewSignatureEvent(sender, sig);
  396. // }
  397. // else
  398. // {
  399. // Window parentWindow = Window.GetWindow((DependencyObject)sender);
  400. // AddCertificationDialog addCertificationControl = new AddCertificationDialog
  401. // {
  402. // Owner = parentWindow
  403. // };
  404. // currentSignatureWidget = signatureWidget;
  405. // addCertificationControl.FillSignatureEvent -= AddCertificationControl_FillSignatureEvent;
  406. // addCertificationControl.FillSignatureEvent += AddCertificationControl_FillSignatureEvent;
  407. // addCertificationControl.ShowDialog();
  408. // }
  409. //}
  410. /// <summary>
  411. /// Event of filling a signature.
  412. /// </summary>
  413. /// <param name="sender"></param>
  414. /// <param name="e"></param>
  415. private void AddCertificationControl_FillSignatureEvent(object sender, CertificateAccess e)
  416. {
  417. FillDigitalSignatureDialog fillDigitalSignatureDialog = new FillDigitalSignatureDialog
  418. {
  419. FilePath = e.filePath,
  420. Password = e.password,
  421. SignatureWidget = currentSignatureWidget,
  422. Document = PDFViewControl.GetCPDFViewer().GetDocument(),
  423. Owner = Window.GetWindow(this)
  424. };
  425. fillDigitalSignatureDialog.AfterFillSignature -= FillDigitalSignatureDialog_AfterFillSignature; ;
  426. fillDigitalSignatureDialog.AfterFillSignature += FillDigitalSignatureDialog_AfterFillSignature; ;
  427. fillDigitalSignatureDialog.ShowDialog();
  428. }
  429. /// <summary>
  430. /// Event of after filling a signature.
  431. /// </summary>
  432. /// <param name="sender"></param>
  433. /// <param name="e"></param>
  434. private void FillDigitalSignatureDialog_AfterFillSignature(object sender, string e)
  435. {
  436. AfterFillSignature?.Invoke(this, e);
  437. }
  438. /// <summary>
  439. /// Click event of buttons in digital SignatureBarControl.
  440. /// </summary>
  441. /// <param name="sender"></param>
  442. /// <param name="e"></param>
  443. private void DigitalSignatureBarControl_DigitalSignatureActionChanged(object sender, Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction e)
  444. {
  445. if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.AddSignatureField)
  446. {
  447. CreateSign();
  448. }
  449. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.Signing)
  450. {
  451. PDFViewControl.PDFToolManager.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
  452. }
  453. else if (e == Common.CPDFDigitalSignatureBarControl.DigitalSignatureAction.VerifySignature)
  454. {
  455. ToggleButton button = sender as ToggleButton;
  456. button.IsChecked = false;
  457. SignatureStatusChanged?.Invoke(this, null);
  458. }
  459. }
  460. /// <summary>
  461. /// Property changed event of PanelState.
  462. /// </summary>
  463. /// <param name="sender"></param>
  464. /// <param name="e"></param>
  465. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  466. {
  467. if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand))
  468. {
  469. ExpandLeftPanel(panelState.IsLeftPanelExpand);
  470. }
  471. else if (e.PropertyName == nameof(PanelState.RightPanel))
  472. {
  473. if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings)
  474. {
  475. ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible);
  476. }
  477. else
  478. {
  479. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  480. }
  481. }
  482. }
  483. /// <summary>
  484. /// Event of UndoManager property changed.
  485. /// </summary>
  486. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  487. {
  488. OnPropertyChanged(e.PropertyName);
  489. if (e.PropertyName == "CanSave")
  490. {
  491. OnCanSaveChanged?.Invoke(this, CanSave);
  492. }
  493. }
  494. /// <summary>
  495. /// Command event of undo operation.
  496. /// </summary>
  497. /// <param name="sender"></param>
  498. /// <param name="e"></param>
  499. private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
  500. {
  501. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  502. {
  503. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  504. }
  505. }
  506. /// <summary>
  507. /// Command event of redo operation.
  508. /// </summary>
  509. /// <param name="sender"></param>
  510. /// <param name="e"></param>
  511. private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
  512. {
  513. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  514. {
  515. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  516. }
  517. }
  518. /// <summary>
  519. /// Click event of undo button.
  520. /// </summary>
  521. /// <param name="sender"></param>
  522. /// <param name="e"></param>
  523. private void UndoButton_Click(object sender, RoutedEventArgs e)
  524. {
  525. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanUndo)
  526. {
  527. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Undo();
  528. }
  529. }
  530. /// <summary>
  531. /// Click event of redo button.
  532. /// </summary>
  533. /// <param name="sender"></param>
  534. /// <param name="e"></param>
  535. private void RedoButton_Click(object sender, RoutedEventArgs e)
  536. {
  537. if (PDFViewControl != null && PDFViewControl.PDFViewTool.GetCPDFViewer() != null && CanRedo)
  538. {
  539. PDFViewControl.PDFViewTool.GetCPDFViewer().UndoManager?.Redo();
  540. }
  541. }
  542. #endregion
  543. #region ContextMenu
  544. /// <summary>
  545. /// Right click context menu
  546. /// </summary>
  547. /// <param name="sender"></param>
  548. /// <param name="e"></param>
  549. private void PDFView_AnnotCommandHandler(object sender, MouseEventObject e)
  550. {
  551. ContextMenu ContextMenu = PDFViewControl.GetRightMenu();
  552. if (ContextMenu == null)
  553. {
  554. ContextMenu = new ContextMenu();
  555. }
  556. switch (e.hitTestType)
  557. {
  558. case MouseHitTestType.Annot:
  559. case MouseHitTestType.SelectRect:
  560. break;
  561. case MouseHitTestType.Text:
  562. ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  563. break;
  564. default:
  565. PDFViewControl.CreateViewerMenu(sender, ref ContextMenu);
  566. break;
  567. }
  568. PDFViewControl.SetRightMenu(ContextMenu);
  569. }
  570. #endregion
  571. #region Load Unload Event
  572. /// <summary>
  573. /// Load event
  574. /// </summary>
  575. /// <param name="sender"></param>
  576. /// <param name="e"></param>
  577. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  578. {
  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. }
  590. #endregion
  591. }
  592. }