CPDFBOTABarControl.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.PDFControlUI;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace Compdfkit_Tools.PDFControl
  20. {
  21. public enum BOTATools
  22. {
  23. Bookmark = 1 << 0,
  24. Outline = 1 << 1,
  25. Thumbnail = 1 << 2,
  26. Annotation = 1 << 3,
  27. Search = 1 << 4,
  28. }
  29. /// <summary>
  30. /// BOTABarControl.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class CPDFBOTABarControl : UserControl
  33. {
  34. private CPDFViewer pdfViewer;
  35. private ToggleButton bookmarkButton;
  36. private ToggleButton outlineButton;
  37. private ToggleButton thumbnailButton;
  38. private ToggleButton annotButton;
  39. private ToggleButton searchButton;
  40. private CPDFSearchControl searchControl;
  41. public CPDFBOTABarControl(BOTATools botaTools)
  42. {
  43. InitializeComponent();
  44. AddBOTAContent(botaTools);
  45. }
  46. public CPDFBOTABarControl()
  47. {
  48. InitializeComponent();
  49. }
  50. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  51. {
  52. this.pdfViewer = pdfViewer;
  53. UIElement currentBotaTool = GetBotaTool();
  54. if (currentBotaTool is CPDFSearchControl)
  55. {
  56. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  57. }
  58. if (currentBotaTool is CPDFThumbnailControl)
  59. {
  60. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  61. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  62. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  63. }
  64. if (currentBotaTool is CPDFBookmarkControl)
  65. {
  66. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  67. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  68. }
  69. if (currentBotaTool is CPDFOutlineControl)
  70. {
  71. ((CPDFOutlineControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  72. }
  73. if (currentBotaTool is CPDFAnnotationListControl)
  74. {
  75. CPDFAnnotationListControl annotListControl= currentBotaTool as CPDFAnnotationListControl;
  76. annotListControl.InitWithPDFViewer(pdfViewer);
  77. annotListControl.LoadAnnotationList();
  78. }
  79. }
  80. public void AddBOTAContent(BOTATools botaTools)
  81. {
  82. var brush = (Brush)FindResource("btn.bg.bota");
  83. while (botaTools > 0)
  84. {
  85. if ((botaTools & BOTATools.Thumbnail) > 0)
  86. {
  87. thumbnailButton = new ToggleButton();
  88. thumbnailButton.Background = brush;
  89. Geometry thumbnailGeometry = Geometry.Parse("M1.6001 0.850098H0.850098V1.6001V12.8001V13.5501H1.6001H4.0001V13.7001V15.2001H5.5001H13.7001H15.2001V13.7001V5.5001V4.0001H13.7001H13.5501V1.6001V0.850098H12.8001H1.6001ZM12.0501 4.0001V2.3501H2.3501V12.0501H4.0001V5.5001V4.0001H5.5001H12.0501ZM5.5001 5.5001H13.7001V13.7001H5.5001V5.5001Z");
  90. Path path = new Path
  91. {
  92. Width = 16,
  93. Height = 16,
  94. Data = thumbnailGeometry,
  95. Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  96. };
  97. thumbnailButton.Height = 40;
  98. thumbnailButton.Width = 52;
  99. thumbnailButton.BorderThickness = new Thickness(0);
  100. thumbnailButton.Style = FindResource("ToggleButtonStyle") as Style;
  101. thumbnailButton.Content = path;
  102. BOTABarTitleGrid.Children.Add(thumbnailButton);
  103. thumbnailButton.Click += ThumbnailButton_Click;
  104. botaTools -= BOTATools.Thumbnail;
  105. }
  106. else if ((botaTools & BOTATools.Outline) > 0)
  107. {
  108. outlineButton = new ToggleButton();
  109. outlineButton.Background = brush;
  110. Geometry outlineGeometry = Geometry.Parse("M3 0.5H0V3.5H3V0.5ZM3 5.5H0V8.5H3V5.5ZM0 10.5H3V13.5H0V10.5ZM14 2.75V1.25H5V2.75H14ZM14 6.25V7.75H5V6.25H14ZM14 12.75V11.25H5V12.75H14Z");
  111. Path path = new Path
  112. {
  113. Width = 16,
  114. Height = 16,
  115. Data = outlineGeometry,
  116. Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  117. };
  118. outlineButton.Height = 40;
  119. outlineButton.Width = 52;
  120. outlineButton.BorderThickness= new Thickness(0);
  121. outlineButton.Style = FindResource("ToggleButtonStyle") as Style;
  122. outlineButton.Content = path;
  123. BOTABarTitleGrid.Children.Add(outlineButton);
  124. outlineButton.Click += OutlineButton_Click;
  125. outlineButton.ToolTip = botaTools.ToString();
  126. botaTools -= BOTATools.Outline;
  127. }
  128. else if ((botaTools & BOTATools.Bookmark) > 0)
  129. {
  130. bookmarkButton = new ToggleButton();
  131. bookmarkButton.Background = brush;
  132. Geometry bookmarkGeometry = Geometry.Parse("M5.6221 9.85217L0.75 12.6942V0.75H11.25V12.6942L6.3779 9.85217L6 9.63172L5.6221 9.85217Z");
  133. Path path = new Path
  134. {
  135. Width = 16,
  136. Height = 16,
  137. Data = bookmarkGeometry,
  138. StrokeThickness = 1.5,
  139. Stroke = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  140. };
  141. bookmarkButton.Height = 40;
  142. bookmarkButton.Width = 52;
  143. bookmarkButton.BorderThickness = new Thickness(0);
  144. bookmarkButton.Style = FindResource("ToggleButtonStyle") as Style;
  145. bookmarkButton.Content = path;
  146. BOTABarTitleGrid.Children.Add(bookmarkButton);
  147. bookmarkButton.Click += BookmarkButton_Click;
  148. botaTools -= BOTATools.Bookmark;
  149. }
  150. else if ((botaTools & BOTATools.Annotation) > 0)
  151. {
  152. annotButton = new ToggleButton();
  153. annotButton.Background = brush;
  154. Geometry thumbnailGeometry = Geometry.Parse("M16 0H1.6L0 14H14.4L16 0ZM8.34885 1.8143L12.0693 11.1154H13.2692V12.1154H10.1923V11.1154H10.9922L10.2384 9.23077H5.53048L4.77689 11.1154H5.57692V12.1154H2.5V11.1154H3.69995L7.42038 1.8143H8.34885ZM7.884 3.345L9.83837 8.23077H5.93035L7.884 3.345Z");
  155. Path path = new Path
  156. {
  157. Width = 16,
  158. Height = 16,
  159. Data = thumbnailGeometry,
  160. Fill = (Brush)FindResource("btn.logo.bota")
  161. };
  162. annotButton.Height = 40;
  163. annotButton.Width = 52;
  164. annotButton.BorderThickness = new Thickness(0);
  165. annotButton.Style = FindResource("ToggleButtonStyle") as Style;
  166. annotButton.Content = path;
  167. BOTABarTitleGrid.Children.Add(annotButton);
  168. annotButton.Click += AnnotButton_Click;
  169. botaTools -= BOTATools.Annotation;
  170. }
  171. else if ((botaTools & BOTATools.Search) > 0)
  172. {
  173. searchButton = new ToggleButton();
  174. searchButton.Background = brush;
  175. Geometry thumbnailGeometry = Geometry.Parse("M3.4284 10.1635C1.56851 8.30364 1.56851 5.28816 3.4284 3.42827C5.28829 1.56838 8.30377 1.56838 10.1637 3.42827C12.0235 5.28816 12.0235 8.30364 10.1637 10.1635C8.30377 12.0234 5.28829 12.0234 3.4284 10.1635ZM2.36774 2.36761C-0.0779349 4.81329 -0.0779349 8.77851 2.36774 11.2242C4.63397 13.4904 8.20494 13.6567 10.6626 11.723L12.8875 13.9479C13.1804 14.2408 13.6552 14.2408 13.9481 13.9479C14.241 13.655 14.241 13.1801 13.9481 12.8872L11.7233 10.6624C13.6568 8.20466 13.4905 4.63379 11.2243 2.36761C8.77864 -0.078065 4.81342 -0.078065 2.36774 2.36761Z");
  176. Path path = new Path
  177. {
  178. Width = 16,
  179. Height = 16,
  180. Data = thumbnailGeometry,
  181. Fill = (Brush)FindResource("btn.logo.bota"),
  182. };
  183. searchButton.Height = 40;
  184. searchButton.Width = 52;
  185. searchButton.BorderThickness = new Thickness(0);
  186. searchButton.Style = FindResource("ToggleButtonStyle") as Style;
  187. searchButton.Content = path;
  188. BOTABarTitleGrid.Children.Add(searchButton);
  189. searchButton.Click += SearchButton_Click;
  190. botaTools -= BOTATools.Search;
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 获取Bota工具
  196. /// </summary>
  197. /// <returns></returns>
  198. private UIElement GetBotaTool()
  199. {
  200. return BotaToolContainer.Child;
  201. }
  202. private void SetBotaTool(UIElement newChild)
  203. {
  204. BotaToolContainer.Child = newChild;
  205. }
  206. /// <summary>
  207. /// 展开Bota工具
  208. /// </summary>
  209. /// <param name="isExpand"></param>
  210. private void ExpandTool(bool isExpand)
  211. {
  212. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  213. }
  214. /// <summary>
  215. /// 清除工具栏状态
  216. /// </summary>
  217. private void ClearToolState(UIElement ignoreTool)
  218. {
  219. foreach (UIElement child in BOTABarTitleGrid.Children)
  220. {
  221. if (child != ignoreTool && child is ToggleButton buttonTool)
  222. {
  223. buttonTool.IsChecked = false;
  224. }
  225. }
  226. }
  227. private void SearchButton_Click(object sender, RoutedEventArgs e)
  228. {
  229. SelectBotaTool(BOTATools.Search);
  230. }
  231. private void AnnotButton_Click(object sender, RoutedEventArgs e)
  232. {
  233. SelectBotaTool(BOTATools.Annotation);
  234. }
  235. private void ThumbnailButton_Click(object sender, RoutedEventArgs e)
  236. {
  237. SelectBotaTool(BOTATools.Thumbnail);
  238. }
  239. private void OutlineButton_Click(object sender, RoutedEventArgs e)
  240. {
  241. SelectBotaTool(BOTATools.Outline);
  242. }
  243. private void BookmarkButton_Click(object sender, RoutedEventArgs e)
  244. {
  245. SelectBotaTool(BOTATools.Bookmark);
  246. }
  247. public void SelectBotaTool(BOTATools tool)
  248. {
  249. UIElement botaTool = GetBotaTool();
  250. ToggleButton checkBtn = null;
  251. switch(tool)
  252. {
  253. case BOTATools.Thumbnail:
  254. {
  255. if(thumbnailButton != null)
  256. {
  257. thumbnailButton.IsChecked = true;
  258. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  259. {
  260. CPDFThumbnailControl thumbnailControl = new CPDFThumbnailControl();
  261. if (pdfViewer != null && pdfViewer.Document != null)
  262. {
  263. thumbnailControl.InitWithPDFViewer(pdfViewer);
  264. thumbnailControl.LoadThumb();
  265. }
  266. SetBotaTool(thumbnailControl);
  267. checkBtn = thumbnailButton;
  268. }
  269. }
  270. }
  271. break;
  272. case BOTATools.Outline:
  273. {
  274. if(outlineButton != null)
  275. {
  276. outlineButton.IsChecked = true;
  277. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  278. {
  279. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  280. if (pdfViewer != null && pdfViewer.Document != null)
  281. {
  282. outlineControl.InitWithPDFViewer(pdfViewer);
  283. }
  284. SetBotaTool(outlineControl);
  285. checkBtn = outlineButton;
  286. }
  287. }
  288. }
  289. break;
  290. case BOTATools.Bookmark:
  291. {
  292. if(bookmarkButton!=null)
  293. {
  294. bookmarkButton.IsChecked = true;
  295. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  296. {
  297. CPDFBookmarkControl pdfBookmarkControl = new CPDFBookmarkControl();
  298. if (pdfViewer != null && pdfViewer.Document != null)
  299. {
  300. pdfBookmarkControl.InitWithPDFViewer(pdfViewer);
  301. pdfBookmarkControl.LoadBookmark();
  302. }
  303. SetBotaTool(pdfBookmarkControl);
  304. }
  305. checkBtn = bookmarkButton;
  306. }
  307. }
  308. break;
  309. case BOTATools.Search:
  310. {
  311. if(searchButton!=null)
  312. {
  313. searchButton.IsChecked = true;
  314. if (botaTool == null || !(botaTool is CPDFSearchControl))
  315. {
  316. if (searchControl == null)
  317. {
  318. searchControl = new CPDFSearchControl();
  319. if (pdfViewer != null && pdfViewer.Document != null)
  320. {
  321. searchControl.InitWithPDFViewer(pdfViewer);
  322. }
  323. }
  324. SetBotaTool(searchControl);
  325. checkBtn = searchButton;
  326. }
  327. }
  328. }
  329. break;
  330. case BOTATools.Annotation:
  331. {
  332. if(annotButton!=null)
  333. {
  334. annotButton.IsChecked = true;
  335. if (botaTool == null || !(botaTool is CPDFAnnotationListControl))
  336. {
  337. CPDFAnnotationListControl annotationListControl = new CPDFAnnotationListControl();
  338. if (pdfViewer != null && pdfViewer.Document != null)
  339. {
  340. annotationListControl.InitWithPDFViewer(pdfViewer);
  341. annotationListControl.LoadAnnotationList();
  342. }
  343. SetBotaTool(annotationListControl);
  344. checkBtn = annotButton;
  345. }
  346. }
  347. }
  348. break;
  349. default:
  350. break;
  351. }
  352. if(checkBtn!=null)
  353. {
  354. ExpandTool(checkBtn.IsChecked == true);
  355. ClearToolState(checkBtn);
  356. }
  357. }
  358. public void LoadAnnotationList()
  359. {
  360. UIElement currentBotaTool = GetBotaTool();
  361. if (currentBotaTool is CPDFAnnotationListControl)
  362. {
  363. ((CPDFAnnotationListControl)currentBotaTool).LoadAnnotationList();
  364. }
  365. }
  366. }
  367. }