MainWindow.xaml.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. using ComPDFKit.PDFDocument;
  2. using compdfkit_tools.Data;
  3. using compdfkit_tools.Helper;
  4. using compdfkit_tools.PDFControl;
  5. using compdfkit_tools.PDFControlUI;
  6. using ComPDFKitViewer;
  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.Animation;
  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 CPDFAnnotationControl pdfAnnotationControl = null;
  33. private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  34. public MainWindow()
  35. {
  36. InitializeComponent();
  37. DataContext = this;
  38. }
  39. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  40. {
  41. pdfAnnotationControl = new CPDFAnnotationControl();
  42. LoadDefaultDocument();
  43. BindZoomLevel();
  44. }
  45. private void BindZoomLevel()
  46. {
  47. foreach (double zoomLevel in zoomLevelList)
  48. {
  49. ComboBoxItem zoomItem = new ComboBoxItem();
  50. zoomItem.Content = zoomLevel + "%";
  51. ZoomComboBox.Items.Add(zoomItem);
  52. }
  53. }
  54. private void LoadDocument()
  55. {
  56. pdfViewControl.PDFView?.Load();
  57. pdfViewControl.PDFView?.SetShowLink(true);
  58. PDFGrid.Child = pdfViewControl;
  59. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
  60. pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
  61. pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
  62. pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
  63. PasswordUI.Closed -= PasswordUI_Closed;
  64. PasswordUI.Canceled -= PasswordUI_Canceled;
  65. PasswordUI.Confirmed -= PasswordUI_Confirmed;
  66. PasswordUI.Closed += PasswordUI_Closed;
  67. PasswordUI.Canceled += PasswordUI_Canceled;
  68. PasswordUI.Confirmed += PasswordUI_Confirmed;
  69. UIElement currentBotaTool = GetBotaTool();
  70. if (currentBotaTool is CPDFSearchControl)
  71. {
  72. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  73. }
  74. if (currentBotaTool is CPDFThumbnailControl)
  75. {
  76. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  77. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  78. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  79. }
  80. if (currentBotaTool is CPDFBookmarkControl)
  81. {
  82. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewControl.PDFView);
  83. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  84. }
  85. ViewSettingBtn.IsChecked = false;
  86. PropertyContainer.Child = null;
  87. PropertyContainer.Visibility = Visibility.Collapsed;
  88. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  89. InitialPDFViewControl(pdfViewControl);
  90. }
  91. private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.AnnotCommandArgs e)
  92. {
  93. switch (e.CommandType)
  94. {
  95. case CommandType.Context:
  96. e.Handle = true;
  97. if (e.CommandTarget == TargetType.Annot)
  98. {
  99. e.Handle = true;
  100. e.PopupMenu = new ContextMenu();
  101. if (e.PressOnLink)
  102. {
  103. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  104. MenuItem propertyMenu = new MenuItem();
  105. propertyMenu = new MenuItem();
  106. propertyMenu.Header = "Edit";
  107. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
  108. propertyMenu.CommandParameter = e;
  109. e.PopupMenu.Items.Add(propertyMenu);
  110. }
  111. else if (e.PressOnAnnot)
  112. {
  113. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  114. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  115. e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
  116. }
  117. else if (e.PressOnMedia || e.PressOnSound)
  118. {
  119. e.Handle = true;
  120. e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
  121. e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
  122. }
  123. else if (e.PressOnSelectedText)
  124. {
  125. e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
  126. }
  127. else
  128. {
  129. e.Handle = true;
  130. e.PopupMenu = new ContextMenu();
  131. e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
  132. e.PopupMenu.Items.Add(new Separator());
  133. MenuItem fitWidthMenu = new MenuItem();
  134. fitWidthMenu.Header = "Fit Width";
  135. fitWidthMenu.Click += (o, p) =>
  136. {
  137. if (pdfViewControl != null)
  138. {
  139. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
  140. }
  141. };
  142. e.PopupMenu.Items.Add(fitWidthMenu);
  143. MenuItem fitSizeMenu = new MenuItem();
  144. fitSizeMenu.Header = "Actual Size";
  145. fitSizeMenu.Click += (o, p) =>
  146. {
  147. if (pdfViewControl != null)
  148. {
  149. pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
  150. }
  151. };
  152. e.PopupMenu.Items.Add(fitSizeMenu);
  153. MenuItem zoomInMenu = new MenuItem();
  154. zoomInMenu.Header = "Zoom In";
  155. zoomInMenu.Click += (o, p) =>
  156. {
  157. if (pdfViewControl != null)
  158. {
  159. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  160. pdfViewControl.PDFView?.Zoom(newZoom);
  161. }
  162. };
  163. e.PopupMenu.Items.Add(zoomInMenu);
  164. MenuItem zoomOutMenu = new MenuItem();
  165. zoomOutMenu.Header = "Zoom Out";
  166. zoomOutMenu.Click += (o, p) =>
  167. {
  168. if (pdfViewControl != null)
  169. {
  170. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  171. pdfViewControl.PDFView?.Zoom(newZoom);
  172. }
  173. };
  174. e.PopupMenu.Items.Add(zoomOutMenu);
  175. e.PopupMenu.Items.Add(new Separator());
  176. MenuItem singleView = new MenuItem();
  177. singleView.Header = "Single Page";
  178. singleView.Click += (o, p) =>
  179. {
  180. if (pdfViewControl != null)
  181. {
  182. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
  183. }
  184. };
  185. e.PopupMenu.Items.Add(singleView);
  186. MenuItem singleContinuousView = new MenuItem();
  187. singleContinuousView.Header = "Single Continuous Page";
  188. singleContinuousView.Click += (o, p) =>
  189. {
  190. if (pdfViewControl != null)
  191. {
  192. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
  193. }
  194. };
  195. e.PopupMenu.Items.Add(singleContinuousView);
  196. MenuItem doubleView = new MenuItem();
  197. doubleView.Header = "Double Page";
  198. doubleView.Click += (o, p) =>
  199. {
  200. if (pdfViewControl != null)
  201. {
  202. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
  203. }
  204. };
  205. e.PopupMenu.Items.Add(doubleView);
  206. MenuItem doubleContinuousView = new MenuItem();
  207. doubleContinuousView.Header = "Double Continuous Page";
  208. doubleContinuousView.Click += (o, p) =>
  209. {
  210. if (pdfViewControl != null)
  211. {
  212. pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
  213. }
  214. };
  215. e.PopupMenu.Items.Add(doubleContinuousView);
  216. }
  217. }
  218. else if (e.CommandTarget == TargetType.ImageSelection)
  219. {
  220. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
  221. {
  222. e.Handle = true;
  223. e.PopupMenu = new ContextMenu();
  224. MenuItem imageCopyMenu = new MenuItem();
  225. imageCopyMenu = new MenuItem();
  226. imageCopyMenu.Header = "Copy Images";
  227. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
  228. imageCopyMenu.CommandParameter = e;
  229. e.PopupMenu.Items.Add(imageCopyMenu);
  230. MenuItem imageExtraMenu = new MenuItem();
  231. imageExtraMenu = new MenuItem();
  232. imageExtraMenu.Header = "Extract Images";
  233. WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
  234. imageExtraMenu.CommandParameter = e;
  235. e.PopupMenu.Items.Add(imageExtraMenu);
  236. }
  237. }
  238. break;
  239. case CommandType.Copy:
  240. e.DoCommand();
  241. break;
  242. case CommandType.Cut:
  243. case CommandType.Paste:
  244. case CommandType.Delete:
  245. e.DoCommand();
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251. private void CopyImage_Click(object sender, RoutedEventArgs e)
  252. {
  253. try
  254. {
  255. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  256. if (imageDict != null && imageDict.Count > 0)
  257. {
  258. foreach (int pageIndex in imageDict.Keys)
  259. {
  260. List<Bitmap> imageList = imageDict[pageIndex];
  261. foreach (Bitmap image in imageList)
  262. {
  263. MemoryStream ms = new MemoryStream();
  264. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  265. BitmapImage imageData = new BitmapImage();
  266. imageData.BeginInit();
  267. imageData.StreamSource = ms;
  268. imageData.CacheOption = BitmapCacheOption.OnLoad;
  269. imageData.EndInit();
  270. imageData.Freeze();
  271. Clipboard.SetImage(imageData);
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. catch (Exception ex)
  278. {
  279. }
  280. }
  281. private void ExtraImage_Click(object sender, RoutedEventArgs e)
  282. {
  283. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  284. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  285. {
  286. string choosePath = folderDialog.SelectedPath;
  287. string openPath = choosePath;
  288. try
  289. {
  290. Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
  291. if (imageDict != null && imageDict.Count > 0)
  292. {
  293. foreach (int pageIndex in imageDict.Keys)
  294. {
  295. List<Bitmap> imageList = imageDict[pageIndex];
  296. foreach (Bitmap image in imageList)
  297. {
  298. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  299. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  300. openPath = savePath;
  301. }
  302. }
  303. }
  304. Process.Start("explorer", "/select,\"" + openPath + "\"");
  305. }
  306. catch (Exception ex)
  307. {
  308. }
  309. }
  310. }
  311. private void PasswordUI_Confirmed(object sender, string e)
  312. {
  313. if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
  314. {
  315. passwordViewer.PDFView.Document.UnlockWithPassword(e);
  316. if (passwordViewer.PDFView.Document.IsLocked == false)
  317. {
  318. PasswordUI.SetShowError("", Visibility.Collapsed);
  319. PasswordUI.ClearPassword();
  320. PasswordUI.Visibility = Visibility.Collapsed;
  321. PopupBorder.Visibility = Visibility.Collapsed;
  322. pdfViewControl = passwordViewer;
  323. LoadDocument();
  324. }
  325. else
  326. {
  327. PasswordUI.SetShowError("error", Visibility.Visible);
  328. }
  329. }
  330. }
  331. private void PasswordUI_Canceled(object sender, EventArgs e)
  332. {
  333. PopupBorder.Visibility = Visibility.Collapsed;
  334. PasswordUI.Visibility = Visibility.Collapsed;
  335. }
  336. private void PasswordUI_Closed(object sender, EventArgs e)
  337. {
  338. PopupBorder.Visibility = Visibility.Collapsed;
  339. PasswordUI.Visibility = Visibility.Collapsed;
  340. }
  341. private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  342. {
  343. if (e.Key == "PageNum")
  344. {
  345. PageRangeText.Text = string.Format("{0}/{1}", e.Value, pdfViewControl.PDFView.Document.PageCount);
  346. }
  347. if (e.Key == "Zoom")
  348. {
  349. ZoomTextBox.Text = string.Format("{0}", (int)((double)e.Value * 100)) + "%";
  350. }
  351. }
  352. private double CheckZoomLevel(double zoom, bool IsGrowth)
  353. {
  354. double standardZoom = 100;
  355. if (zoom <= 0.01)
  356. {
  357. return 0.01;
  358. }
  359. if (zoom >= 10)
  360. {
  361. return 10;
  362. }
  363. zoom *= 100;
  364. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  365. {
  366. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  367. {
  368. standardZoom = zoomLevelList[i + 1];
  369. break;
  370. }
  371. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  372. {
  373. standardZoom = zoomLevelList[i];
  374. break;
  375. }
  376. }
  377. return standardZoom / 100;
  378. }
  379. private void LoadDefaultDocument()
  380. {
  381. string defaultFilePath = "developer_guide_windows.pdf";
  382. pdfViewControl = new PDFViewControl();
  383. pdfViewControl.PDFView.InitDocument(defaultFilePath);
  384. LoadDocument();
  385. }
  386. /// <summary>
  387. /// 搜索工具点击处理
  388. /// </summary>
  389. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  390. {
  391. UIElement botaTool = GetBotaTool();
  392. if (botaTool == null || !(botaTool is CPDFSearchControl))
  393. {
  394. CPDFSearchControl searchControl = new CPDFSearchControl();
  395. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  396. {
  397. searchControl.InitWithPDFViewer(pdfViewControl.PDFView);
  398. }
  399. SetBotaTool(searchControl);
  400. }
  401. ExpandBotaTool(SearchToolButton.IsChecked == true);
  402. ClearToolState(SearchToolButton);
  403. }
  404. /// <summary>
  405. /// 缩略图列表点击处理
  406. /// </summary>
  407. private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
  408. {
  409. UIElement botaTool = GetBotaTool();
  410. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  411. {
  412. CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
  413. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  414. {
  415. thumbControl.InitWithPDFViewer(pdfViewControl.PDFView);
  416. thumbControl.LoadThumb();
  417. }
  418. SetBotaTool(thumbControl);
  419. }
  420. ExpandBotaTool(ThumbToolButton.IsChecked == true);
  421. ClearToolState(ThumbToolButton);
  422. }
  423. /// <summary>
  424. /// 书签列表点击处理
  425. /// </summary>
  426. private void BookmarkToolButton_Click(object sender, RoutedEventArgs e)
  427. {
  428. UIElement botaTool = GetBotaTool();
  429. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  430. {
  431. CPDFBookmarkControl bookmarkControl = new CPDFBookmarkControl();
  432. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  433. {
  434. bookmarkControl.InitWithPDFViewer(pdfViewControl.PDFView);
  435. bookmarkControl.LoadBookmark();
  436. }
  437. SetBotaTool(bookmarkControl);
  438. }
  439. ExpandBotaTool(BookmarkToolButton.IsChecked == true);
  440. ClearToolState(BookmarkToolButton);
  441. }
  442. /// <summary>
  443. /// 大纲列表处理
  444. /// </summary>
  445. private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
  446. {
  447. UIElement botaTool = GetBotaTool();
  448. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  449. {
  450. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  451. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  452. {
  453. outlineControl.InitWithPDFViewer(pdfViewControl.PDFView);
  454. }
  455. SetBotaTool(outlineControl);
  456. }
  457. ExpandBotaTool(OutlineToolButton.IsChecked == true);
  458. ClearToolState(OutlineToolButton);
  459. }
  460. /// <summary>
  461. /// 设置Bota工具内容
  462. /// </summary>
  463. /// <param name="newChild"></param>
  464. private void SetBotaTool(UIElement newChild)
  465. {
  466. BotaToolContainer.Child = newChild;
  467. }
  468. /// <summary>
  469. /// 获取Bota工具
  470. /// </summary>
  471. /// <returns></returns>
  472. private UIElement GetBotaTool()
  473. {
  474. return BotaToolContainer.Child;
  475. }
  476. /// <summary>
  477. /// 展开Bota工具
  478. /// </summary>
  479. /// <param name="isExpand"></param>
  480. private void ExpandBotaTool(bool isExpand)
  481. {
  482. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  483. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  484. }
  485. /// <summary>
  486. /// 清除左边工具栏状态
  487. /// </summary>
  488. private void ClearToolState(UIElement ignoreTool)
  489. {
  490. foreach (UIElement child in BotaSideTool.Children)
  491. {
  492. if (child != ignoreTool && child is ToggleButton buttonTool)
  493. {
  494. buttonTool.IsChecked = false;
  495. }
  496. }
  497. }
  498. private void ToolExpand_Click(object sender, RoutedEventArgs e)
  499. {
  500. ToggleButton expandBtn = sender as ToggleButton;
  501. if (expandBtn != null)
  502. {
  503. bool isExpand = expandBtn.IsChecked == true;
  504. ExpandLeftPanel(isExpand);
  505. if (isExpand)
  506. {
  507. ThumbToolButton.IsChecked = isExpand;
  508. ThumbToolButton_Click(ThumbToolButton, new RoutedEventArgs());
  509. }
  510. }
  511. }
  512. private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
  513. {
  514. ExpandLeftPanel(true);
  515. SearchToolButton.IsChecked = true;
  516. SearchToolButton_Click(SearchToolButton, new RoutedEventArgs());
  517. }
  518. private void ExpandLeftPanel(bool isExpand)
  519. {
  520. ExpandToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  521. Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  522. if (isExpand)
  523. {
  524. BodyGrid.ColumnDefinitions[0].Width = new GridLength(260);
  525. BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
  526. }
  527. else
  528. {
  529. BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
  530. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  531. }
  532. }
  533. private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
  534. {
  535. ToggleButton toggleButton = sender as ToggleButton;
  536. if (toggleButton != null)
  537. {
  538. if (toggleButton.IsChecked == true)
  539. {
  540. CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
  541. displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
  542. PropertyContainer.Child = displayPanel;
  543. PropertyContainer.Visibility = Visibility.Visible;
  544. if ((bool)AnnotationBarBtn.IsChecked)
  545. {
  546. AnnotationBarBtn.IsChecked = false;
  547. }
  548. }
  549. else
  550. {
  551. PropertyContainer.Child = null;
  552. PropertyContainer.Visibility = Visibility.Collapsed;
  553. }
  554. }
  555. }
  556. private void ZoomComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  557. {
  558. ComboBoxItem selectItem = ZoomComboBox.SelectedItem as ComboBoxItem;
  559. if (selectItem != null && selectItem.Content != null && pdfViewControl != null)
  560. {
  561. if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double zoomLevel))
  562. {
  563. pdfViewControl.PDFView.Zoom(zoomLevel / 100D);
  564. ZoomTextBox.Text = string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)) + "%";
  565. }
  566. }
  567. }
  568. private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
  569. {
  570. if (pdfViewControl != null)
  571. {
  572. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
  573. pdfViewControl.PDFView.Zoom(newZoom);
  574. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  575. }
  576. }
  577. private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
  578. {
  579. if (pdfViewControl != null)
  580. {
  581. double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
  582. pdfViewControl.PDFView.Zoom(newZoom);
  583. ZoomTextBox.Text = string.Format("{0}", (int)(newZoom * 100)) + "%";
  584. }
  585. }
  586. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  587. {
  588. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1);
  589. }
  590. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  591. {
  592. pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1);
  593. }
  594. private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
  595. {
  596. PasswordUI.Visibility = Visibility.Collapsed;
  597. FileInfoUI.Visibility = Visibility.Visible;
  598. FileInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
  599. PopupBorder.Visibility = Visibility.Visible;
  600. }
  601. private void FileInfoCloseBtn_Click(object sender, RoutedEventArgs e)
  602. {
  603. PopupBorder.Visibility = Visibility.Collapsed;
  604. }
  605. private void OpenFile_Click(object sender, RoutedEventArgs e)
  606. {
  607. try
  608. {
  609. string filePath = CommonHelper.GetFilePathOrEmpty();
  610. if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
  611. {
  612. if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  613. {
  614. string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
  615. if (oldFilePath.ToLower() == filePath.ToLower())
  616. {
  617. return;
  618. }
  619. }
  620. passwordViewer = new PDFViewControl();
  621. passwordViewer.PDFView.InitDocument(filePath);
  622. if (passwordViewer.PDFView.Document == null)
  623. {
  624. MessageBox.Show("Open File Failed");
  625. return;
  626. }
  627. if (passwordViewer.PDFView.Document.IsLocked)
  628. {
  629. PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
  630. PasswordUI.ClearPassword();
  631. PopupBorder.Visibility = Visibility.Visible;
  632. PasswordUI.Visibility = Visibility.Visible;
  633. }
  634. else
  635. {
  636. pdfViewControl = passwordViewer;
  637. LoadDocument();
  638. }
  639. }
  640. }
  641. catch (Exception ex)
  642. {
  643. }
  644. }
  645. private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
  646. {
  647. if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
  648. {
  649. try
  650. {
  651. CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
  652. if (pdfDoc.WriteToLoadedPath())
  653. {
  654. return;
  655. }
  656. SaveFileDialog saveDialog = new SaveFileDialog();
  657. saveDialog.Filter = "(*.pdf)|*.pdf";
  658. saveDialog.DefaultExt = ".pdf";
  659. saveDialog.OverwritePrompt = true;
  660. if (saveDialog.ShowDialog() == true)
  661. {
  662. pdfDoc.WriteToFilePath(saveDialog.FileName);
  663. }
  664. }
  665. catch (Exception ex)
  666. {
  667. }
  668. }
  669. }
  670. #region Annotation
  671. public void InitialPDFViewControl(PDFViewControl newPDFViewer)
  672. {
  673. pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
  674. pdfAnnotationControl.AnnotationCancel();
  675. AnnotationBarControl.ClearAllToolState();
  676. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  677. pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
  678. }
  679. private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
  680. {
  681. AnnotationBarControl.ClearAllToolState();
  682. }
  683. #endregion
  684. private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
  685. {
  686. CPDFAnnotationType[] annotationProperties = { CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout, CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText, CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square, CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio };
  687. AnnotationBarControl.InitAnnotationBar(annotationProperties);
  688. AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
  689. AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
  690. AnnotationBarControl.UndoRedoEvent += AnnotationBarControl_UndoRedoEvent;
  691. }
  692. private void AnnotationBarControl_UndoRedoEvent(object sender, string e)
  693. {
  694. if (pdfViewControl != null && pdfViewControl.PDFView != null)
  695. {
  696. if (e == "Undo")
  697. {
  698. pdfViewControl.PDFView.UndoManager?.Undo();
  699. }
  700. else
  701. {
  702. pdfViewControl.PDFView.UndoManager?.Redo();
  703. }
  704. }
  705. }
  706. private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
  707. {
  708. AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
  709. AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
  710. }
  711. private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
  712. {
  713. pdfAnnotationControl.AnnotationCancel();
  714. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  715. AnnotationBarBtn.IsChecked = false;
  716. ViewSettingBtn.IsChecked = false;
  717. }
  718. private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
  719. {
  720. pdfAnnotationControl.LoadAnnotationPanel(e);
  721. if (e != CPDFAnnotationType.Audio)
  722. {
  723. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  724. AnnotationBarBtn.IsChecked = true;
  725. }
  726. }
  727. private void AnnotationBarControl_Click(object sender, RoutedEventArgs e)
  728. {
  729. ToggleButton toggleButton = sender as ToggleButton;
  730. if (toggleButton != null)
  731. {
  732. if (toggleButton.IsChecked == true)
  733. {
  734. if (pdfAnnotationControl != null)
  735. {
  736. ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
  737. if ((bool)ViewSettingBtn.IsChecked)
  738. {
  739. ViewSettingBtn.IsChecked = false;
  740. }
  741. }
  742. }
  743. else
  744. {
  745. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  746. }
  747. }
  748. }
  749. public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible)
  750. {
  751. PropertyContainer.Width = 260;
  752. PropertyContainer.Child = properytPanel;
  753. PropertyContainer.Visibility = visible;
  754. }
  755. private void EditLink_Click(object sender, RoutedEventArgs e)
  756. {
  757. PropertyContainer.Visibility = Visibility.Visible;
  758. }
  759. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  760. {
  761. var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
  762. if ((string)item.Content == "Viewer")
  763. {
  764. AnnotationBarControl.ClearAllToolState();
  765. AnnotationBarControl.Visibility = Visibility.Collapsed;
  766. ExpandRightPropertyPanel(null, Visibility.Collapsed);
  767. pdfAnnotationControl.AnnotationCancel();
  768. AnnotationBarBtn.IsChecked = false;
  769. }
  770. else if ((string)item.Content == "Annotation")
  771. {
  772. AnnotationBarControl.Visibility = Visibility.Visible;
  773. }
  774. }
  775. }
  776. }