MainPage.xaml.cs 30 KB

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