MainWindow.xaml.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using compdfkit_tools.PDFControl;
  2. using compdfkit_tools.PDFControlUI;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Controls.Primitives;
  7. namespace viewer_ctrl_demo
  8. {
  9. /// <summary>
  10. /// Interaction logic for MainWindow.xaml
  11. /// </summary>
  12. public partial class MainWindow : Window
  13. {
  14. private CPDFViewer pdfViewer;
  15. private double[] zoomLevel = { 1.00f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  16. public MainWindow()
  17. {
  18. InitializeComponent();
  19. TitleBarControl.Loaded += TitleBarControl_Loaded;
  20. LoadDefaultDocument();
  21. }
  22. private void LoadDocument()
  23. {
  24. pdfViewer.Load();
  25. PDFGrid.Child = pdfViewer;
  26. pdfViewer.MouseWheelZoomHandler -= PdfViewer_MouseWheelZoomHandler;
  27. pdfViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  28. CPDFScalingControl.InitWithPDFViewer(pdfViewer);
  29. CPDFPageTurningControl.InitWithPDFViewer(pdfViewer);
  30. CPDFViewModeControl.InitWithPDFViewer(pdfViewer);
  31. CPDFDrawModeControl.InitWithPDFViewer(pdfViewer);
  32. UIElement currentBotaTool = GetBotaTool();
  33. if (currentBotaTool is CPDFSearchControl)
  34. {
  35. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  36. }
  37. if (currentBotaTool is CPDFThumbnailControl)
  38. {
  39. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  40. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  41. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  42. }
  43. if (currentBotaTool is CPDFBookmarkControl)
  44. {
  45. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  46. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  47. }
  48. }
  49. private void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  50. {
  51. double newZoom = CheckZoomLevel(pdfViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  52. pdfViewer.Zoom(newZoom);
  53. }
  54. private double CheckZoomLevel(double zoom, bool IsGrowth)
  55. {
  56. double standardZoom = 100;
  57. if (zoom <= 0.01)
  58. {
  59. return 0.01;
  60. }
  61. if (zoom >= 10)
  62. {
  63. return 10;
  64. }
  65. zoom *= 100;
  66. for (int i = 0; i < zoomLevel.Length - 1; i++)
  67. {
  68. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  69. {
  70. standardZoom = zoomLevel[i + 1];
  71. break;
  72. }
  73. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  74. {
  75. standardZoom = zoomLevel[i];
  76. break;
  77. }
  78. }
  79. return standardZoom / 100;
  80. }
  81. private void LoadDefaultDocument()
  82. {
  83. string defaultFilePath = "developer_guide_windows.pdf";
  84. pdfViewer = new CPDFViewer();
  85. pdfViewer.InitDocument(defaultFilePath);
  86. LoadDocument();
  87. }
  88. private void TitleBarControl_Loaded(object sender, RoutedEventArgs e)
  89. {
  90. TitleBarControl.OpenFileEvent += TitleBarControl_OpenFileEvent;
  91. }
  92. private void TitleBarControl_OpenFileEvent(object sender, string filePath)
  93. {
  94. pdfViewer?.CloseDocument();
  95. pdfViewer = new CPDFViewer();
  96. pdfViewer.InitDocument(filePath);
  97. LoadDocument();
  98. }
  99. /// <summary>
  100. /// 搜索工具点击处理
  101. /// </summary>
  102. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  103. {
  104. UIElement botaTool = GetBotaTool();
  105. if (botaTool == null || !(botaTool is CPDFSearchControl))
  106. {
  107. CPDFSearchControl searchControl = new CPDFSearchControl();
  108. if (pdfViewer != null && pdfViewer.Document != null)
  109. {
  110. searchControl.InitWithPDFViewer(pdfViewer);
  111. }
  112. SetBotaTool(searchControl);
  113. }
  114. ExpandTool(SearchToolButton.IsChecked == true);
  115. ClearToolState(SearchToolButton);
  116. }
  117. /// <summary>
  118. /// 缩略图列表点击处理
  119. /// </summary>
  120. private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
  121. {
  122. UIElement botaTool = GetBotaTool();
  123. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  124. {
  125. CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
  126. if (pdfViewer != null && pdfViewer.Document != null)
  127. {
  128. thumbControl.InitWithPDFViewer(pdfViewer);
  129. thumbControl.LoadThumb();
  130. }
  131. SetBotaTool(thumbControl);
  132. }
  133. ExpandTool(ThumbToolButton.IsChecked == true);
  134. ClearToolState(ThumbToolButton);
  135. }
  136. /// <summary>
  137. /// 书签列表点击处理
  138. /// </summary>
  139. private void BookmarkToolButtonn_Click(object sender, RoutedEventArgs e)
  140. {
  141. UIElement botaTool = GetBotaTool();
  142. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  143. {
  144. CPDFBookmarkControl bookmarkControl = new CPDFBookmarkControl();
  145. if (pdfViewer != null && pdfViewer.Document != null)
  146. {
  147. bookmarkControl.InitWithPDFViewer(pdfViewer);
  148. bookmarkControl.LoadBookmark();
  149. }
  150. SetBotaTool(bookmarkControl);
  151. }
  152. ExpandTool(BookmarkToolButton.IsChecked == true);
  153. ClearToolState(BookmarkToolButton);
  154. }
  155. /// <summary>
  156. /// 大纲列表处理
  157. /// </summary>
  158. private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
  159. {
  160. UIElement botaTool = GetBotaTool();
  161. if (botaTool == null||!(botaTool is CPDFOutlineControl))
  162. {
  163. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  164. if (pdfViewer != null && pdfViewer.Document != null)
  165. {
  166. outlineControl.InitWithPDFViewer(pdfViewer);
  167. }
  168. SetBotaTool(outlineControl);
  169. }
  170. ExpandTool(OutlineToolButton.IsChecked == true);
  171. ClearToolState(OutlineToolButton);
  172. }
  173. /// <summary>
  174. /// 设置Bota工具内容
  175. /// </summary>
  176. /// <param name="newChild"></param>
  177. private void SetBotaTool(UIElement newChild)
  178. {
  179. BotaToolContainer.Child = newChild;
  180. }
  181. /// <summary>
  182. /// 获取Bota工具
  183. /// </summary>
  184. /// <returns></returns>
  185. private UIElement GetBotaTool()
  186. {
  187. return BotaToolContainer.Child;
  188. }
  189. /// <summary>
  190. /// 展开Bota工具
  191. /// </summary>
  192. /// <param name="isExpand"></param>
  193. private void ExpandTool(bool isExpand)
  194. {
  195. if(isExpand)
  196. {
  197. BodyGrid.ColumnDefinitions[1].Width = new GridLength(300);
  198. }
  199. else
  200. {
  201. BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
  202. }
  203. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  204. Splitter.Visibility=isExpand ? Visibility.Visible : Visibility.Collapsed;
  205. }
  206. /// <summary>
  207. /// 清除左边工具栏状态
  208. /// </summary>
  209. private void ClearToolState(UIElement ignoreTool)
  210. {
  211. foreach (UIElement child in BotaSideTool.Children)
  212. {
  213. if (child != ignoreTool && child is ToggleButton buttonTool)
  214. {
  215. buttonTool.IsChecked = false;
  216. }
  217. }
  218. }
  219. private void BotaButton_Click(object sender, RoutedEventArgs e)
  220. {
  221. UIElement botaTool = GetBotaTool();
  222. if (botaTool == null)
  223. {
  224. CPDFBOTABarControl pdfBOTABarControl = new CPDFBOTABarControl(BOTATools.Bookmark|BOTATools.Outline|BOTATools.Thumbnail|BOTATools.Search);
  225. if (pdfViewer != null && pdfViewer.Document != null)
  226. {
  227. pdfBOTABarControl.InitWithPDFViewer(pdfViewer);
  228. }
  229. SetBotaTool(pdfBOTABarControl);
  230. }
  231. ExpandTool(BotaButton.IsChecked == true);
  232. ClearToolState(BotaButton);
  233. }
  234. private void ShowPDFInfoDialog()
  235. {
  236. CPDFInfoControl pdfInfoControl = new CPDFInfoControl();
  237. pdfInfoControl.InitWithPDFViewer(pdfViewer);
  238. Window dialog = new Window
  239. {
  240. Title = "PDF Info", // 设置弹出窗口的标题
  241. Content = pdfInfoControl, // 将UserControl添加到弹出窗口中
  242. Width = 600, // 设置弹出窗口的宽度
  243. Height = 300, // 设置弹出窗口的高度
  244. ResizeMode = ResizeMode.NoResize, // 禁止用户调整弹出窗口的大小
  245. WindowStartupLocation = WindowStartupLocation.CenterScreen, // 将弹出窗口显示在屏幕中央
  246. WindowStyle = WindowStyle.ToolWindow, // 设置弹出窗口的样式
  247. Owner = Application.Current.MainWindow // 将主窗口设置为弹出窗口的拥有者
  248. };
  249. dialog.ShowDialog(); // 显示弹出窗口
  250. }
  251. private void PDFInfoButton_Click(object sender, RoutedEventArgs e)
  252. {
  253. ShowPDFInfoDialog();
  254. }
  255. }
  256. }