MainPage.xaml.cs 38 KB

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