CPDFBOTABarControl.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. public partial class CPDFBOTABarControl : UserControl
  30. {
  31. private CPDFViewer pdfViewer;
  32. private ToggleButton bookmarkButton;
  33. private ToggleButton outlineButton;
  34. private ToggleButton thumbnailButton;
  35. private ToggleButton annotButton;
  36. private ToggleButton searchButton;
  37. private CPDFSearchControl searchControl;
  38. public CPDFBOTABarControl(BOTATools botaTools)
  39. {
  40. InitializeComponent();
  41. AddBOTAContent(botaTools);
  42. }
  43. public CPDFBOTABarControl()
  44. {
  45. InitializeComponent();
  46. }
  47. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  48. {
  49. this.pdfViewer = pdfViewer;
  50. UIElement currentBotaTool = GetBotaTool();
  51. if (currentBotaTool is CPDFSearchControl)
  52. {
  53. ((CPDFSearchControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  54. }
  55. if (currentBotaTool is CPDFThumbnailControl)
  56. {
  57. ((CPDFThumbnailControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  58. ((CPDFThumbnailControl)currentBotaTool).ThumbLoaded = false;
  59. ((CPDFThumbnailControl)currentBotaTool).LoadThumb();
  60. }
  61. if (currentBotaTool is CPDFBookmarkControl)
  62. {
  63. ((CPDFBookmarkControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  64. ((CPDFBookmarkControl)currentBotaTool).LoadBookmark();
  65. }
  66. if (currentBotaTool is CPDFOutlineControl)
  67. {
  68. ((CPDFOutlineControl)currentBotaTool).InitWithPDFViewer(pdfViewer);
  69. }
  70. if (currentBotaTool is CPDFAnnotationListControl)
  71. {
  72. CPDFAnnotationListControl annotListControl= currentBotaTool as CPDFAnnotationListControl;
  73. annotListControl.InitWithPDFViewer(pdfViewer);
  74. annotListControl.LoadAnnotationList();
  75. }
  76. }
  77. public void AddBOTAContent(BOTATools botaTools)
  78. {
  79. var brush = (Brush)FindResource("btn.bg.bota");
  80. while (botaTools > 0)
  81. {
  82. if ((botaTools & BOTATools.Thumbnail) > 0)
  83. {
  84. thumbnailButton = new ToggleButton();
  85. thumbnailButton.Background = brush;
  86. 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");
  87. Path path = new Path
  88. {
  89. Width = 16,
  90. Height = 16,
  91. Data = thumbnailGeometry,
  92. Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  93. };
  94. thumbnailButton.Height = 40;
  95. thumbnailButton.Width = 52;
  96. thumbnailButton.BorderThickness = new Thickness(0);
  97. thumbnailButton.Style = FindResource("ToggleButtonStyle") as Style;
  98. thumbnailButton.Content = path;
  99. BOTABarTitleGrid.Children.Add(thumbnailButton);
  100. thumbnailButton.Click += ThumbnailButton_Click;
  101. botaTools -= BOTATools.Thumbnail;
  102. }
  103. else if ((botaTools & BOTATools.Outline) > 0)
  104. {
  105. outlineButton = new ToggleButton();
  106. outlineButton.Background = brush;
  107. 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");
  108. Path path = new Path
  109. {
  110. Width = 16,
  111. Height = 16,
  112. Data = outlineGeometry,
  113. Fill = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  114. };
  115. outlineButton.Height = 40;
  116. outlineButton.Width = 52;
  117. outlineButton.BorderThickness= new Thickness(0);
  118. outlineButton.Style = FindResource("ToggleButtonStyle") as Style;
  119. outlineButton.Content = path;
  120. BOTABarTitleGrid.Children.Add(outlineButton);
  121. outlineButton.Click += OutlineButton_Click;
  122. outlineButton.ToolTip = botaTools.ToString();
  123. botaTools -= BOTATools.Outline;
  124. }
  125. else if ((botaTools & BOTATools.Bookmark) > 0)
  126. {
  127. bookmarkButton = new ToggleButton();
  128. bookmarkButton.Background = brush;
  129. Geometry bookmarkGeometry = Geometry.Parse("M5.6221 9.85217L0.75 12.6942V0.75H11.25V12.6942L6.3779 9.85217L6 9.63172L5.6221 9.85217Z");
  130. Path path = new Path
  131. {
  132. Width = 16,
  133. Height = 16,
  134. Data = bookmarkGeometry,
  135. StrokeThickness = 1.5,
  136. Stroke = new SolidColorBrush(Color.FromRgb(0x27, 0x3C, 0x62))
  137. };
  138. bookmarkButton.Height = 40;
  139. bookmarkButton.Width = 52;
  140. bookmarkButton.BorderThickness = new Thickness(0);
  141. bookmarkButton.Style = FindResource("ToggleButtonStyle") as Style;
  142. bookmarkButton.Content = path;
  143. BOTABarTitleGrid.Children.Add(bookmarkButton);
  144. bookmarkButton.Click += BookmarkButton_Click;
  145. botaTools -= BOTATools.Bookmark;
  146. }
  147. else if ((botaTools & BOTATools.Annotation) > 0)
  148. {
  149. annotButton = new ToggleButton();
  150. annotButton.Background = brush;
  151. 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");
  152. Path path = new Path
  153. {
  154. Width = 16,
  155. Height = 16,
  156. Data = thumbnailGeometry,
  157. Fill = (Brush)FindResource("btn.logo.bota")
  158. };
  159. annotButton.Height = 40;
  160. annotButton.Width = 52;
  161. annotButton.BorderThickness = new Thickness(0);
  162. annotButton.Style = FindResource("ToggleButtonStyle") as Style;
  163. annotButton.Content = path;
  164. BOTABarTitleGrid.Children.Add(annotButton);
  165. annotButton.Click += AnnotButton_Click;
  166. botaTools -= BOTATools.Annotation;
  167. }
  168. else if ((botaTools & BOTATools.Search) > 0)
  169. {
  170. searchButton = new ToggleButton();
  171. searchButton.Background = brush;
  172. 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");
  173. Path path = new Path
  174. {
  175. Width = 16,
  176. Height = 16,
  177. Data = thumbnailGeometry,
  178. Fill = (Brush)FindResource("btn.logo.bota"),
  179. };
  180. searchButton.Height = 40;
  181. searchButton.Width = 52;
  182. searchButton.BorderThickness = new Thickness(0);
  183. searchButton.Style = FindResource("ToggleButtonStyle") as Style;
  184. searchButton.Content = path;
  185. BOTABarTitleGrid.Children.Add(searchButton);
  186. searchButton.Click += SearchButton_Click;
  187. botaTools -= BOTATools.Search;
  188. }
  189. }
  190. }
  191. private UIElement GetBotaTool()
  192. {
  193. return BotaToolContainer.Child;
  194. }
  195. private void SetBotaTool(UIElement newChild)
  196. {
  197. BotaToolContainer.Child = newChild;
  198. }
  199. private void ExpandTool(bool isExpand)
  200. {
  201. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  202. }
  203. private void ClearToolState(UIElement ignoreTool)
  204. {
  205. foreach (UIElement child in BOTABarTitleGrid.Children)
  206. {
  207. if (child != ignoreTool && child is ToggleButton buttonTool)
  208. {
  209. buttonTool.IsChecked = false;
  210. }
  211. }
  212. }
  213. private void SearchButton_Click(object sender, RoutedEventArgs e)
  214. {
  215. SelectBotaTool(BOTATools.Search);
  216. }
  217. private void AnnotButton_Click(object sender, RoutedEventArgs e)
  218. {
  219. SelectBotaTool(BOTATools.Annotation);
  220. }
  221. private void ThumbnailButton_Click(object sender, RoutedEventArgs e)
  222. {
  223. SelectBotaTool(BOTATools.Thumbnail);
  224. }
  225. private void OutlineButton_Click(object sender, RoutedEventArgs e)
  226. {
  227. SelectBotaTool(BOTATools.Outline);
  228. }
  229. private void BookmarkButton_Click(object sender, RoutedEventArgs e)
  230. {
  231. SelectBotaTool(BOTATools.Bookmark);
  232. }
  233. public void SelectBotaTool(BOTATools tool)
  234. {
  235. UIElement botaTool = GetBotaTool();
  236. ToggleButton checkBtn = null;
  237. switch(tool)
  238. {
  239. case BOTATools.Thumbnail:
  240. {
  241. if(thumbnailButton != null)
  242. {
  243. thumbnailButton.IsChecked = true;
  244. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  245. {
  246. CPDFThumbnailControl thumbnailControl = new CPDFThumbnailControl();
  247. if (pdfViewer != null && pdfViewer.Document != null)
  248. {
  249. thumbnailControl.InitWithPDFViewer(pdfViewer);
  250. thumbnailControl.LoadThumb();
  251. }
  252. SetBotaTool(thumbnailControl);
  253. checkBtn = thumbnailButton;
  254. }
  255. }
  256. }
  257. break;
  258. case BOTATools.Outline:
  259. {
  260. if(outlineButton != null)
  261. {
  262. outlineButton.IsChecked = true;
  263. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  264. {
  265. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  266. if (pdfViewer != null && pdfViewer.Document != null)
  267. {
  268. outlineControl.InitWithPDFViewer(pdfViewer);
  269. }
  270. SetBotaTool(outlineControl);
  271. checkBtn = outlineButton;
  272. }
  273. }
  274. }
  275. break;
  276. case BOTATools.Bookmark:
  277. {
  278. if(bookmarkButton!=null)
  279. {
  280. bookmarkButton.IsChecked = true;
  281. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  282. {
  283. CPDFBookmarkControl pdfBookmarkControl = new CPDFBookmarkControl();
  284. if (pdfViewer != null && pdfViewer.Document != null)
  285. {
  286. pdfBookmarkControl.InitWithPDFViewer(pdfViewer);
  287. pdfBookmarkControl.LoadBookmark();
  288. }
  289. SetBotaTool(pdfBookmarkControl);
  290. }
  291. checkBtn = bookmarkButton;
  292. }
  293. }
  294. break;
  295. case BOTATools.Search:
  296. {
  297. if(searchButton!=null)
  298. {
  299. searchButton.IsChecked = true;
  300. if (botaTool == null || !(botaTool is CPDFSearchControl))
  301. {
  302. if (searchControl == null)
  303. {
  304. searchControl = new CPDFSearchControl();
  305. if (pdfViewer != null && pdfViewer.Document != null)
  306. {
  307. searchControl.InitWithPDFViewer(pdfViewer);
  308. }
  309. }
  310. SetBotaTool(searchControl);
  311. checkBtn = searchButton;
  312. }
  313. }
  314. }
  315. break;
  316. case BOTATools.Annotation:
  317. {
  318. if(annotButton!=null)
  319. {
  320. annotButton.IsChecked = true;
  321. if (botaTool == null || !(botaTool is CPDFAnnotationListControl))
  322. {
  323. CPDFAnnotationListControl annotationListControl = new CPDFAnnotationListControl();
  324. if (pdfViewer != null && pdfViewer.Document != null)
  325. {
  326. annotationListControl.InitWithPDFViewer(pdfViewer);
  327. annotationListControl.LoadAnnotationList();
  328. }
  329. SetBotaTool(annotationListControl);
  330. checkBtn = annotButton;
  331. }
  332. }
  333. }
  334. break;
  335. default:
  336. break;
  337. }
  338. if(checkBtn!=null)
  339. {
  340. ExpandTool(checkBtn.IsChecked == true);
  341. ClearToolState(checkBtn);
  342. }
  343. }
  344. public void LoadAnnotationList()
  345. {
  346. UIElement currentBotaTool = GetBotaTool();
  347. if (currentBotaTool is CPDFAnnotationListControl)
  348. {
  349. ((CPDFAnnotationListControl)currentBotaTool).LoadAnnotationList();
  350. }
  351. }
  352. }
  353. }