MainWindow.xaml.cs 32 KB

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