MainWindow.xaml.cs 32 KB

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