MainWindow.xaml.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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.Runtime.CompilerServices;
  16. using System.Windows;
  17. using System.Windows.Annotations;
  18. using System.Windows.Controls;
  19. using System.Windows.Controls.Primitives;
  20. using System.Windows.Input;
  21. using System.Windows.Media.Imaging;
  22. namespace viewer_ctrl_demo
  23. {
  24. /// <summary>
  25. /// Interaction logic for MainWindow.xaml
  26. /// </summary>
  27. public partial class MainWindow : Window
  28. {
  29. private PDFViewControl passwordViewer;
  30. private PDFViewControl pdfViewControl;
  31. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  32. public MainWindow()
  33. {
  34. InitializeComponent();
  35. Loaded += MainWindow_Loaded;
  36. DataContext = this;
  37. }
  38. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  39. {
  40. LoadDefaultDocument();
  41. BindZoomLevel();
  42. }
  43. private void BindZoomLevel()
  44. {
  45. foreach (double zoomLevel in zoomLevelList)
  46. {
  47. ComboBoxItem zoomItem = new ComboBoxItem();
  48. zoomItem.Content = zoomLevel + "%";
  49. ZoomComboBox.Items.Add(zoomItem);
  50. }
  51. }
  52. private void LoadDocument()
  53. {
  54. pdfViewControl.PDFView?.Load();
  55. pdfViewControl.PDFView?.SetShowLink(true);
  56. PDFGrid.Child = pdfViewControl;
  57. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  58. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  59. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  60. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  61. PasswordUI.Closed -= PasswordUI_Closed;
  62. PasswordUI.Canceled -= PasswordUI_Canceled;
  63. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  64. PasswordUI.Closed += PasswordUI_Closed;
  65. PasswordUI.Canceled += PasswordUI_Canceled;
  66. PasswordUI.Confirmed += PasswordUI_Confirmed;
  67. UIElement currentBotaTool = GetBotaTool();
  68. if (currentBotaTool is CPDFSearchControl)
  69. {
  70. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  71. }
  72. if (currentBotaTool is CPDFThumbnailControl)
  73. {
  74. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  75. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  76. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  77. }
  78. if (currentBotaTool is CPDFBookmarkControl)
  79. {
  80. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  81. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  82. }
  83. ViewSettingBtn.IsChecked = false;
  84. PropertyContainer.Child = null;
  85. PropertyContainer.Visibility = Visibility.Collapsed;
  86. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  87. }
  88. private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  89. {
  90. if (e != null && e.CommandType == CommandType.Context)
  91. {
  92. if (e.PressOnSelectedText)
  93. {
  94. e.Handle = true;
  95. e.PopupMenu = new ContextMenu();
  96. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  97. }
  98. else if (e.CommandTarget == TargetType.ImageSelection)
  99. {
  100. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  101. {
  102. e.Handle = true;
  103. e.PopupMenu = new ContextMenu();
  104. MenuItem imageCopyMenu = new MenuItem();
  105. imageCopyMenu = new MenuItem();
  106. imageCopyMenu.Header = "Copy Images";
  107. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  108. imageCopyMenu.CommandParameter = e;
  109. e.PopupMenu.Items.Add(imageCopyMenu);
  110. MenuItem imageExtraMenu = new MenuItem();
  111. imageExtraMenu = new MenuItem();
  112. imageExtraMenu.Header = "Extract Images";
  113. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  114. imageExtraMenu.CommandParameter = e;
  115. e.PopupMenu.Items.Add(imageExtraMenu);
  116. }
  117. }
  118. else
  119. {
  120. e.Handle = true;
  121. e.PopupMenu = new ContextMenu();
  122. MenuItem fitWidthMenu = new MenuItem();
  123. fitWidthMenu.Header = "Fit Width";
  124. fitWidthMenu.Click += (o, p) =>
  125. {
  126. if (pdfViewControl != null)
  127. {
  128. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  129. }
  130. };
  131. e.PopupMenu.Items.Add(fitWidthMenu);
  132. MenuItem fitSizeMenu = new MenuItem();
  133. fitSizeMenu.Header = "Actual Size";
  134. fitSizeMenu.Click += (o, p) =>
  135. {
  136. if (pdfViewControl != null)
  137. {
  138. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  139. }
  140. };
  141. e.PopupMenu.Items.Add(fitSizeMenu);
  142. MenuItem zoomInMenu = new MenuItem();
  143. zoomInMenu.Header = "Zoom In";
  144. zoomInMenu.Click += (o, p) =>
  145. {
  146. if (pdfViewControl != null)
  147. {
  148. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  149. pdfViewControl.PDFView?.Zoom(newZoom);
  150. }
  151. };
  152. e.PopupMenu.Items.Add(zoomInMenu);
  153. MenuItem zoomOutMenu = new MenuItem();
  154. zoomOutMenu.Header = "Zoom Out";
  155. zoomOutMenu.Click += (o, p) =>
  156. {
  157. if (pdfViewControl != null)
  158. {
  159. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  160. pdfViewControl.PDFView?.Zoom(newZoom);
  161. }
  162. };
  163. e.PopupMenu.Items.Add(zoomOutMenu);
  164. e.PopupMenu.Items.Add(new Separator());
  165. MenuItem singleView = new MenuItem();
  166. singleView.Header = "Single Page";
  167. singleView.Click += (o, p) =>
  168. {
  169. if (pdfViewControl != null)
  170. {
  171. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  172. }
  173. };
  174. e.PopupMenu.Items.Add(singleView);
  175. MenuItem singleContinuousView = new MenuItem();
  176. singleContinuousView.Header = "Single Continuous Page";
  177. singleContinuousView.Click += (o, p) =>
  178. {
  179. if (pdfViewControl != null)
  180. {
  181. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  182. }
  183. };
  184. e.PopupMenu.Items.Add(singleContinuousView);
  185. MenuItem doubleView = new MenuItem();
  186. doubleView.Header = "Double Page";
  187. doubleView.Click += (o, p) =>
  188. {
  189. if (pdfViewControl != null)
  190. {
  191. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  192. }
  193. };
  194. e.PopupMenu.Items.Add(doubleView);
  195. MenuItem doubleContinuousView = new MenuItem();
  196. doubleContinuousView.Header = "Double Continuous Page";
  197. doubleContinuousView.Click += (o, p) =>
  198. {
  199. if (pdfViewControl != null)
  200. {
  201. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  202. }
  203. };
  204. e.PopupMenu.Items.Add(doubleContinuousView);
  205. }
  206. }
  207. if (e != null && e.CommandType == CommandType.Copy)
  208. {
  209. e.DoCommand();
  210. }
  211. }
  212. private void PasswordUI_Confirmed(object sender, string e)
  213. {
  214. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  215. {
  216. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  217. if (passwordViewer.PDFView.Document.IsLocked == false)
  218. {
  219. PasswordUI.SetShowError("", Visibility.Collapsed);
  220. PasswordUI.ClearPassword();
  221. PasswordUI.Visibility = Visibility.Collapsed;
  222. PopupBorder.Visibility = Visibility.Collapsed;
  223. pdfViewControl = passwordViewer;
  224. LoadDocument();
  225. }
  226. else
  227. {
  228. PasswordUI.SetShowError("error", Visibility.Visible);
  229. }
  230. }
  231. }
  232. private void PasswordUI_Canceled(object sender, EventArgs e)
  233. {
  234. PopupBorder.Visibility = Visibility.Collapsed;
  235. PasswordUI.Visibility = Visibility.Collapsed;
  236. }
  237. private void PasswordUI_Closed(object sender, EventArgs e)
  238. {
  239. PopupBorder.Visibility = Visibility.Collapsed;
  240. PasswordUI.Visibility = Visibility.Collapsed;
  241. }
  242. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  243. {
  244. if (e.Key == "PageNum")
  245. {
  246. PageRangeText.Text = string.Format("{0}/{1}", e.Value, pdfViewControl.PDFView.Document.PageCount);
  247. }
  248. if (e.Key == "Zoom")
  249. {
  250. ZoomTextBox.Text = string.Format("{0}", (int)((double)e.Value * 100)) + "%";
  251. }
  252. }
  253. private double CheckZoomLevel(double zoom, bool IsGrowth)
  254. {
  255. double standardZoom = 100;
  256. if (zoom <= 0.01)
  257. {
  258. return 0.01;
  259. }
  260. if (zoom >= 10)
  261. {
  262. return 10;
  263. }
  264. zoom *= 100;
  265. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  266. {
  267. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  268. {
  269. standardZoom = zoomLevelList[i + 1];
  270. break;
  271. }
  272. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  273. {
  274. standardZoom = zoomLevelList[i];
  275. break;
  276. }
  277. }
  278. return standardZoom / 100;
  279. }
  280. private void LoadDefaultDocument()
  281. {
  282. string defaultFilePath = "developer_guide_windows.pdf";
  283. pdfViewControl = new PDFViewControl();
  284. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  285. LoadDocument();
  286. }
  287. /// <summary>
  288. /// 搜索工具点击处理
  289. /// </summary>
  290. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  291. {
  292. UIElement botaTool = GetBotaTool();
  293. if (botaTool == null || !(botaTool is CPDFSearchControl))
  294. {
  295. CPDFSearchControl searchControl = new CPDFSearchControl();
  296. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  297. {
  298. searchControl.InitWithPDFViewer(pdfViewControl.PDFView);
  299. }
  300. SetBotaTool(searchControl);
  301. }
  302. ExpandBotaTool(SearchToolButton.IsChecked == true);
  303. ClearToolState(SearchToolButton);
  304. }
  305. /// <summary>
  306. /// 缩略图列表点击处理
  307. /// </summary>
  308. private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
  309. {
  310. UIElement botaTool = GetBotaTool();
  311. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  312. {
  313. CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
  314. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  315. {
  316. thumbControl.InitWithPDFViewer(pdfViewControl.PDFView);
  317. thumbControl.LoadThumb();
  318. }
  319. SetBotaTool(thumbControl);
  320. }
  321. ExpandBotaTool(ThumbToolButton.IsChecked == true);
  322. ClearToolState(ThumbToolButton);
  323. }
  324. /// <summary>
  325. /// 书签列表点击处理
  326. /// </summary>
  327. private void BookmarkToolButtonn_Click(object sender, RoutedEventArgs e)
  328. {
  329. UIElement botaTool = GetBotaTool();
  330. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  331. {
  332. CPDFBookmarkControl bookmarkControl = new CPDFBookmarkControl();
  333. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  334. {
  335. bookmarkControl.InitWithPDFViewer(pdfViewControl.PDFView);
  336. bookmarkControl.LoadBookmark();
  337. }
  338. SetBotaTool(bookmarkControl);
  339. }
  340. ExpandBotaTool(BookmarkToolButton.IsChecked == true);
  341. ClearToolState(BookmarkToolButton);
  342. }
  343. /// <summary>
  344. /// 大纲列表处理
  345. /// </summary>
  346. private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
  347. {
  348. UIElement botaTool = GetBotaTool();
  349. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  350. {
  351. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  352. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  353. {
  354. outlineControl.InitWithPDFViewer(pdfViewControl.PDFView);
  355. }
  356. SetBotaTool(outlineControl);
  357. }
  358. ExpandBotaTool(OutlineToolButton.IsChecked == true);
  359. ClearToolState(OutlineToolButton);
  360. }
  361. /// <summary>
  362. /// 设置Bota工具内容
  363. /// </summary>
  364. /// <param name="newChild"></param>
  365. private void SetBotaTool(UIElement newChild)
  366. {
  367. BotaToolContainer.Child = newChild;
  368. }
  369. /// <summary>
  370. /// 获取Bota工具
  371. /// </summary>
  372. /// <returns></returns>
  373. private UIElement GetBotaTool()
  374. {
  375. return BotaToolContainer.Child;
  376. }
  377. /// <summary>
  378. /// 展开Bota工具
  379. /// </summary>
  380. /// <param name="isExpand"></param>
  381. private void ExpandBotaTool(bool isExpand)
  382. {
  383. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  384. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  385. }
  386. /// <summary>
  387. /// 清除左边工具栏状态
  388. /// </summary>
  389. private void ClearToolState(UIElement ignoreTool)
  390. {
  391. foreach (UIElement child in BotaSideTool.Children)
  392. {
  393. if (child != ignoreTool && child is ToggleButton buttonTool)
  394. {
  395. buttonTool.IsChecked = false;
  396. }
  397. }
  398. }
  399. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  400. {
  401. ToggleButton expandBtn = sender as ToggleButton;
  402. if (expandBtn != null)
  403. {
  404. bool isExpand = expandBtn.IsChecked == true;
  405. ExpandLeftPanel(isExpand);
  406. if (isExpand)
  407. {
  408. ThumbToolButton.IsChecked = isExpand;
  409. ThumbToolButton_Click(ThumbToolButton, new RoutedEventArgs());
  410. }
  411. }
  412. }
  413. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  414. {
  415. ExpandLeftPanel(true);
  416. SearchToolButton.IsChecked = true;
  417. SearchToolButton_Click(SearchToolButton, new RoutedEventArgs());
  418. }
  419. private void ExpandLeftPanel(bool isExpand)
  420. {
  421. ExpandToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  422. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  423. if (isExpand)
  424. {
  425. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  426. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  427. }
  428. else
  429. {
  430. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  431. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  432. }
  433. }
  434. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  435. {
  436. ToggleButton toggleButton = sender as ToggleButton;
  437. if (toggleButton != null)
  438. {
  439. if (toggleButton.IsChecked == true)
  440. {
  441. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  442. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  443. PropertyContainer.Child = displayPanel;
  444. PropertyContainer.Visibility = Visibility.Visible;
  445. }
  446. else
  447. {
  448. PropertyContainer.Child = null;
  449. PropertyContainer.Visibility = Visibility.Collapsed;
  450. }
  451. }
  452. }
  453. private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  454. {
  455. ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem;
  456. if (selectItem != null && selectItem.Content != null && pdfViewControl != null)
  457. {
  458. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double zoomLevel))
  459. {
  460. pdfViewControl.PDFView.Zoom(zoomLevel / 100D);
  461. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  462. }
  463. }
  464. }
  465. private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
  466. {
  467. if (pdfViewControl != null)
  468. {
  469. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  470. pdfViewControl.PDFView.Zoom(newZoom);
  471. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  472. }
  473. }
  474. private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
  475. {
  476. if (pdfViewControl != null)
  477. {
  478. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  479. pdfViewControl.PDFView.Zoom(newZoom);
  480. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  481. }
  482. }
  483. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  484. {
  485. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  486. }
  487. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  488. {
  489. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  490. }
  491. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  492. {
  493. PasswordUI.Visibility = Visibility.Collapsed;
  494. FileInfoUI.Visibility = Visibility.Visible;
  495. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  496. PopupBorder.Visibility = Visibility.Visible;
  497. }
  498. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  499. {
  500. PopupBorder.Visibility = Visibility.Collapsed;
  501. }
  502. private void OpenFile_Click(object sender, RoutedEventArgs e)
  503. {
  504. try
  505. {
  506. string filePath = CommonHelper.GetFilePathOrEmpty();
  507. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  508. {
  509. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  510. {
  511. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  512. if (oldFilePath.ToLower() == filePath.ToLower())
  513. {
  514. return;
  515. }
  516. }
  517. passwordViewer = new PDFViewControl();
  518. passwordViewer.PDFView.InitDocument(filePath);
  519. if (passwordViewer.PDFView.Document == null)
  520. {
  521. MessageBox.Show("Open File Failed");
  522. return;
  523. }
  524. if (passwordViewer.PDFView.Document.IsLocked)
  525. {
  526. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  527. PasswordUI.ClearPassword();
  528. PopupBorder.Visibility = Visibility.Visible;
  529. PasswordUI.Visibility = Visibility.Visible;
  530. }
  531. else
  532. {
  533. pdfViewControl = passwordViewer;
  534. LoadDocument();
  535. }
  536. }
  537. }
  538. catch (Exception ex)
  539. {
  540. }
  541. }
  542. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  543. {
  544. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  545. {
  546. try
  547. {
  548. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  549. if (pdfDoc.WriteToLoadedPath())
  550. {
  551. return;
  552. }
  553. SaveFileDialog saveDialog = new SaveFileDialog();
  554. saveDialog.Filter = "(*.pdf)|*.pdf";
  555. saveDialog.DefaultExt = ".pdf";
  556. saveDialog.OverwritePrompt = true;
  557. if (saveDialog.ShowDialog() == true)
  558. {
  559. pdfDoc.WriteToFilePath(saveDialog.FileName);
  560. }
  561. }
  562. catch (Exception ex)
  563. {
  564. }
  565. }
  566. }
  567. private void CopyImage_Click(object sender, RoutedEventArgs e)
  568. {
  569. try
  570. {
  571. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  572. if (imageDict != null && imageDict.Count > 0)
  573. {
  574. foreach (int pageIndex in imageDict.Keys)
  575. {
  576. List<Bitmap> imageList = imageDict[pageIndex];
  577. foreach (Bitmap image in imageList)
  578. {
  579. MemoryStream ms = new MemoryStream();
  580. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  581. BitmapImage imageData = new BitmapImage();
  582. imageData.BeginInit();
  583. imageData.StreamSource = ms;
  584. imageData.CacheOption = BitmapCacheOption.OnLoad;
  585. imageData.EndInit();
  586. imageData.Freeze();
  587. Clipboard.SetImage(imageData);
  588. break;
  589. }
  590. }
  591. }
  592. }
  593. catch (Exception ex)
  594. {
  595. }
  596. }
  597. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  598. {
  599. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  600. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  601. {
  602. string choosePath = folderDialog.SelectedPath;
  603. string openPath = choosePath;
  604. try
  605. {
  606. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  607. if (imageDict != null && imageDict.Count > 0)
  608. {
  609. foreach (int pageIndex in imageDict.Keys)
  610. {
  611. List<Bitmap> imageList = imageDict[pageIndex];
  612. foreach (Bitmap image in imageList)
  613. {
  614. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  615. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  616. openPath = savePath;
  617. }
  618. }
  619. }
  620. Process.Start("explorer", "/select,\"" + openPath + "\"");
  621. }
  622. catch (Exception ex)
  623. {
  624. }
  625. }
  626. }
  627. }
  628. }