MainPage.xaml.cs 28 KB

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