CPDFBOTABarControl.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 0.25H0.25V1V15V15.75H1H4V16.5V18H5.5H16.5H18V16.5V5.5V4H16.5H15.75V1V0.25H15H1ZM14.25 4V1.75H1.75V14.25H4V5.5V4H5.5H14.25ZM5.5 15.75V16.5H16.5V5.5H5.5V14.25V15.75Z");
  90. Path path = new Path
  91. {
  92. Width = 20,
  93. Height = 20,
  94. Data = thumbnailGeometry,
  95. Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  96. };
  97. thumbnailButton.Height = 40;
  98. thumbnailButton.Width = 65;
  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(" M4.5 1.5H1.5V4.5H4.5V1.5ZM4.5 6.5H1.5V9.5H4.5V6.5ZM1.5 11.5H4.5V14.5H1.5V11.5ZM15.5 3.75V2.25H6.5V3.75H15.5ZM15.5 7.25V8.75H6.5V7.25H15.5ZM15.5 13.75V12.25H6.5V13.75H15.5Z");
  111. Path path = new Path
  112. {
  113. Width = 20,
  114. Height = 20,
  115. Data = outlineGeometry,
  116. Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  117. };
  118. outlineButton.Height = 40;
  119. outlineButton.Width = 65;
  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. botaTools -= BOTATools.Outline;
  126. }
  127. else if ((botaTools & BOTATools.Bookmark) > 0)
  128. {
  129. bookmarkButton = new ToggleButton();
  130. bookmarkButton.Background = brush;
  131. Geometry bookmarkGeometry = Geometry.Parse("M6.1221 9.85217L1.25 12.6942V0.75H11.75V12.6942L6.8779 9.85217L6.5 9.63172L6.1221 9.85217Z");
  132. Path path = new Path
  133. {
  134. Width = 20,
  135. Height = 20,
  136. Data = bookmarkGeometry,
  137. Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  138. };
  139. bookmarkButton.Height = 40;
  140. bookmarkButton.Width = 65;
  141. bookmarkButton.BorderThickness = new Thickness(0);
  142. bookmarkButton.Style = FindResource("ToggleButtonStyle") as Style;
  143. bookmarkButton.Content = path;
  144. BOTABarTitleGrid.Children.Add(bookmarkButton);
  145. bookmarkButton.Click += BookmarkButton_Click;
  146. botaTools -= BOTATools.Bookmark;
  147. }
  148. else if ((botaTools & BOTATools.Annotation) > 0)
  149. {
  150. annotButton = new ToggleButton();
  151. annotButton.Background = brush;
  152. 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 "+
  153. "11.1154H5.57692V12.1154H2.5V11.1154H3.69995L7.42038 1.8143H8.34885ZM7.884 3.345L9.83837 8.23077H5.93035L7.884 3.345Z");
  154. Path path = new Path
  155. {
  156. Width = 20,
  157. Height = 20,
  158. Data = thumbnailGeometry,
  159. Fill = (Brush)FindResource("btn.logo.bota")
  160. };
  161. annotButton.Height = 40;
  162. annotButton.Width = 65;
  163. annotButton.BorderThickness = new Thickness(0);
  164. annotButton.Style = FindResource("ToggleButtonStyle") as Style;
  165. annotButton.Content = path;
  166. BOTABarTitleGrid.Children.Add(annotButton);
  167. annotButton.Click += AnnotButton_Click;
  168. botaTools -= BOTATools.Annotation;
  169. }
  170. else if ((botaTools & BOTATools.Search) > 0)
  171. {
  172. searchButton = new ToggleButton();
  173. searchButton.Background = brush;
  174. Geometry thumbnailGeometry = Geometry.Parse(
  175. "M4.9284 11.1635C3.06851 9.30364 3.06851 6.28816 4.9284 4.42827C6.78829 2.56838 9.80377 2.56838 11.6637 4.42827C13.5235 6.28816 13.5235 9.30364 11.6637 11.1635C9.80377 "+
  176. "13.0234 6.78829 13.0234 4.9284 11.1635ZM3.86774 3.36761C1.42207 5.81329 1.42207 9.77851 3.86774 12.2242C6.13397 14.4904 9.70494 14.6567 12.1626 12.723L14.3875 "+
  177. "14.9479C14.6804 15.2408 15.1552 15.2408 15.4481 14.9479C15.741 14.655 15.741 14.1801 15.4481 13.8872L13.2233 11.6624C15.1568 9.20466 14.9905 5.63379 12.7243 "+
  178. "3.36761C10.2786 0.921935 6.31342 0.921935 3.86774 3.36761Z");
  179. Path path = new Path
  180. {
  181. Width = 20,
  182. Height = 20,
  183. Data = thumbnailGeometry,
  184. Fill = (Brush)FindResource("btn.logo.bota"),
  185. };
  186. searchButton.Height = 40;
  187. searchButton.Width = 65;
  188. searchButton.BorderThickness = new Thickness(0);
  189. searchButton.Style = FindResource("ToggleButtonStyle") as Style;
  190. searchButton.Content = path;
  191. BOTABarTitleGrid.Children.Add(searchButton);
  192. searchButton.Click += SearchButton_Click;
  193. botaTools -= BOTATools.Search;
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// 获取Bota工具
  199. /// </summary>
  200. /// <returns></returns>
  201. private UIElement GetBotaTool()
  202. {
  203. return BotaToolContainer.Child;
  204. }
  205. private void SetBotaTool(UIElement newChild)
  206. {
  207. BotaToolContainer.Child = newChild;
  208. }
  209. /// <summary>
  210. /// 展开Bota工具
  211. /// </summary>
  212. /// <param name="isExpand"></param>
  213. private void ExpandTool(bool isExpand)
  214. {
  215. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  216. }
  217. /// <summary>
  218. /// 清除工具栏状态
  219. /// </summary>
  220. private void ClearToolState(UIElement ignoreTool)
  221. {
  222. foreach (UIElement child in BOTABarTitleGrid.Children)
  223. {
  224. if (child != ignoreTool && child is ToggleButton buttonTool)
  225. {
  226. buttonTool.IsChecked = false;
  227. }
  228. }
  229. }
  230. private void SearchButton_Click(object sender, RoutedEventArgs e)
  231. {
  232. SelectBotaTool(BOTATools.Search);
  233. }
  234. private void AnnotButton_Click(object sender, RoutedEventArgs e)
  235. {
  236. SelectBotaTool(BOTATools.Annotation);
  237. }
  238. private void ThumbnailButton_Click(object sender, RoutedEventArgs e)
  239. {
  240. SelectBotaTool(BOTATools.Thumbnail);
  241. }
  242. private void OutlineButton_Click(object sender, RoutedEventArgs e)
  243. {
  244. SelectBotaTool(BOTATools.Outline);
  245. }
  246. private void BookmarkButton_Click(object sender, RoutedEventArgs e)
  247. {
  248. SelectBotaTool(BOTATools.Bookmark);
  249. }
  250. public void SelectBotaTool(BOTATools tool)
  251. {
  252. UIElement botaTool = GetBotaTool();
  253. ToggleButton checkBtn = null;
  254. switch(tool)
  255. {
  256. case BOTATools.Thumbnail:
  257. {
  258. if(thumbnailButton != null)
  259. {
  260. thumbnailButton.IsChecked = true;
  261. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  262. {
  263. CPDFThumbnailControl thumbnailControl = new CPDFThumbnailControl();
  264. if (pdfViewer != null && pdfViewer.Document != null)
  265. {
  266. thumbnailControl.InitWithPDFViewer(pdfViewer);
  267. thumbnailControl.LoadThumb();
  268. }
  269. SetBotaTool(thumbnailControl);
  270. checkBtn = thumbnailButton;
  271. }
  272. }
  273. }
  274. break;
  275. case BOTATools.Outline:
  276. {
  277. if(outlineButton != null)
  278. {
  279. outlineButton.IsChecked = true;
  280. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  281. {
  282. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  283. if (pdfViewer != null && pdfViewer.Document != null)
  284. {
  285. outlineControl.InitWithPDFViewer(pdfViewer);
  286. }
  287. SetBotaTool(outlineControl);
  288. checkBtn = outlineButton;
  289. }
  290. }
  291. }
  292. break;
  293. case BOTATools.Bookmark:
  294. {
  295. if(bookmarkButton!=null)
  296. {
  297. bookmarkButton.IsChecked = true;
  298. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  299. {
  300. CPDFBookmarkControl pdfBookmarkControl = new CPDFBookmarkControl();
  301. if (pdfViewer != null && pdfViewer.Document != null)
  302. {
  303. pdfBookmarkControl.InitWithPDFViewer(pdfViewer);
  304. }
  305. SetBotaTool(pdfBookmarkControl);
  306. }
  307. checkBtn = bookmarkButton;
  308. }
  309. }
  310. break;
  311. case BOTATools.Search:
  312. {
  313. if(searchButton!=null)
  314. {
  315. searchButton.IsChecked = true;
  316. if (botaTool == null || !(botaTool is CPDFSearchControl))
  317. {
  318. if (searchControl == null)
  319. {
  320. searchControl = new CPDFSearchControl();
  321. if (pdfViewer != null && pdfViewer.Document != null)
  322. {
  323. searchControl.InitWithPDFViewer(pdfViewer);
  324. }
  325. }
  326. SetBotaTool(searchControl);
  327. checkBtn = searchButton;
  328. }
  329. }
  330. }
  331. break;
  332. case BOTATools.Annotation:
  333. {
  334. if(annotButton!=null)
  335. {
  336. annotButton.IsChecked = true;
  337. if (botaTool == null || !(botaTool is CPDFAnnotationListControl))
  338. {
  339. CPDFAnnotationListControl annotationListControl = new CPDFAnnotationListControl();
  340. if (pdfViewer != null && pdfViewer.Document != null)
  341. {
  342. annotationListControl.InitWithPDFViewer(pdfViewer);
  343. annotationListControl.LoadAnnotationList();
  344. }
  345. SetBotaTool(annotationListControl);
  346. checkBtn = annotButton;
  347. }
  348. }
  349. }
  350. break;
  351. default:
  352. break;
  353. }
  354. if(checkBtn!=null)
  355. {
  356. ExpandTool(checkBtn.IsChecked == true);
  357. ClearToolState(checkBtn);
  358. }
  359. }
  360. public void LoadAnnotationList()
  361. {
  362. UIElement currentBotaTool = GetBotaTool();
  363. if (currentBotaTool is CPDFAnnotationListControl)
  364. {
  365. ((CPDFAnnotationListControl)currentBotaTool).LoadAnnotationList();
  366. }
  367. }
  368. }
  369. }