MainPage.xaml.cs 30 KB

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