MainWindow.xaml.cs 32 KB

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