MainWindow.xaml.cs 22 KB

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