MainWindow.xaml.cs 37 KB

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