MainWindow.xaml.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Helper;
  3. using Compdfkit_Tools.PDFControl;
  4. using ComPDFKitViewer;
  5. using Microsoft.Win32;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Runtime.CompilerServices;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Input;
  17. using System.Windows.Media.Imaging;
  18. namespace Viewer
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window, INotifyPropertyChanged
  24. {
  25. #region Property
  26. private PDFViewControl passwordViewer;
  27. private PDFViewControl pdfViewControl;
  28. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  29. private int[] zoomLevelList = { 10, 25, 50, 100, 150, 200, 300, 400, 500, 1000 };
  30. public event PropertyChangedEventHandler PropertyChanged;
  31. private KeyEventHandler KeyDownHandler;
  32. public bool CanSave
  33. {
  34. get
  35. {
  36. if (pdfViewControl != null && pdfViewControl.PDFViewTool.GetCPDFViewer() != null)
  37. {
  38. if (pdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanRedo ||
  39. pdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.CanUndo)
  40. {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. }
  47. #endregion
  48. public MainWindow()
  49. {
  50. InitializeComponent();
  51. Loaded += MainWindow_Loaded;
  52. DataContext = this;
  53. }
  54. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  55. {
  56. BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search);
  57. LoadDefaultDocument();
  58. }
  59. #region Load Document
  60. private void LoadDocument()
  61. {
  62. if (pdfViewControl.PDFToolManager.GetDocument() == null)
  63. {
  64. return;
  65. }
  66. pdfViewControl.PDFToolManager?.SetToolType(ComPDFKit.Tool.CPDFToolManager.ToolType.Viewer);
  67. pdfViewControl.PDFView?.Load();
  68. pdfViewControl.PDFView?.SetShowLink(true);
  69. PDFGrid.Child = pdfViewControl;
  70. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  71. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  72. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  73. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  74. pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
  75. pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  76. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  77. PasswordUI.Closed -= PasswordUI_Closed;
  78. PasswordUI.Canceled -= PasswordUI_Canceled;
  79. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  80. PasswordUI.Closed += PasswordUI_Closed;
  81. PasswordUI.Canceled += PasswordUI_Canceled;
  82. PasswordUI.Confirmed += PasswordUI_Confirmed;
  83. pdfViewControl.PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitWidth);
  84. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl);
  85. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() * 100)));
  86. FloatPageTool.InitWithPDFViewer(pdfViewControl);
  87. BotaSideTool.InitWithPDFViewer(pdfViewControl);
  88. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  89. ViewSettingBtn.IsChecked = false;
  90. PropertyContainer.Child = null;
  91. PropertyContainer.Visibility = Visibility.Collapsed;
  92. }
  93. private void LoadDefaultDocument()
  94. {
  95. string defaultFilePath = "PDF32000_2008.pdf";
  96. pdfViewControl = new PDFViewControl();
  97. pdfViewControl.InitDocument(defaultFilePath);
  98. LoadDocument();
  99. }
  100. #endregion
  101. #region Password
  102. private void PasswordUI_Confirmed(object sender, string e)
  103. {
  104. if (passwordViewer != null && passwordViewer.PDFToolManager != null && passwordViewer.PDFToolManager.GetDocument() != null)
  105. {
  106. passwordViewer.PDFToolManager.GetDocument().UnlockWithPassword(e);
  107. if (passwordViewer.PDFToolManager.GetDocument().IsLocked == false)
  108. {
  109. PasswordUI.SetShowError("", Visibility.Collapsed);
  110. PasswordUI.ClearPassword();
  111. PasswordUI.Visibility = Visibility.Collapsed;
  112. PopupBorder.Visibility = Visibility.Collapsed;
  113. pdfViewControl = passwordViewer;
  114. LoadDocument();
  115. }
  116. else
  117. {
  118. PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
  119. }
  120. }
  121. }
  122. private void PasswordUI_Canceled(object sender, EventArgs e)
  123. {
  124. PopupBorder.Visibility = Visibility.Collapsed;
  125. PasswordUI.Visibility = Visibility.Collapsed;
  126. }
  127. private void PasswordUI_Closed(object sender, EventArgs e)
  128. {
  129. PopupBorder.Visibility = Visibility.Collapsed;
  130. PasswordUI.Visibility = Visibility.Collapsed;
  131. }
  132. #endregion
  133. #region Expand or Hide Panel
  134. private void ControlLeftPanel()
  135. {
  136. if (LeftToolPanelButton != null)
  137. {
  138. bool isExpand = LeftToolPanelButton.IsChecked == true;
  139. ExpandLeftPanel(isExpand);
  140. }
  141. }
  142. private void ExpandLeftPanel(bool isExpand)
  143. {
  144. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  145. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  146. if (isExpand)
  147. {
  148. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  149. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  150. }
  151. else
  152. {
  153. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  154. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  155. }
  156. }
  157. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  158. {
  159. ExpandLeftPanel(true);
  160. BotaSideTool.SelectBotaTool(BOTATools.Search);
  161. }
  162. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  163. {
  164. ToggleButton expandBtn = sender as ToggleButton;
  165. if (expandBtn != null)
  166. {
  167. bool isExpand = expandBtn.IsChecked == true;
  168. ExpandLeftPanel(isExpand);
  169. }
  170. }
  171. #endregion
  172. #region UI
  173. private double CheckZoomLevel(double zoom, bool IsGrowth)
  174. {
  175. double standardZoom = 100;
  176. if (zoom <= 0.01)
  177. {
  178. return 0.01;
  179. }
  180. if (zoom >= 10)
  181. {
  182. return 10;
  183. }
  184. zoom *= 100;
  185. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  186. {
  187. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  188. {
  189. standardZoom = zoomLevelList[i + 1];
  190. break;
  191. }
  192. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  193. {
  194. standardZoom = zoomLevelList[i];
  195. break;
  196. }
  197. }
  198. return standardZoom / 100;
  199. }
  200. private void ShowViewSettings()
  201. {
  202. if (ViewSettingBtn != null)
  203. {
  204. if (ViewSettingBtn.IsChecked == true)
  205. {
  206. displayPanel.InitWithPDFViewer(pdfViewControl);
  207. PropertyContainer.Child = displayPanel;
  208. PropertyContainer.Visibility = Visibility.Visible;
  209. }
  210. else
  211. {
  212. PropertyContainer.Child = null;
  213. PropertyContainer.Visibility = Visibility.Collapsed;
  214. }
  215. }
  216. }
  217. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  218. {
  219. ShowViewSettings();
  220. }
  221. private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
  222. {
  223. if (pdfViewControl != null)
  224. {
  225. double newZoom = CheckZoomLevel(pdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() + 0.01, true);
  226. pdfViewControl.PDFViewTool.GetCPDFViewer().SetZoom(newZoom);
  227. }
  228. }
  229. private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
  230. {
  231. if (pdfViewControl != null)
  232. {
  233. double newZoom = CheckZoomLevel(pdfViewControl.PDFViewTool.GetCPDFViewer().GetZoom() - 0.01, false);
  234. pdfViewControl.PDFViewTool.GetCPDFViewer().SetZoom(newZoom);
  235. }
  236. }
  237. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  238. {
  239. pdfViewControl.PDFViewTool.GetCPDFViewer()?.GoToPage(pdfViewControl.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex + 1,new System.Windows.Point(0,0));
  240. }
  241. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  242. {
  243. pdfViewControl.PDFViewTool.GetCPDFViewer()?.GoToPage(pdfViewControl.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex - 1, new System.Windows.Point(0, 0));
  244. }
  245. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  246. {
  247. PasswordUI.Visibility = Visibility.Collapsed;
  248. FileInfoUI.Visibility = Visibility.Visible;
  249. FileInfoControl.InitWithPDFViewer(pdfViewControl);
  250. PopupBorder.Visibility = Visibility.Visible;
  251. }
  252. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  253. {
  254. PopupBorder.Visibility = Visibility.Collapsed;
  255. }
  256. #endregion
  257. #region Open and save file
  258. /// <summary>
  259. /// Save the file to another PDF file.
  260. /// </summary>
  261. public void SaveAsFile()
  262. {
  263. {
  264. if (pdfViewControl != null && pdfViewControl.PDFToolManager != null && pdfViewControl.PDFToolManager.GetDocument() != null)
  265. {
  266. CPDFDocument pdfDoc = pdfViewControl.PDFToolManager.GetDocument();
  267. SaveFileDialog saveDialog = new SaveFileDialog();
  268. saveDialog.Filter = "(*.pdf)|*.pdf";
  269. saveDialog.DefaultExt = ".pdf";
  270. saveDialog.OverwritePrompt = true;
  271. if (saveDialog.ShowDialog() == true)
  272. {
  273. pdfDoc.WriteToFilePath(saveDialog.FileName);
  274. }
  275. }
  276. }
  277. }
  278. private void SaveFile()
  279. {
  280. if (pdfViewControl != null && pdfViewControl.PDFToolManager != null && pdfViewControl.PDFToolManager.GetDocument() != null)
  281. {
  282. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  283. if (pdfDoc.WriteToLoadedPath())
  284. {
  285. pdfViewControl.PDFView.UndoManager.CanSave = false;
  286. return;
  287. }
  288. SaveFileDialog saveDialog = new SaveFileDialog();
  289. saveDialog.Filter = "(*.pdf)|*.pdf";
  290. saveDialog.DefaultExt = ".pdf";
  291. saveDialog.OverwritePrompt = true;
  292. if (saveDialog.ShowDialog() == true)
  293. {
  294. if (pdfDoc.WriteToFilePath(saveDialog.FileName))
  295. {
  296. pdfViewControl.PDFView.UndoManager.CanSave = false;
  297. }
  298. }
  299. }
  300. }
  301. private void OpenFile()
  302. {
  303. string filePath = CommonHelper.GetExistedPathOrEmpty();
  304. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  305. {
  306. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  307. {
  308. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  309. if (oldFilePath.ToLower() == filePath.ToLower())
  310. {
  311. return;
  312. }
  313. }
  314. passwordViewer = new PDFViewControl();
  315. passwordViewer.PDFView.InitDocument(filePath);
  316. if (passwordViewer.PDFView.Document == null)
  317. {
  318. MessageBox.Show("Open File Failed");
  319. return;
  320. }
  321. if (passwordViewer.PDFView.Document.IsLocked)
  322. {
  323. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  324. PasswordUI.ClearPassword();
  325. PopupBorder.Visibility = Visibility.Visible;
  326. PasswordUI.Visibility = Visibility.Visible;
  327. }
  328. else
  329. {
  330. pdfViewControl.PDFView.Document.Release();
  331. pdfViewControl = passwordViewer;
  332. LoadDocument();
  333. }
  334. }
  335. }
  336. private void OpenFile_Click(object sender, RoutedEventArgs e)
  337. {
  338. OpenFile();
  339. }
  340. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  341. {
  342. SaveFile();
  343. }
  344. #endregion
  345. #region Context Menu
  346. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  347. {
  348. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  349. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  350. {
  351. string choosePath = folderDialog.SelectedPath;
  352. string openPath = choosePath;
  353. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  354. if (imageDict != null && imageDict.Count > 0)
  355. {
  356. foreach (int pageIndex in imageDict.Keys)
  357. {
  358. List<Bitmap> imageList = imageDict[pageIndex];
  359. foreach (Bitmap image in imageList)
  360. {
  361. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  362. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  363. openPath = savePath;
  364. }
  365. }
  366. }
  367. Process.Start("explorer", "/select,\"" + openPath + "\"");
  368. }
  369. }
  370. private void CopyImage_Click(object sender, RoutedEventArgs e)
  371. {
  372. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  373. if (imageDict != null && imageDict.Count > 0)
  374. {
  375. foreach (int pageIndex in imageDict.Keys)
  376. {
  377. List<Bitmap> imageList = imageDict[pageIndex];
  378. foreach (Bitmap image in imageList)
  379. {
  380. MemoryStream ms = new MemoryStream();
  381. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  382. BitmapImage imageData = new BitmapImage();
  383. imageData.BeginInit();
  384. imageData.StreamSource = ms;
  385. imageData.CacheOption = BitmapCacheOption.OnLoad;
  386. imageData.EndInit();
  387. imageData.Freeze();
  388. Clipboard.SetImage(imageData);
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  395. {
  396. if (e != null && e.CommandType == CommandType.Context)
  397. {
  398. if (e.PressOnSelectedText)
  399. {
  400. e.Handle = true;
  401. e.PopupMenu = new ContextMenu();
  402. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  403. }
  404. else if (e.CommandTarget == TargetType.ImageSelection)
  405. {
  406. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  407. {
  408. e.Handle = true;
  409. e.PopupMenu = new ContextMenu();
  410. MenuItem imageCopyMenu = new MenuItem();
  411. imageCopyMenu = new MenuItem();
  412. imageCopyMenu.Header = "Copy Images";
  413. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  414. imageCopyMenu.CommandParameter = e;
  415. e.PopupMenu.Items.Add(imageCopyMenu);
  416. MenuItem imageExtraMenu = new MenuItem();
  417. imageExtraMenu = new MenuItem();
  418. imageExtraMenu.Header = "Extract Images";
  419. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  420. imageExtraMenu.CommandParameter = e;
  421. e.PopupMenu.Items.Add(imageExtraMenu);
  422. }
  423. }
  424. else
  425. {
  426. e.Handle = true;
  427. e.PopupMenu = new ContextMenu();
  428. //if (pdfViewControl.CheckHasForm())
  429. MenuItem fitWidthMenu = new MenuItem();
  430. fitWidthMenu.Header = "Automatically Resize";
  431. fitWidthMenu.Click += (o, p) =>
  432. {
  433. if (pdfViewControl != null)
  434. {
  435. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  436. }
  437. };
  438. e.PopupMenu.Items.Add(fitWidthMenu);
  439. MenuItem fitSizeMenu = new MenuItem();
  440. fitSizeMenu.Header = "Actual Size";
  441. fitSizeMenu.Click += (o, p) =>
  442. {
  443. if (pdfViewControl != null)
  444. {
  445. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  446. }
  447. };
  448. e.PopupMenu.Items.Add(fitSizeMenu);
  449. MenuItem zoomInMenu = new MenuItem();
  450. zoomInMenu.Header = "Zoom In";
  451. zoomInMenu.Click += (o, p) =>
  452. {
  453. if (pdfViewControl != null)
  454. {
  455. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  456. pdfViewControl.PDFView?.Zoom(newZoom);
  457. }
  458. };
  459. e.PopupMenu.Items.Add(zoomInMenu);
  460. MenuItem zoomOutMenu = new MenuItem();
  461. zoomOutMenu.Header = "Zoom Out";
  462. zoomOutMenu.Click += (o, p) =>
  463. {
  464. if (pdfViewControl != null)
  465. {
  466. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  467. pdfViewControl.PDFView?.Zoom(newZoom);
  468. }
  469. };
  470. e.PopupMenu.Items.Add(zoomOutMenu);
  471. e.PopupMenu.Items.Add(new Separator());
  472. MenuItem singleView = new MenuItem();
  473. singleView.Header = "Single Page";
  474. singleView.Click += (o, p) =>
  475. {
  476. if (pdfViewControl != null)
  477. {
  478. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  479. }
  480. };
  481. e.PopupMenu.Items.Add(singleView);
  482. MenuItem singleContinuousView = new MenuItem();
  483. singleContinuousView.Header = "Single Page Continuous";
  484. singleContinuousView.Click += (o, p) =>
  485. {
  486. if (pdfViewControl != null)
  487. {
  488. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  489. }
  490. };
  491. e.PopupMenu.Items.Add(singleContinuousView);
  492. MenuItem doubleView = new MenuItem();
  493. doubleView.Header = "Two Pages";
  494. doubleView.Click += (o, p) =>
  495. {
  496. if (pdfViewControl != null)
  497. {
  498. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  499. }
  500. };
  501. e.PopupMenu.Items.Add(doubleView);
  502. MenuItem doubleContinuousView = new MenuItem();
  503. doubleContinuousView.Header = "Two Pages Continuous";
  504. doubleContinuousView.Click += (o, p) =>
  505. {
  506. if (pdfViewControl != null)
  507. {
  508. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  509. }
  510. };
  511. e.PopupMenu.Items.Add(doubleContinuousView);
  512. {
  513. MenuItem resetForms = new MenuItem();
  514. resetForms.Header = "Reset Forms";
  515. resetForms.Click += (o, p) =>
  516. {
  517. if (pdfViewControl != null)
  518. {
  519. pdfViewControl.PDFView?.ResetForm(null);
  520. }
  521. };
  522. e.PopupMenu.Items.Add(new Separator());
  523. e.PopupMenu.Items.Add(resetForms);
  524. }
  525. }
  526. }
  527. if (e != null && e.CommandType == CommandType.Copy)
  528. {
  529. e.DoCommand();
  530. }
  531. }
  532. #endregion
  533. #region Close Window
  534. protected override void OnClosing(CancelEventArgs e)
  535. {
  536. if (pdfViewControl.PDFView.UndoManager.CanSave)
  537. {
  538. MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
  539. if (result == MessageBoxResult.Yes)
  540. {
  541. SaveFile();
  542. }
  543. else if (result == MessageBoxResult.No)
  544. {
  545. }
  546. else
  547. {
  548. e.Cancel = true;
  549. }
  550. }
  551. }
  552. #endregion
  553. #region PropertyChanged
  554. /// <summary>
  555. /// Zoom
  556. /// </summary>
  557. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  558. {
  559. if (e.Key == "Zoom")
  560. {
  561. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  562. }
  563. }
  564. /// <summary>
  565. /// Undo Redo Event Noitfy
  566. /// </summary>
  567. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  568. {
  569. OnPropertyChanged(e.PropertyName);
  570. }
  571. protected void OnPropertyChanged([CallerMemberName] string name = null)
  572. {
  573. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  574. }
  575. #endregion
  576. #region Shortcut
  577. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  578. {
  579. OpenFile();
  580. }
  581. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  582. {
  583. if (CanSave)
  584. {
  585. SaveFile();
  586. }
  587. }
  588. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  589. {
  590. SaveAsFile();
  591. }
  592. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  593. {
  594. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  595. ControlLeftPanel();
  596. }
  597. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  598. {
  599. ExpandLeftPanel(true);
  600. LeftToolPanelButton.IsChecked = true;
  601. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  602. }
  603. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  604. {
  605. ExpandLeftPanel(true);
  606. LeftToolPanelButton.IsChecked = true;
  607. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  608. }
  609. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  610. {
  611. ExpandLeftPanel(true);
  612. LeftToolPanelButton.IsChecked = true;
  613. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  614. }
  615. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  616. {
  617. ExpandLeftPanel(true);
  618. LeftToolPanelButton.IsChecked = true;
  619. BotaSideTool.SelectBotaTool(BOTATools.Search);
  620. }
  621. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  622. {
  623. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  624. pdfViewControl.PDFView?.Zoom(newZoom);
  625. }
  626. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  627. {
  628. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  629. pdfViewControl.PDFView?.Zoom(newZoom);
  630. }
  631. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  632. {
  633. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  634. ShowViewSettings();
  635. }
  636. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  637. {
  638. if (PopupBorder.Visibility != Visibility.Visible)
  639. {
  640. PasswordUI.Visibility = Visibility.Collapsed;
  641. FileInfoUI.Visibility = Visibility.Visible;
  642. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  643. PopupBorder.Visibility = Visibility.Visible;
  644. }
  645. else
  646. {
  647. FileInfoUI.Visibility = Visibility.Collapsed;
  648. PopupBorder.Visibility = Visibility.Collapsed;
  649. }
  650. }
  651. #endregion
  652. }
  653. }