DigitalSignatureControl.xaml.cs 27 KB

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