MainWindow.xaml.cs 22 KB

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