MainPage.xaml.cs 31 KB

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