MainPage.xaml.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  8. using Compdfkit_Tools.Helper;
  9. using Compdfkit_Tools.PDFControl;
  10. using Compdfkit_Tools.PDFView;
  11. using ComPDFKit.PDFDocument;
  12. using ComPDFKit.PDFPage;
  13. using ComPDFKitViewer;
  14. using ComPDFKitViewer.PdfViewer;
  15. using Microsoft.Win32;
  16. using System.Windows.Input;
  17. using ComPDFKitViewer.AnnotEvent;
  18. namespace PDFViewer
  19. {
  20. public partial class MainPage : UserControl, INotifyPropertyChanged
  21. {
  22. #region Properties
  23. private string currentMode = "Viewer";
  24. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  25. public PDFViewControl pdfViewer;
  26. private PDFViewControl passwordViewer;
  27. private RegularViewerControl regularViewerControl = new RegularViewerControl();
  28. private AnnotationControl annotationControl = new AnnotationControl();
  29. private FormControl formControl = new FormControl();
  30. private ContentEditControl contentEditControl = new ContentEditControl();
  31. private PageEditControl pageEditControl = new PageEditControl();
  32. private DigitalSignatureControl digitalSignatureControl = new DigitalSignatureControl();
  33. private SignatureStatusBarControl signatureStatusBarControl = new SignatureStatusBarControl();
  34. private CPDFBOTABarControl botaBarControl = new CPDFBOTABarControl();
  35. private CPDFDisplaySettingsControl displaySettingsControl = new CPDFDisplaySettingsControl();
  36. private PanelState panelState = PanelState.GetInstance();
  37. public event EventHandler FileChangeEvent;
  38. public event Func<string[], bool> CheckExistBeforeOpenFileEvent;
  39. public event EventHandler<string> AfterSaveAsFileEvent;
  40. public event PropertyChangedEventHandler PropertyChanged;
  41. private bool _canSave = false;
  42. /// <summary>
  43. /// Whether the save operation can be performed.
  44. /// </summary>
  45. public bool CanSave
  46. {
  47. get => _canSave;
  48. set
  49. {
  50. _canSave = value;
  51. OnPropertyChanged();
  52. }
  53. }
  54. /// <summary>
  55. /// Whether the left panel is expanded.
  56. /// </summary>
  57. public bool LeftToolPanelButtonIsChecked
  58. {
  59. get => panelState.IsLeftPanelExpand;
  60. set
  61. {
  62. panelState.IsLeftPanelExpand = value;
  63. OnPropertyChanged();
  64. }
  65. }
  66. /// <summary>
  67. /// Whether the right panel is expanded.
  68. /// </summary>
  69. public bool RightToolPanelButtonIsChecked
  70. {
  71. get => panelState.RightPanel == PanelState.RightPanelState.PropertyPanel;
  72. set
  73. {
  74. panelState.RightPanel = (value) ? PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  75. OnPropertyChanged();
  76. }
  77. }
  78. /// <summary>
  79. /// Whether the view setting panel is expanded.
  80. /// </summary>
  81. public bool ViewSettingBtnIsChecked
  82. {
  83. get
  84. {
  85. return (panelState.RightPanel == PanelState.RightPanelState.ViewSettings);
  86. }
  87. set
  88. {
  89. panelState.RightPanel = (value) ? PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  90. OnPropertyChanged();
  91. }
  92. }
  93. private Visibility _notDocsEditorVisible = Visibility.Visible;
  94. /// <summary>
  95. /// Whether the Document Editor panel is visible.
  96. /// </summary>
  97. public Visibility NotDocsEditorVisible
  98. {
  99. get => _notDocsEditorVisible;
  100. set
  101. {
  102. _notDocsEditorVisible = value;
  103. OnPropertyChanged();
  104. }
  105. }
  106. public MainPage()
  107. {
  108. InitializeComponent();
  109. this.DataContext = this;
  110. }
  111. #endregion
  112. #region Load document
  113. /// <summary>
  114. /// Initialize the PDFViewer and load the PDF file.
  115. /// </summary>
  116. /// <param name="filePath"></param>
  117. public void InitWithFilePath(string filePath)
  118. {
  119. pdfViewer = new PDFViewControl();
  120. pdfViewer.PDFView.InitDocument(filePath);
  121. }
  122. public void InitWithDocument(CPDFDocument document)
  123. {
  124. pdfViewer = new PDFViewControl();
  125. pdfViewer.PDFView.InitDocument(document);
  126. }
  127. /// <summary>
  128. /// Init controls and set events.
  129. /// </summary>
  130. private void LoadDocument()
  131. {
  132. if (pdfViewer.PDFView.Document == null)
  133. {
  134. return;
  135. }
  136. pdfViewer.PDFView.Load();
  137. pdfViewer.PDFView.SetShowLink(true);
  138. pdfViewer.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  139. pdfViewer.PDFView.InfoChanged += PdfViewer_InfoChanged;
  140. pdfViewer.PDFView.SetFormFieldHighlight(true);
  141. PasswordUI.Closed -= PasswordUI_Closed;
  142. PasswordUI.Canceled -= PasswordUI_Canceled;
  143. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  144. PasswordUI.Closed += PasswordUI_Closed;
  145. PasswordUI.Canceled += PasswordUI_Canceled;
  146. PasswordUI.Confirmed += PasswordUI_Confirmed;
  147. pdfViewer.PDFView.ChangeFitMode(FitMode.FitWidth);
  148. CPDFSaclingControl.InitWithPDFViewer(pdfViewer.PDFView);
  149. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.PDFView.ZoomFactor * 100)));
  150. ViewSettingBtn.IsChecked = false;
  151. botaBarControl.InitWithPDFViewer(pdfViewer.PDFView);
  152. ModeComboBox.SelectedIndex = 0;
  153. botaBarControl.AddBOTAContent(new[] { BOTATools.Thumbnail, BOTATools.Outline, BOTATools.Bookmark, BOTATools.Annotation, BOTATools.Search });
  154. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  155. botaBarControl.DeleteSignatureEvent -= BotaControlOnDeleteSignatureEvent;
  156. botaBarControl.DeleteSignatureEvent += BotaControlOnDeleteSignatureEvent;
  157. botaBarControl.ViewCertificateEvent -= digitalSignatureControl.ViewCertificateEvent;
  158. botaBarControl.ViewCertificateEvent += digitalSignatureControl.ViewCertificateEvent;
  159. botaBarControl.ViewSignatureEvent -= digitalSignatureControl.ViewSignatureEvent;
  160. botaBarControl.ViewSignatureEvent += digitalSignatureControl.ViewSignatureEvent;
  161. displaySettingsControl.InitWithPDFViewer(pdfViewer.PDFView);
  162. LoadCustomControl();
  163. panelState.PropertyChanged -= PanelState_PropertyChanged;
  164. panelState.PropertyChanged += PanelState_PropertyChanged;
  165. pdfViewer.PDFView.SetShowLink(Properties.Settings.Default.IsHighlightLinkArea);
  166. pdfViewer.PDFView.SetFormFieldHighlight(Properties.Settings.Default.IsHighlightFormArea);
  167. pdfViewer.PDFView.SetScrollStepDivisor(Properties.Settings.Default.Divisor / 100.0);
  168. }
  169. /// <summary>
  170. /// When the RightPanel state is changed, decide whether to display the ViewSettingPanel or RightToolPanel.
  171. /// </summary>
  172. /// <param name="sender"></param>
  173. /// <param name="e"></param>
  174. private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e)
  175. {
  176. if (e.PropertyName == "RightPanel")
  177. {
  178. OnPropertyChanged(nameof(RightToolPanelButtonIsChecked));
  179. OnPropertyChanged(nameof(ViewSettingBtnIsChecked));
  180. }
  181. }
  182. public void SetFeatureMode(string featureName)
  183. {
  184. if (!string.IsNullOrEmpty(featureName))
  185. {
  186. switch (featureName)
  187. {
  188. case "Viewer":
  189. ModeComboBox.SelectedIndex = 0;
  190. break;
  191. case "Annotations":
  192. ModeComboBox.SelectedIndex = 1;
  193. break;
  194. case "Forms":
  195. ModeComboBox.SelectedIndex = 2;
  196. break;
  197. case "Signatures":
  198. ModeComboBox.SelectedIndex = 5;
  199. break;
  200. case "Document Editor":
  201. ModeComboBox.SelectedIndex = 4;
  202. break;
  203. case "Content Editor":
  204. ModeComboBox.SelectedIndex = 3;
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. }
  211. internal void SetPDFViewer(PDFViewControl newPdfViewer)
  212. {
  213. if (newPdfViewer != null)
  214. {
  215. pdfViewer = newPdfViewer;
  216. }
  217. }
  218. #endregion
  219. #region Password
  220. /// <summary>
  221. /// Event handler for confirming the password. Open the PDF file if the password is correct.
  222. /// </summary>
  223. /// <param name="sender"></param>
  224. /// <param name="e"></param>
  225. private void PasswordUI_Confirmed(object sender, string e)
  226. {
  227. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  228. {
  229. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  230. if (passwordViewer.PDFView.Document.IsLocked == false)
  231. {
  232. PasswordUI.SetShowError("", Visibility.Collapsed);
  233. PasswordUI.ClearPassword();
  234. PasswordUI.Visibility = Visibility.Collapsed;
  235. PopupBorder.Visibility = Visibility.Collapsed;
  236. pdfViewer = passwordViewer;
  237. LoadDocument();
  238. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  239. }
  240. else
  241. {
  242. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// Event handler for canceling password input.
  248. /// </summary>
  249. /// <param name="sender"></param>
  250. /// <param name="e"></param>
  251. private void PasswordUI_Canceled(object sender, EventArgs e)
  252. {
  253. PopupBorder.Visibility = Visibility.Collapsed;
  254. PasswordUI.Visibility = Visibility.Collapsed;
  255. }
  256. /// <summary>
  257. /// Event handler for closing the password input dialog.
  258. /// </summary>
  259. /// <param name="sender"></param>
  260. /// <param name="e"></param>
  261. private void PasswordUI_Closed(object sender, EventArgs e)
  262. {
  263. PopupBorder.Visibility = Visibility.Collapsed;
  264. PasswordUI.Visibility = Visibility.Collapsed;
  265. }
  266. #endregion
  267. #region Load custom control
  268. /// <summary>
  269. /// Load the custom controls for the PDF viewer.
  270. /// </summary>
  271. private void LoadCustomControl()
  272. {
  273. regularViewerControl.PdfViewControl = pdfViewer;
  274. regularViewerControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  275. regularViewerControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  276. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  277. regularViewerControl.PdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  278. regularViewerControl.SetBOTAContainer(null);
  279. regularViewerControl.SetBOTAContainer(botaBarControl);
  280. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  281. PDFGrid.Child = regularViewerControl;
  282. SignatureHelper.InitEffectiveSignatureList(pdfViewer.PDFView.Document);
  283. SignatureHelper.VerifySignatureList(pdfViewer.PDFView.Document);
  284. digitalSignatureControl.LoadUndoManagerEvent(pdfViewer.PDFView);
  285. signatureStatusBarControl.SetStatus(SignatureHelper.SignatureList);
  286. }
  287. /// <summary>
  288. /// Event handler for the Loaded event of the MainPage.
  289. /// </summary>
  290. /// <param name="sender"></param>
  291. /// <param name="e"></param>
  292. private void MainPage_Loaded(object sender, RoutedEventArgs e)
  293. {
  294. LoadDocument();
  295. }
  296. #endregion
  297. #region Event handle
  298. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  299. {
  300. if (e.Key == "Zoom")
  301. {
  302. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  303. }
  304. }
  305. /// <summary>
  306. /// Command for opening a PDF file saved with a signature.
  307. /// </summary>
  308. /// <param name="sender"></param>
  309. /// <param name="e"></param>
  310. private void DigitalSignatureControl_AfterFillSignature(object sender, string e)
  311. {
  312. OpenFile(e);
  313. }
  314. /// <summary>
  315. /// Event handler for deleting a signature from the BOTA. Set the CanSave property to true and update the signature status.
  316. /// </summary>
  317. /// <param name="sender"></param>
  318. /// <param name="e"></param>
  319. private void BotaControlOnDeleteSignatureEvent(object sender, EventArgs e)
  320. {
  321. pdfViewer.PDFView.UndoManager.CanSave = true;
  322. DigitalSignatureControl_OnSignatureStatusChanged(sender, e);
  323. }
  324. /// <summary>
  325. /// Event handler for updating a signature. Update the signature status.
  326. /// </summary>
  327. /// <param name="sender"></param>
  328. /// <param name="e"></param>
  329. private void DigitalSignatureControl_OnSignatureStatusChanged(object sender, EventArgs e)
  330. {
  331. SignatureHelper.InitEffectiveSignatureList(pdfViewer.PDFView.Document);
  332. SignatureHelper.VerifySignatureList(pdfViewer.PDFView.Document);
  333. signatureStatusBarControl.SetStatus(SignatureHelper.SignatureList);
  334. botaBarControl.LoadSignatureList();
  335. }
  336. /// <summary>
  337. /// Event handler for updating the CanSave property.
  338. /// </summary>
  339. /// <param name="sender"></param>
  340. /// <param name="e"></param>
  341. private void DigitalSignatureControl_OnCanSaveChanged(object sender, bool e)
  342. {
  343. this.CanSave = e;
  344. }
  345. #endregion
  346. #region Private Command Event
  347. /// <summary>
  348. /// Close all the expanded panels.
  349. /// </summary>
  350. private void ClearPanelState()
  351. {
  352. LeftToolPanelButtonIsChecked = false;
  353. ViewSettingBtnIsChecked = false;
  354. RightToolPanelButtonIsChecked = false;
  355. }
  356. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  357. {
  358. panelState.IsLeftPanelExpand = (sender as ToggleButton).IsChecked == true;
  359. }
  360. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  361. {
  362. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  363. if ((string)item.Tag == currentMode)
  364. {
  365. return;
  366. }
  367. ClearPanelState();
  368. if (ViewSettingBtn != null)
  369. ViewSettingBtn.IsChecked = false;
  370. if (RightPanelButton != null)
  371. RightPanelButton.IsChecked = false;
  372. if (pdfViewer != null && pdfViewer.PDFView != null)
  373. {
  374. // pdfViewer.PDFView.ToolManager.EnableClickCreate = false;
  375. }
  376. if (currentMode == "Viewer")
  377. {
  378. regularViewerControl.ClearViewerControl();
  379. }
  380. else if (currentMode == "Annotation")
  381. {
  382. annotationControl.UnloadEvent();
  383. annotationControl.ClearViewerControl();
  384. }
  385. else if (currentMode == "Form")
  386. {
  387. formControl.UnloadEvent();
  388. formControl.ClearViewerControl();
  389. }
  390. else if (currentMode == "Content Editor")
  391. {
  392. contentEditControl.ClearViewerControl();
  393. contentEditControl.ClearPDFEditState();
  394. }
  395. else if (currentMode == "Document Editor")
  396. {
  397. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  398. NotDocsEditorVisible = Visibility.Visible;
  399. }
  400. else if (currentMode == "Digital Signature")
  401. {
  402. RightPanelButton.Visibility = Visibility.Visible;
  403. digitalSignatureControl.UnloadEvent();
  404. digitalSignatureControl.ClearViewerControl();
  405. botaBarControl.RemoveBOTAContent(BOTATools.Signature);
  406. }
  407. if (item.Tag as string == "Viewer")
  408. {
  409. if (regularViewerControl.PdfViewControl != null && regularViewerControl.PdfViewControl.PDFView != null)
  410. {
  411. PDFGrid.Child = regularViewerControl;
  412. pdfViewer.PDFView.SetMouseMode(MouseModes.Viewer);
  413. regularViewerControl.PdfViewControl = pdfViewer;
  414. regularViewerControl.InitWithPDFViewer(pdfViewer.PDFView);
  415. regularViewerControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  416. regularViewerControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  417. regularViewerControl.SetBOTAContainer(botaBarControl);
  418. regularViewerControl.SetDisplaySettingsControl(displaySettingsControl);
  419. }
  420. }
  421. else if (item.Tag as string == "Annotation")
  422. {
  423. annotationControl.SetToolBarContainerVisibility(Visibility.Visible);
  424. if (annotationControl.PDFViewControl != null && annotationControl.PDFViewControl.PDFView != null)
  425. {
  426. PDFGrid.Child = annotationControl;
  427. pdfViewer.PDFView.SetMouseMode(MouseModes.AnnotCreate);
  428. annotationControl.PDFViewControl.PDFView.SetToolParam(new AnnotHandlerEventArgs());
  429. annotationControl.PDFViewControl = pdfViewer;
  430. annotationControl.InitWithPDFViewer(pdfViewer.PDFView);
  431. annotationControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  432. annotationControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  433. annotationControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  434. annotationControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  435. annotationControl.InitialPDFViewControl(annotationControl.PDFViewControl);
  436. annotationControl.SetBOTAContainer(botaBarControl);
  437. annotationControl.SetDisplaySettingsControl(displaySettingsControl);
  438. }
  439. }
  440. else if (item.Tag as string == "Form")
  441. {
  442. formControl.SetToolBarContainerVisibility(Visibility.Visible);
  443. if (formControl.PdfViewControl != null && formControl.PdfViewControl.PDFView != null)
  444. {
  445. PDFGrid.Child = formControl;
  446. pdfViewer.PDFView.SetMouseMode(MouseModes.FormEditTool);
  447. formControl.PdfViewControl = pdfViewer;
  448. formControl.InitWithPDFViewer(pdfViewer.PDFView);
  449. formControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  450. formControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  451. formControl.OnAnnotEditHandler -= PdfFormControlRefreshAnnotList;
  452. formControl.OnAnnotEditHandler += PdfFormControlRefreshAnnotList;
  453. formControl.SetBOTAContainer(botaBarControl);
  454. formControl.InitialPDFViewControl(formControl.PdfViewControl);
  455. formControl.SetDisplaySettingsControl(displaySettingsControl);
  456. }
  457. }
  458. else if (item.Tag as string == "Content Editor")
  459. {
  460. if (contentEditControl.pdfContentEditControl != null && contentEditControl.PdfViewControl.PDFView != null)
  461. {
  462. pdfViewer.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage);
  463. pdfViewer.PDFView?.SetPDFEditCreateType(CPDFEditType.None);
  464. pdfViewer.PDFView?.SetMouseMode(MouseModes.PDFEdit);
  465. pdfViewer.PDFView?.ReloadDocument();
  466. pdfViewer.PDFView?.SetSplitMode(SplitMode.None);
  467. PDFGrid.Child = contentEditControl;
  468. pdfViewer.PDFView.SetMouseMode(MouseModes.PDFEdit);
  469. contentEditControl.PdfViewControl = pdfViewer;
  470. contentEditControl.InitWithPDFViewer(pdfViewer.PDFView);
  471. contentEditControl.OnCanSaveChanged -= ControlOnCanSaveChanged;
  472. contentEditControl.OnCanSaveChanged += ControlOnCanSaveChanged;
  473. contentEditControl.SetBOTAContainer(botaBarControl);
  474. contentEditControl.SetDisplaySettingsControl(displaySettingsControl);
  475. }
  476. }
  477. else if (item.Tag as string == "Document Editor")
  478. {
  479. pageEditControl.PDFViewControl = pdfViewer;
  480. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit;
  481. PDFGrid.Child = pageEditControl;
  482. NotDocsEditorVisible = Visibility.Collapsed;
  483. }
  484. else if (item.Tag as string == "Digital Signature")
  485. {
  486. if (contentEditControl.pdfContentEditControl != null && contentEditControl.PdfViewControl.PDFView != null)
  487. {
  488. RightPanelButton.Visibility = Visibility.Collapsed;
  489. PDFGrid.Child = digitalSignatureControl;
  490. digitalSignatureControl.PDFViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
  491. digitalSignatureControl.PDFViewControl = pdfViewer;
  492. botaBarControl.AddBOTAContent(BOTATools.Signature);
  493. digitalSignatureControl.InitWithPDFViewer(pdfViewer.PDFView);
  494. digitalSignatureControl.SetBOTAContainer(botaBarControl);
  495. digitalSignatureControl.SetDisplaySettingsControl(displaySettingsControl);
  496. digitalSignatureControl.SetSignatureStatusBarControl(signatureStatusBarControl);
  497. signatureStatusBarControl.OnViewSignatureButtonClicked -= ViewAllSignatures;
  498. signatureStatusBarControl.OnViewSignatureButtonClicked += ViewAllSignatures;
  499. digitalSignatureControl.OnCanSaveChanged -= DigitalSignatureControl_OnCanSaveChanged;
  500. digitalSignatureControl.OnCanSaveChanged += DigitalSignatureControl_OnCanSaveChanged;
  501. digitalSignatureControl.SignatureStatusChanged -= DigitalSignatureControl_OnSignatureStatusChanged;
  502. digitalSignatureControl.SignatureStatusChanged += DigitalSignatureControl_OnSignatureStatusChanged;
  503. digitalSignatureControl.AfterFillSignature -= DigitalSignatureControl_AfterFillSignature;
  504. digitalSignatureControl.AfterFillSignature += DigitalSignatureControl_AfterFillSignature;
  505. }
  506. }
  507. currentMode = item.Tag as string;
  508. RightToolPanelButtonIsChecked = false;
  509. }
  510. /// <summary>
  511. /// Event handler for ViewSignatureBtn click event. Expand the bota control and select the Signature tool.
  512. /// </summary>
  513. /// <param name="sender"></param>
  514. /// <param name="e"></param>
  515. private void ViewAllSignatures(object sender, EventArgs e)
  516. {
  517. LeftToolPanelButton.IsChecked = true;
  518. botaBarControl.SelectBotaTool(BOTATools.Signature);
  519. }
  520. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  521. {
  522. ModeComboBox.SelectedIndex = 0;
  523. }
  524. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  525. {
  526. PasswordUI.Visibility = Visibility.Collapsed;
  527. FileInfoUI.Visibility = Visibility.Visible;
  528. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  529. PopupBorder.Visibility = Visibility.Visible;
  530. }
  531. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  532. {
  533. panelState.RightPanel =
  534. ((sender as ToggleButton).IsChecked == true) ?
  535. PanelState.RightPanelState.ViewSettings : PanelState.RightPanelState.None;
  536. }
  537. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  538. {
  539. panelState.RightPanel =
  540. ((sender as ToggleButton).IsChecked == true) ?
  541. PanelState.RightPanelState.PropertyPanel : PanelState.RightPanelState.None;
  542. }
  543. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  544. {
  545. LeftToolPanelButton.IsChecked = true;
  546. botaBarControl.SelectBotaTool(BOTATools.Search);
  547. }
  548. private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e)
  549. {
  550. CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent;
  551. CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent;
  552. CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent;
  553. CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent;
  554. CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent;
  555. CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent;
  556. CPDFTitleBarControl.FlattenEvent -= CPDFTitleBarControl_FlattenEvent;
  557. CPDFTitleBarControl.FlattenEvent += CPDFTitleBarControl_FlattenEvent;
  558. }
  559. private void CPDFTitleBarControl_FlattenEvent(object sender, EventArgs e)
  560. {
  561. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  562. {
  563. string savePath = CommonHelper.GetGeneratePathOrEmpty("PDF files (*.pdf)|*.pdf", pdfViewer.PDFView.Document.FileName + "_Flattened.pdf");
  564. if (!string.IsNullOrEmpty(savePath))
  565. {
  566. if (CanSave)
  567. {
  568. SaveFile();
  569. pdfViewer.PDFView.UndoManager.CanSave = false;
  570. }
  571. CPDFDocument document = CPDFDocument.InitWithFilePath(pdfViewer.PDFView.Document.FilePath);
  572. if (document?.WriteFlattenToFilePath(savePath) == true)
  573. {
  574. OpenFile(savePath);
  575. }
  576. document?.Release();
  577. }
  578. }
  579. }
  580. private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e)
  581. {
  582. SaveFile();
  583. }
  584. private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e)
  585. {
  586. SaveAsFile();
  587. }
  588. private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e)
  589. {
  590. OpenFile();
  591. }
  592. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  593. {
  594. PopupBorder.Visibility = Visibility.Collapsed;
  595. }
  596. /// <summary>
  597. /// Refresh the annotation list when a annotation is edited.
  598. /// </summary>
  599. /// <param name="sender"></param>
  600. /// <param name="e"></param>
  601. private void PdfFormControlRefreshAnnotList(object sender, EventArgs e)
  602. {
  603. botaBarControl.LoadAnnotationList();
  604. }
  605. /// <summary>
  606. /// When a CanSave property of a control is changed, the CanSave property of current page is changed.
  607. /// </summary>
  608. /// <param name="sender"></param>
  609. /// <param name="e"></param>
  610. private void ControlOnCanSaveChanged(object sender, bool e)
  611. {
  612. this.CanSave = e;
  613. }
  614. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  615. {
  616. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  617. }
  618. #endregion
  619. #region Open and Save file
  620. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  621. {
  622. SaveFile();
  623. pdfViewer.PDFView.UndoManager.CanSave = false;
  624. }
  625. private void OpenFile(string filePath = "")
  626. {
  627. if (string.IsNullOrEmpty(filePath))
  628. {
  629. filePath = CommonHelper.GetExistedPathOrEmpty();
  630. }
  631. string oldFilePath = pdfViewer.PDFView.Document.FilePath;
  632. if (!string.IsNullOrEmpty(filePath) && regularViewerControl.PdfViewControl != null)
  633. {
  634. if (pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  635. {
  636. if (oldFilePath.ToLower() == filePath.ToLower())
  637. {
  638. return;
  639. }
  640. }
  641. if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath }))
  642. {
  643. return;
  644. }
  645. passwordViewer = new PDFViewControl();
  646. passwordViewer.PDFView.InitDocument(filePath);
  647. if (passwordViewer.PDFView.Document == null)
  648. {
  649. MessageBox.Show("Open File Failed");
  650. return;
  651. }
  652. if (passwordViewer.PDFView.Document.IsLocked)
  653. {
  654. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  655. PasswordUI.ClearPassword();
  656. PopupBorder.Visibility = Visibility.Visible;
  657. PasswordUI.Visibility = Visibility.Visible;
  658. }
  659. else
  660. {
  661. pdfViewer.PDFView.Document.Release();
  662. pdfViewer = passwordViewer;
  663. LoadDocument();
  664. FileChangeEvent?.Invoke(null, EventArgs.Empty);
  665. }
  666. }
  667. }
  668. private void OpenFile_Click(object sender, RoutedEventArgs e)
  669. {
  670. OpenFile();
  671. }
  672. /// <summary>
  673. /// Save the file to another PDF file.
  674. /// </summary>
  675. public void SaveAsFile()
  676. {
  677. {
  678. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  679. {
  680. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  681. SaveFileDialog saveDialog = new SaveFileDialog
  682. {
  683. Filter = "(*.pdf)|*.pdf",
  684. DefaultExt = ".pdf",
  685. OverwritePrompt = true
  686. };
  687. if (saveDialog.ShowDialog() == true)
  688. {
  689. if (pdfDoc.WriteToFilePath(saveDialog.FileName))
  690. {
  691. AfterSaveAsFileEvent?.Invoke(this, saveDialog.FileName);
  692. }
  693. }
  694. }
  695. }
  696. }
  697. /// <summary>
  698. /// Save the file in the current path.
  699. /// </summary>
  700. public void SaveFile()
  701. {
  702. if (pdfViewer != null && pdfViewer.PDFView != null && pdfViewer.PDFView.Document != null)
  703. {
  704. try
  705. {
  706. CPDFDocument pdfDoc = pdfViewer.PDFView.Document;
  707. if (!string.IsNullOrEmpty(pdfDoc.FilePath))
  708. {
  709. if (pdfDoc.WriteToLoadedPath())
  710. {
  711. return;
  712. }
  713. }
  714. SaveFileDialog saveDialog = new SaveFileDialog
  715. {
  716. Filter = "(*.pdf)|*.pdf",
  717. DefaultExt = ".pdf",
  718. OverwritePrompt = true
  719. };
  720. if (saveDialog.ShowDialog() == true)
  721. {
  722. if (pdfDoc.WriteToFilePath(saveDialog.FileName))
  723. {
  724. AfterSaveAsFileEvent?.Invoke(this, saveDialog.FileName);
  725. }
  726. }
  727. }
  728. catch (Exception ex)
  729. {
  730. return;
  731. }
  732. }
  733. }
  734. #endregion
  735. #region Command Binding
  736. private double CheckZoomLevel(double zoom, bool IsGrowth)
  737. {
  738. double standardZoom = 100;
  739. if (zoom <= 0.01)
  740. {
  741. return 0.01;
  742. }
  743. if (zoom >= 10)
  744. {
  745. return 10;
  746. }
  747. zoom *= 100;
  748. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  749. {
  750. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  751. {
  752. standardZoom = zoomLevelList[i + 1];
  753. break;
  754. }
  755. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  756. {
  757. standardZoom = zoomLevelList[i];
  758. break;
  759. }
  760. }
  761. return standardZoom / 100;
  762. }
  763. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  764. {
  765. OpenFile();
  766. }
  767. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  768. {
  769. if (CanSave)
  770. {
  771. SaveFile();
  772. }
  773. }
  774. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  775. {
  776. SaveAsFile();
  777. }
  778. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  779. {
  780. panelState.IsLeftPanelExpand = !panelState.IsLeftPanelExpand;
  781. }
  782. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  783. {
  784. if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel)
  785. {
  786. panelState.RightPanel = PanelState.RightPanelState.None;
  787. }
  788. else
  789. {
  790. panelState.RightPanel = PanelState.RightPanelState.PropertyPanel;
  791. }
  792. }
  793. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  794. {
  795. LeftToolPanelButton.IsChecked = true;
  796. botaBarControl.SelectBotaTool(BOTATools.Bookmark);
  797. }
  798. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  799. {
  800. LeftToolPanelButton.IsChecked = true;
  801. botaBarControl.SelectBotaTool(BOTATools.Outline);
  802. }
  803. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  804. {
  805. LeftToolPanelButton.IsChecked = true;
  806. botaBarControl.SelectBotaTool(BOTATools.Thumbnail);
  807. }
  808. private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
  809. {
  810. LeftToolPanelButton.IsChecked = true;
  811. botaBarControl.SelectBotaTool(BOTATools.Annotation);
  812. }
  813. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  814. {
  815. LeftToolPanelButton.IsChecked = true;
  816. botaBarControl.SelectBotaTool(BOTATools.Search);
  817. }
  818. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  819. {
  820. double newZoom = CheckZoomLevel(pdfViewer.PDFView.ZoomFactor + 0.01, true);
  821. pdfViewer.PDFView?.Zoom(newZoom);
  822. }
  823. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  824. {
  825. double newZoom = CheckZoomLevel(pdfViewer.PDFView.ZoomFactor - 0.01, false);
  826. pdfViewer.PDFView?.Zoom(newZoom);
  827. }
  828. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  829. {
  830. panelState.RightPanel = PanelState.RightPanelState.ViewSettings;
  831. }
  832. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  833. {
  834. if (PopupBorder.Visibility != Visibility.Visible)
  835. {
  836. PasswordUI.Visibility = Visibility.Collapsed;
  837. FileInfoControl.Visibility = Visibility.Visible;
  838. FileInfoControl.InitWithPDFViewer(pdfViewer.PDFView);
  839. FileInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
  840. FileInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
  841. PopupBorder.Visibility = Visibility.Visible;
  842. }
  843. else
  844. {
  845. FileInfoControl.Visibility = Visibility.Collapsed;
  846. PopupBorder.Visibility = Visibility.Collapsed;
  847. this.Focus();
  848. }
  849. }
  850. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  851. {
  852. PopupBorder.Visibility = Visibility.Collapsed;
  853. }
  854. #endregion
  855. }
  856. }