MainWindow.xaml.cs 21 KB

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