MainWindow.xaml.cs 30 KB

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