MainWindow.xaml.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. passwordViewer.Password = e;
  333. pdfViewControl = passwordViewer;
  334. LoadDocument();
  335. }
  336. else
  337. {
  338. PasswordUI.SetShowError("error", Visibility.Visible);
  339. }
  340. }
  341. }
  342. #endregion
  343. #region Expand and collapse Panel
  344. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  345. {
  346. PropertyContainer.Width = 260;
  347. PropertyContainer.Child = properytPanel;
  348. PropertyContainer.Visibility = visible;
  349. if (visible == Visibility.Hidden || visible == Visibility.Collapsed)
  350. {
  351. RightPanelButton.IsChecked = false;
  352. }
  353. }
  354. private void ExpandLeftPanel(bool isExpand)
  355. {
  356. BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  357. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  358. if (isExpand)
  359. {
  360. BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
  361. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  362. }
  363. else
  364. {
  365. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  366. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  367. }
  368. }
  369. #endregion
  370. #region UI
  371. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  372. {
  373. PasswordUI.Visibility = Visibility.Collapsed;
  374. FileInfoUI.Visibility = Visibility.Visible;
  375. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  376. PopupBorder.Visibility = Visibility.Visible;
  377. }
  378. private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
  379. {
  380. PopupBorder.Visibility = Visibility.Collapsed;
  381. }
  382. private void OpenFile()
  383. {
  384. string filePath = CommonHelper.GetExistedPathOrEmpty();
  385. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  386. {
  387. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  388. {
  389. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  390. if (oldFilePath.ToLower() == filePath.ToLower())
  391. {
  392. return;
  393. }
  394. else
  395. {
  396. pdfViewControl.PDFView.Document.Release();
  397. }
  398. }
  399. passwordViewer = new PDFViewControl();
  400. passwordViewer.PDFView.InitDocument(filePath);
  401. if (passwordViewer.PDFView.Document == null)
  402. {
  403. MessageBox.Show("Open File Failed");
  404. return;
  405. }
  406. if (passwordViewer.PDFView.Document.IsLocked)
  407. {
  408. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " " + LanguageHelper.CommonManager.GetString("Tip_Encrypted"));
  409. PasswordUI.ClearPassword();
  410. PopupBorder.Visibility = Visibility.Visible;
  411. PasswordUI.Visibility = Visibility.Visible;
  412. }
  413. else
  414. {
  415. pdfViewControl.PDFView.Document.Release();
  416. pdfViewControl = passwordViewer;
  417. LoadDocument();
  418. }
  419. }
  420. }
  421. private void OpenFile_Click(object sender, RoutedEventArgs e)
  422. {
  423. OpenFile();
  424. }
  425. private void ControlLeftPanel()
  426. {
  427. if (LeftToolPanelButton != null)
  428. {
  429. bool isExpand = LeftToolPanelButton.IsChecked == true;
  430. ExpandLeftPanel(isExpand);
  431. }
  432. }
  433. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  434. {
  435. ControlLeftPanel();
  436. }
  437. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  438. {
  439. SaveFile();
  440. pdfViewControl.PDFView.UndoManager.CanSave = false;
  441. }
  442. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  443. {
  444. PopupBorder.Visibility = Visibility.Collapsed;
  445. }
  446. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  447. {
  448. ExpandLeftPanel(true);
  449. LeftToolPanelButton.IsChecked = true;
  450. BotaSideTool.SelectBotaTool(BOTATools.Search);
  451. }
  452. private void ControlRightPanel()
  453. {
  454. if ((bool)ViewSettingBtn.IsChecked)
  455. {
  456. ViewSettingBtn.IsChecked = false;
  457. }
  458. if (RightPanelButton != null)
  459. {
  460. if (RightPanelButton.IsChecked == true)
  461. {
  462. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  463. }
  464. else
  465. {
  466. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  467. }
  468. }
  469. }
  470. private void RightPanelButton_Click(object sender, RoutedEventArgs e)
  471. {
  472. ControlRightPanel();
  473. }
  474. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  475. {
  476. ShowViewSettings();
  477. }
  478. private double CheckZoomLevel(double zoom, bool IsGrowth)
  479. {
  480. double standardZoom = 100;
  481. if (zoom <= 0.01)
  482. {
  483. return 0.01;
  484. }
  485. if (zoom >= 10)
  486. {
  487. return 10;
  488. }
  489. zoom *= 100;
  490. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  491. {
  492. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  493. {
  494. standardZoom = zoomLevelList[i + 1];
  495. break;
  496. }
  497. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  498. {
  499. standardZoom = zoomLevelList[i];
  500. break;
  501. }
  502. }
  503. return standardZoom / 100;
  504. }
  505. #endregion
  506. #region Save file
  507. /// <summary>
  508. /// Save the file to another PDF file.
  509. /// </summary>
  510. public void SaveAsFile()
  511. {
  512. {
  513. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  514. {
  515. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  516. SaveFileDialog saveDialog = new SaveFileDialog();
  517. saveDialog.Filter = "(*.pdf)|*.pdf";
  518. saveDialog.DefaultExt = ".pdf";
  519. saveDialog.OverwritePrompt = true;
  520. if (saveDialog.ShowDialog() == true)
  521. {
  522. pdfDoc.WriteToFilePath(saveDialog.FileName);
  523. }
  524. }
  525. }
  526. }
  527. /// <summary>
  528. /// Save the file in the current path.
  529. /// </summary>
  530. private void SaveFile()
  531. {
  532. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  533. {
  534. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  535. if (pdfDoc.WriteToLoadedPath())
  536. {
  537. return;
  538. }
  539. SaveFileDialog saveDialog = new SaveFileDialog();
  540. saveDialog.Filter = "(*.pdf)|*.pdf";
  541. saveDialog.DefaultExt = ".pdf";
  542. saveDialog.OverwritePrompt = true;
  543. if (saveDialog.ShowDialog() == true)
  544. {
  545. pdfDoc.WriteToFilePath(saveDialog.FileName);
  546. }
  547. }
  548. }
  549. #endregion
  550. #region Selection changed
  551. private void ViewComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  552. {
  553. if (isFirstLoad)
  554. {
  555. isFirstLoad = false;
  556. return;
  557. }
  558. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  559. switch (currentMode)
  560. {
  561. case "Viewer":
  562. break;
  563. case "Page Edit":
  564. ToolBarContainer.Visibility = Visibility.Collapsed;
  565. pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit;
  566. pageEditControl.PageMoved -= PageEditControl_PageMoved;
  567. CPDFPageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent;
  568. FloatPageTool.Visibility = Visibility.Visible;
  569. pdfViewControl.PDFView.ReloadDocument();
  570. LeftToolPanelButton.IsEnabled = true;
  571. SearchButton.IsEnabled = true;
  572. RightPanelButton.IsEnabled = true;
  573. ViewSettingBtn.IsEnabled = true;
  574. break;
  575. default:
  576. break;
  577. }
  578. if ((string)item.Content == "Viewer")
  579. {
  580. ToolBarContainer.Visibility = Visibility.Collapsed;
  581. PDFGrid.Child = pdfViewControl;
  582. }
  583. else if ((string)item.Content == "Page Edit")
  584. {
  585. ToolBarContainer.Visibility = Visibility.Visible;
  586. if (pageEditControl == null)
  587. {
  588. pageEditControl = new CPDFPageEditControl();
  589. }
  590. pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit;
  591. pageEditControl.PageMoved += PageEditControl_PageMoved;
  592. CPDFPageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent;
  593. pageEditControl.LoadThumbnails(pdfViewControl.PDFView);
  594. PDFGrid.Child = pageEditControl;
  595. LeftToolPanelButton.IsChecked = false;
  596. LeftToolPanelButton.IsEnabled = false;
  597. SearchButton.IsEnabled = false;
  598. RightPanelButton.IsChecked = false;
  599. RightPanelButton.IsEnabled = false;
  600. ViewSettingBtn.IsChecked = false;
  601. ViewSettingBtn.IsEnabled = false;
  602. ExpandLeftPanel(false);
  603. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  604. FloatPageTool.Visibility = Visibility.Collapsed;
  605. }
  606. currentMode = (string)item.Content;
  607. }
  608. #endregion
  609. #region Event handle
  610. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  611. {
  612. BotaSideTool.AddBOTAContent(new []{BOTATools.Thumbnail , BOTATools.Outline , BOTATools.Bookmark , BOTATools.Search});
  613. pdfAnnotationControl = new CPDFAnnotationControl();
  614. LoadDefaultDocument();
  615. }
  616. private void CPDFPageEditBarControl_PageEditEvent(object sender, string e)
  617. {
  618. pageEditControl.PageEdit(e);
  619. }
  620. private void PageEditControl_ExitPageEdit(object sender, EventArgs e)
  621. {
  622. ViewComboBox.SelectedIndex = 0;
  623. }
  624. private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
  625. {
  626. OnPropertyChanged(e.PropertyName);
  627. }
  628. private void PageEditControl_PageMoved(object sender, RoutedEventArgs e)
  629. {
  630. if (pdfViewControl == null || pdfViewControl.PDFView == null)
  631. {
  632. return;
  633. }
  634. BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
  635. }
  636. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  637. {
  638. if (e.Key == "Zoom")
  639. {
  640. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
  641. }
  642. }
  643. #endregion
  644. #region Property changed
  645. protected void OnPropertyChanged([CallerMemberName] string name = null)
  646. {
  647. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  648. }
  649. #endregion
  650. #region Shortcut
  651. private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
  652. {
  653. OpenFile();
  654. }
  655. private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
  656. {
  657. if (CanSave)
  658. {
  659. SaveFile();
  660. }
  661. }
  662. private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
  663. {
  664. SaveAsFile();
  665. }
  666. private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
  667. {
  668. if (currentMode != "Page Edit")
  669. {
  670. LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
  671. ControlLeftPanel();
  672. }
  673. }
  674. private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
  675. {
  676. if (currentMode != "Page Edit")
  677. {
  678. RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
  679. ControlRightPanel();
  680. }
  681. }
  682. private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
  683. {
  684. if (currentMode != "Page Edit")
  685. {
  686. ExpandLeftPanel(true);
  687. LeftToolPanelButton.IsChecked = true;
  688. BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
  689. }
  690. }
  691. private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
  692. {
  693. if (currentMode != "Page Edit")
  694. {
  695. ExpandLeftPanel(true);
  696. LeftToolPanelButton.IsChecked = true;
  697. BotaSideTool.SelectBotaTool(BOTATools.Outline);
  698. }
  699. }
  700. private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
  701. {
  702. if (currentMode != "Page Edit")
  703. {
  704. ExpandLeftPanel(true);
  705. LeftToolPanelButton.IsChecked = true;
  706. BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
  707. }
  708. }
  709. private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
  710. {
  711. if (currentMode != "Page Edit")
  712. {
  713. ExpandLeftPanel(true);
  714. LeftToolPanelButton.IsChecked = true;
  715. BotaSideTool.SelectBotaTool(BOTATools.Search);
  716. }
  717. }
  718. private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
  719. {
  720. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  721. pdfViewControl.PDFView?.Zoom(newZoom);
  722. }
  723. private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
  724. {
  725. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  726. pdfViewControl.PDFView?.Zoom(newZoom);
  727. }
  728. private void ShowViewSettings()
  729. {
  730. if (ViewSettingBtn != null)
  731. {
  732. if (ViewSettingBtn.IsChecked == true)
  733. {
  734. displayPanel = new CPDFDisplaySettingsControl();
  735. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  736. PropertyContainer.Child = displayPanel;
  737. PropertyContainer.Visibility = Visibility.Visible;
  738. if ((bool)RightPanelButton.IsChecked)
  739. {
  740. RightPanelButton.IsChecked = false;
  741. }
  742. }
  743. else
  744. {
  745. PropertyContainer.Child = null;
  746. PropertyContainer.Visibility = Visibility.Collapsed;
  747. }
  748. }
  749. }
  750. private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
  751. {
  752. if (currentMode != "Page Edit")
  753. {
  754. ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
  755. ShowViewSettings();
  756. }
  757. }
  758. private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
  759. {
  760. if (PopupBorder.Visibility != Visibility.Visible)
  761. {
  762. PasswordUI.Visibility = Visibility.Collapsed;
  763. FileInfoUI.Visibility = Visibility.Visible;
  764. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  765. PopupBorder.Visibility = Visibility.Visible;
  766. }
  767. else
  768. {
  769. FileInfoUI.Visibility = Visibility.Collapsed;
  770. PopupBorder.Visibility = Visibility.Collapsed;
  771. }
  772. }
  773. #endregion
  774. }
  775. }