MainWindow.xaml.cs 32 KB

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