MainWindow.xaml.cs 33 KB

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