MainWindow.xaml.cs 17 KB

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