MainWindow.xaml.cs 22 KB

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