MainWindow.xaml.cs 22 KB

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