MainWindow.xaml.cs 27 KB

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