MainWindow.xaml.cs 22 KB

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