MainWindow.xaml.cs 37 KB

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