MainWindow.xaml.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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
  29. {
  30. private PDFViewControl passwordViewer;
  31. private PDFViewControl pdfViewControl;
  32. private CPDFSearchControl searchControl = null;
  33. private int[] zoomLevelList = { 10, 25, 50, 100, 150, 200, 300, 400, 500, 1000 };
  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. LoadDefaultDocument();
  54. }
  55. private void LoadDocument()
  56. {
  57. pdfViewControl.PDFView?.Load();
  58. pdfViewControl.PDFView?.SetShowLink(true);
  59. PDFGrid.Child = pdfViewControl;
  60. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  61. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  62. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  63. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  64. pdfViewControl.PDFView.SetFormFieldHighlight(true);
  65. PasswordUI.Closed -= PasswordUI_Closed;
  66. PasswordUI.Canceled -= PasswordUI_Canceled;
  67. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  68. PasswordUI.Closed += PasswordUI_Closed;
  69. PasswordUI.Canceled += PasswordUI_Canceled;
  70. PasswordUI.Confirmed += PasswordUI_Confirmed;
  71. CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
  72. CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
  73. UIElement currentBotaTool = GetBotaTool();
  74. if (currentBotaTool is CPDFSearchControl)
  75. {
  76. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  77. }
  78. if (currentBotaTool is CPDFThumbnailControl)
  79. {
  80. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  81. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  82. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  83. }
  84. if (currentBotaTool is CPDFBookmarkControl)
  85. {
  86. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  87. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  88. }
  89. FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
  90. ViewSettingBtn.IsChecked = false;
  91. PropertyContainer.Child = null;
  92. PropertyContainer.Visibility = Visibility.Collapsed;
  93. // ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  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. private double CheckZoomLevel(double zoom, bool IsGrowth)
  257. {
  258. double standardZoom = 100;
  259. if (zoom <= 0.01)
  260. {
  261. return 0.01;
  262. }
  263. if (zoom >= 10)
  264. {
  265. return 10;
  266. }
  267. zoom *= 100;
  268. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  269. {
  270. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  271. {
  272. standardZoom = zoomLevelList[i + 1];
  273. break;
  274. }
  275. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  276. {
  277. standardZoom = zoomLevelList[i];
  278. break;
  279. }
  280. }
  281. return standardZoom / 100;
  282. }
  283. private void LoadDefaultDocument()
  284. {
  285. string defaultFilePath = "developer_guide_windows.pdf";
  286. pdfViewControl = new PDFViewControl();
  287. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  288. LoadDocument();
  289. }
  290. /// <summary>
  291. /// 搜索工具点击处理
  292. /// </summary>
  293. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  294. {
  295. UIElement botaTool = GetBotaTool();
  296. if (botaTool == null || !(botaTool is CPDFSearchControl))
  297. {
  298. if (searchControl == null)
  299. {
  300. searchControl = new CPDFSearchControl();
  301. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  302. {
  303. searchControl.InitWithPDFViewer(pdfViewControl.PDFView);
  304. }
  305. }
  306. SetBotaTool(searchControl);
  307. }
  308. ExpandBotaTool(SearchToolButton.IsChecked == true);
  309. ClearToolState(SearchToolButton);
  310. }
  311. /// <summary>
  312. /// 缩略图列表点击处理
  313. /// </summary>
  314. private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
  315. {
  316. UIElement botaTool = GetBotaTool();
  317. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  318. {
  319. CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
  320. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  321. {
  322. thumbControl.InitWithPDFViewer(pdfViewControl.PDFView);
  323. thumbControl.LoadThumb();
  324. }
  325. SetBotaTool(thumbControl);
  326. }
  327. ExpandBotaTool(ThumbToolButton.IsChecked == true);
  328. ClearToolState(ThumbToolButton);
  329. }
  330. /// <summary>
  331. /// 书签列表点击处理
  332. /// </summary>
  333. private void BookmarkToolButtonn_Click(object sender, RoutedEventArgs e)
  334. {
  335. UIElement botaTool = GetBotaTool();
  336. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  337. {
  338. CPDFBookmarkControl bookmarkControl = new CPDFBookmarkControl();
  339. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  340. {
  341. bookmarkControl.InitWithPDFViewer(pdfViewControl.PDFView);
  342. bookmarkControl.LoadBookmark();
  343. }
  344. SetBotaTool(bookmarkControl);
  345. }
  346. ExpandBotaTool(BookmarkToolButton.IsChecked == true);
  347. ClearToolState(BookmarkToolButton);
  348. }
  349. /// <summary>
  350. /// 大纲列表处理
  351. /// </summary>
  352. private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
  353. {
  354. UIElement botaTool = GetBotaTool();
  355. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  356. {
  357. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  358. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  359. {
  360. outlineControl.InitWithPDFViewer(pdfViewControl.PDFView);
  361. }
  362. SetBotaTool(outlineControl);
  363. }
  364. ExpandBotaTool(OutlineToolButton.IsChecked == true);
  365. ClearToolState(OutlineToolButton);
  366. }
  367. /// <summary>
  368. /// 设置Bota工具内容
  369. /// </summary>
  370. /// <param name="newChild"></param>
  371. private void SetBotaTool(UIElement newChild)
  372. {
  373. BotaToolContainer.Child = newChild;
  374. }
  375. /// <summary>
  376. /// 获取Bota工具
  377. /// </summary>
  378. /// <returns></returns>
  379. private UIElement GetBotaTool()
  380. {
  381. return BotaToolContainer.Child;
  382. }
  383. /// <summary>
  384. /// 展开Bota工具
  385. /// </summary>
  386. /// <param name="isExpand"></param>
  387. private void ExpandBotaTool(bool isExpand)
  388. {
  389. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  390. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  391. }
  392. /// <summary>
  393. /// 清除左边工具栏状态
  394. /// </summary>
  395. private void ClearToolState(UIElement ignoreTool)
  396. {
  397. foreach (UIElement child in BotaSideTool.Children)
  398. {
  399. if (child != ignoreTool && child is ToggleButton buttonTool)
  400. {
  401. buttonTool.IsChecked = false;
  402. }
  403. }
  404. }
  405. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  406. {
  407. ToggleButton expandBtn = sender as ToggleButton;
  408. if (expandBtn != null)
  409. {
  410. bool isExpand = expandBtn.IsChecked == true;
  411. ExpandLeftPanel(isExpand);
  412. if (isExpand)
  413. {
  414. ThumbToolButton.IsChecked = isExpand;
  415. ThumbToolButton_Click(ThumbToolButton, new RoutedEventArgs());
  416. }
  417. }
  418. }
  419. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  420. {
  421. ExpandLeftPanel(true);
  422. SearchToolButton.IsChecked = true;
  423. ToolExpandBtn.IsChecked = true;
  424. SearchToolButton_Click(SearchToolButton, new RoutedEventArgs());
  425. }
  426. private void ExpandLeftPanel(bool isExpand)
  427. {
  428. ExpandToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  429. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  430. if (isExpand)
  431. {
  432. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  433. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  434. }
  435. else
  436. {
  437. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  438. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  439. }
  440. }
  441. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  442. {
  443. ToggleButton toggleButton = sender as ToggleButton;
  444. if (toggleButton != null)
  445. {
  446. if (toggleButton.IsChecked == true)
  447. {
  448. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  449. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  450. PropertyContainer.Child = displayPanel;
  451. PropertyContainer.Visibility = Visibility.Visible;
  452. }
  453. else
  454. {
  455. PropertyContainer.Child = null;
  456. PropertyContainer.Visibility = Visibility.Collapsed;
  457. }
  458. }
  459. }
  460. //private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  461. //{
  462. // ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem;
  463. // if (selectItem != null && selectItem.Content != null && pdfViewControl != null)
  464. // {
  465. // if (selectItem.Content.ToString() == "Actual size")
  466. // {
  467. // pdfViewControl.PDFView.ChangeFitMode(FitMode.FitSize);
  468. // }
  469. // else if (selectItem.Content.ToString() == "Suitable width")
  470. // {
  471. // pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
  472. // }
  473. // else if (selectItem.Content.ToString() == "Single page size")
  474. // {
  475. // pdfViewControl.PDFView.ChangeFitMode(FitMode.FitHeight);
  476. // }
  477. // else if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double zoomLevel))
  478. // {
  479. // pdfViewControl.PDFView.Zoom(zoomLevel / 100D);
  480. // }
  481. // ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  482. // }
  483. //}
  484. private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
  485. {
  486. if (pdfViewControl != null)
  487. {
  488. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  489. pdfViewControl.PDFView.Zoom(newZoom);
  490. //ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  491. }
  492. }
  493. private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
  494. {
  495. if (pdfViewControl != null)
  496. {
  497. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  498. pdfViewControl.PDFView.Zoom(newZoom);
  499. //ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  500. }
  501. }
  502. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  503. {
  504. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  505. }
  506. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  507. {
  508. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  509. }
  510. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  511. {
  512. PasswordUI.Visibility = Visibility.Collapsed;
  513. FileInfoUI.Visibility = Visibility.Visible;
  514. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  515. PopupBorder.Visibility = Visibility.Visible;
  516. }
  517. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  518. {
  519. PopupBorder.Visibility = Visibility.Collapsed;
  520. }
  521. private void OpenFile_Click(object sender, RoutedEventArgs e)
  522. {
  523. try
  524. {
  525. string filePath = CommonHelper.GetFilePathOrEmpty();
  526. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  527. {
  528. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  529. {
  530. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  531. if (oldFilePath.ToLower() == filePath.ToLower())
  532. {
  533. return;
  534. }
  535. }
  536. passwordViewer = new PDFViewControl();
  537. passwordViewer.PDFView.InitDocument(filePath);
  538. if (passwordViewer.PDFView.Document == null)
  539. {
  540. MessageBox.Show("Open File Failed");
  541. return;
  542. }
  543. if (passwordViewer.PDFView.Document.IsLocked)
  544. {
  545. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  546. PasswordUI.ClearPassword();
  547. PopupBorder.Visibility = Visibility.Visible;
  548. PasswordUI.Visibility = Visibility.Visible;
  549. }
  550. else
  551. {
  552. pdfViewControl = passwordViewer;
  553. LoadDocument();
  554. }
  555. }
  556. }
  557. catch (Exception ex)
  558. {
  559. }
  560. }
  561. private void SaveFile()
  562. {
  563. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  564. {
  565. try
  566. {
  567. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  568. if (pdfDoc.WriteToLoadedPath())
  569. {
  570. return;
  571. }
  572. SaveFileDialog saveDialog = new SaveFileDialog();
  573. saveDialog.Filter = "(*.pdf)|*.pdf";
  574. saveDialog.DefaultExt = ".pdf";
  575. saveDialog.OverwritePrompt = true;
  576. if (saveDialog.ShowDialog() == true)
  577. {
  578. pdfDoc.WriteToFilePath(saveDialog.FileName);
  579. }
  580. }
  581. catch (Exception ex)
  582. {
  583. }
  584. }
  585. }
  586. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  587. {
  588. SaveFile();
  589. }
  590. private void CopyImage_Click(object sender, RoutedEventArgs e)
  591. {
  592. try
  593. {
  594. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  595. if (imageDict != null && imageDict.Count > 0)
  596. {
  597. foreach (int pageIndex in imageDict.Keys)
  598. {
  599. List<Bitmap> imageList = imageDict[pageIndex];
  600. foreach (Bitmap image in imageList)
  601. {
  602. MemoryStream ms = new MemoryStream();
  603. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  604. BitmapImage imageData = new BitmapImage();
  605. imageData.BeginInit();
  606. imageData.StreamSource = ms;
  607. imageData.CacheOption = BitmapCacheOption.OnLoad;
  608. imageData.EndInit();
  609. imageData.Freeze();
  610. Clipboard.SetImage(imageData);
  611. break;
  612. }
  613. }
  614. }
  615. }
  616. catch (Exception ex)
  617. {
  618. }
  619. }
  620. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  621. {
  622. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  623. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  624. {
  625. string choosePath = folderDialog.SelectedPath;
  626. string openPath = choosePath;
  627. try
  628. {
  629. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  630. if (imageDict != null && imageDict.Count > 0)
  631. {
  632. foreach (int pageIndex in imageDict.Keys)
  633. {
  634. List<Bitmap> imageList = imageDict[pageIndex];
  635. foreach (Bitmap image in imageList)
  636. {
  637. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  638. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  639. openPath = savePath;
  640. }
  641. }
  642. }
  643. Process.Start("explorer", "/select,\"" + openPath + "\"");
  644. }
  645. catch (Exception ex)
  646. {
  647. }
  648. }
  649. }
  650. protected override void OnClosing(CancelEventArgs e)
  651. {
  652. if (pdfViewControl.PDFView.UndoManager.CanSave)
  653. {
  654. MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
  655. if (result == MessageBoxResult.Yes)
  656. {
  657. SaveFile();
  658. }
  659. else if (result == MessageBoxResult.No)
  660. {
  661. }
  662. else
  663. {
  664. e.Cancel = true;
  665. }
  666. }
  667. }
  668. }
  669. }