MainWindow.xaml.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using ComPDFKit.PDFDocument;
  2. using compdfkit_tools;
  3. using compdfkit_tools.PDFControl;
  4. using compdfkit_tools.PDFControl;
  5. using compdfkit_tools.PDFControlUI;
  6. using ComPDFKitViewer.PdfViewer;
  7. using Microsoft.Win32;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace viewer_ctrl_demo
  24. {
  25. /// <summary>
  26. /// Interaction logic for MainWindow.xaml
  27. /// </summary>
  28. public partial class MainWindow : Window
  29. {
  30. public CPDFViewer pdfViewer;
  31. public MainWindow()
  32. {
  33. InitializeComponent();
  34. TitleBarControl.Loaded += TitleBarControl_Loaded;
  35. LoadDefaultDocument();
  36. }
  37. private void LoadDocument()
  38. {
  39. pdfViewer.Load();
  40. PDFGrid.Child = pdfViewer;
  41. CPDFScalingControl.InitWithPDFViewer(pdfViewer);
  42. CPDFPageTurningControl.InitWithPDFViewer(pdfViewer);
  43. CPDFBrowseModeControl.InitWithPDFViewer(pdfViewer);
  44. CPDFColorModeControl.InitWithPDFViewer(pdfViewer);
  45. UIElement currentBotaTool = GetBotaTool();
  46. if (currentBotaTool is CPDFSearchControl)
  47. {
  48. ((CPDFSearchControl)currentBotaTool).SetPDFView(pdfViewer);
  49. }
  50. if (currentBotaTool is CPDFThumbnailControl)
  51. {
  52. ((CPDFThumbnailControl)currentBotaTool).SetPDFView(pdfViewer);
  53. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  54. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  55. }
  56. }
  57. private void LoadDefaultDocument()
  58. {
  59. string defaultFilePath = "..\\..\\..\\..\\developer_guide_windows.pdf";
  60. pdfViewer = new CPDFViewer();
  61. pdfViewer.InitDocument(defaultFilePath);
  62. LoadDocument();
  63. }
  64. private void TitleBarControl_Loaded(object sender, RoutedEventArgs e)
  65. {
  66. TitleBarControl.OpenFileEvent += TitleBarControl_OpenFileEvent;
  67. }
  68. private void TitleBarControl_OpenFileEvent(object sender, CPDFViewer e)
  69. {
  70. this.pdfViewer = TitleBarControl.pdfViewer;
  71. LoadDocument();
  72. }
  73. private void LogoButton_Click(object sender, RoutedEventArgs e)
  74. {
  75. Button button = sender as Button;
  76. if (button.ContextMenu.IsOpen)
  77. {
  78. button.ContextMenu.IsOpen = false;
  79. }
  80. else
  81. {
  82. button.ContextMenu.IsOpen = true;
  83. }
  84. }
  85. /// <summary>
  86. /// 搜索工具点击处理
  87. /// </summary>
  88. private void SearchToolButton_Click(object sender, RoutedEventArgs e)
  89. {
  90. UIElement botaTool = GetBotaTool();
  91. if (botaTool == null || !(botaTool is CPDFSearchControl))
  92. {
  93. CPDFSearchControl searchControl = new CPDFSearchControl();
  94. if (pdfViewer != null && pdfViewer.Document != null)
  95. {
  96. searchControl.SetPDFView(pdfViewer);
  97. }
  98. SetBotaTool(searchControl);
  99. }
  100. ExpandTool(SearchToolButton.IsChecked == true);
  101. ClearToolState(SearchToolButton);
  102. }
  103. /// <summary>
  104. /// 缩略图列表点击处理
  105. /// </summary>
  106. private void ThumbToolButton_Click(object sender, RoutedEventArgs e)
  107. {
  108. UIElement botaTool = GetBotaTool();
  109. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  110. {
  111. CPDFThumbnailControl thumbControl = new CPDFThumbnailControl();
  112. if (pdfViewer != null && pdfViewer.Document != null)
  113. {
  114. thumbControl.SetPDFView(pdfViewer);
  115. thumbControl.LoadThumb();
  116. }
  117. SetBotaTool(thumbControl);
  118. }
  119. ExpandTool(ThumbToolButton.IsChecked == true);
  120. ClearToolState(ThumbToolButton);
  121. }
  122. /// <summary>
  123. /// 大纲列表处理
  124. /// </summary>
  125. private void OutlineToolButton_Click(object sender, RoutedEventArgs e)
  126. {
  127. UIElement botaTool = GetBotaTool();
  128. if (botaTool == null||!(botaTool is CPDFOutlineControl))
  129. {
  130. CPDFOutlineUI outlineControl = new CPDFOutlineUI();
  131. if (pdfViewer != null && pdfViewer.Document != null)
  132. {
  133. }
  134. SetBotaTool(outlineControl);
  135. }
  136. ExpandTool(OutlineToolButton.IsChecked == true);
  137. ClearToolState(OutlineToolButton);
  138. }
  139. /// <summary>
  140. /// 设置Bota工具内容
  141. /// </summary>
  142. /// <param name="newChild"></param>
  143. private void SetBotaTool(UIElement newChild)
  144. {
  145. BotaToolContainer.Child = newChild;
  146. }
  147. /// <summary>
  148. /// 获取Bota工具
  149. /// </summary>
  150. /// <returns></returns>
  151. private UIElement GetBotaTool()
  152. {
  153. return BotaToolContainer.Child;
  154. }
  155. /// <summary>
  156. /// 展开Bota工具
  157. /// </summary>
  158. /// <param name="isExpand"></param>
  159. private void ExpandTool(bool isExpand)
  160. {
  161. BotaToolContainer.Width = isExpand ? 200 : 0;
  162. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  163. }
  164. /// <summary>
  165. /// 清除左边工具栏状态
  166. /// </summary>
  167. private void ClearToolState(UIElement ignoreTool)
  168. {
  169. foreach (UIElement child in BotaSideTool.Children)
  170. {
  171. if (child != ignoreTool && child is ToggleButton buttonTool)
  172. {
  173. buttonTool.IsChecked = false;
  174. }
  175. }
  176. }
  177. private void ShowPDFInfoDialog()
  178. {
  179. CPDFInfoControl pdfInfoControl = new CPDFInfoControl();
  180. pdfInfoControl.PDFDocument = pdfViewer.Document;
  181. Window dialog = new Window
  182. {
  183. Title = "PDF Info", // 设置弹出窗口的标题
  184. Content = pdfInfoControl, // 将UserControl添加到弹出窗口中
  185. Width = 600, // 设置弹出窗口的宽度
  186. Height = 300, // 设置弹出窗口的高度
  187. ResizeMode = ResizeMode.NoResize, // 禁止用户调整弹出窗口的大小
  188. WindowStartupLocation = WindowStartupLocation.CenterScreen, // 将弹出窗口显示在屏幕中央
  189. WindowStyle = WindowStyle.ToolWindow, // 设置弹出窗口的样式
  190. Owner = Application.Current.MainWindow // 将主窗口设置为弹出窗口的拥有者
  191. };
  192. dialog.ShowDialog(); // 显示弹出窗口
  193. }
  194. private void PDFInfoButton_Click(object sender, RoutedEventArgs e)
  195. {
  196. ShowPDFInfoDialog();
  197. }
  198. }
  199. }