CPDFBOTABarControl.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. using Compdfkit_Tools.DigitalSignature.CPDFSignatureListControl;
  20. namespace Compdfkit_Tools.PDFControl
  21. {
  22. public enum BOTATools
  23. {
  24. Bookmark = 1 << 0,
  25. Outline = 1 << 1,
  26. Thumbnail = 1 << 2,
  27. Annotation = 1 << 3,
  28. Search = 1 << 4,
  29. Signature = 1 << 5,
  30. }
  31. public partial class CPDFBOTABarControl : UserControl
  32. {
  33. private CPDFViewer pdfViewer;
  34. private ToggleButton bookmarkButton;
  35. private ToggleButton outlineButton;
  36. private ToggleButton thumbnailButton;
  37. private ToggleButton annotButton;
  38. private ToggleButton searchButton;
  39. private CPDFSearchControl searchControl;
  40. private ToggleButton signatureButton;
  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. else if ((botaTools & BOTATools.Signature) > 0)
  193. {
  194. signatureButton = new ToggleButton();
  195. signatureButton.Background = brush;
  196. Geometry thumbnailGeometry = Geometry.Parse("M12.5 0H3.5C1.57 0 0 1.57 0 3.5V12.5C0 14.43 1.57 16 3.5 16H12.5C14.43 16 16 14.43 16 12.5V3.5C16 1.57 14.43 0 12.5 0ZM14 12.5C14 13.88 13.13 15 12 15H4C2.9 15 2 14.1 2 13V3C2 1.9 2.9 1 4 1H12C13.13 1 14 1.9 14 3V12.5ZM5 5H7V7H5V5ZM5 9H11V11H5V9ZM5 13H7V15H5V13Z");
  197. Path path = new Path
  198. {
  199. Width = 16,
  200. Height = 16,
  201. Data = thumbnailGeometry,
  202. Fill = (Brush)FindResource("btn.logo.bota"),
  203. };
  204. signatureButton.Height = 40;
  205. signatureButton.Width = 52;
  206. signatureButton.BorderThickness = new Thickness(0);
  207. signatureButton.Style = FindResource("ToggleButtonStyle") as Style;
  208. signatureButton.Content = path;
  209. BOTABarTitleGrid.Children.Add(signatureButton);
  210. signatureButton.Click += SignatureButton_Click;
  211. botaTools -= BOTATools.Signature;
  212. }
  213. }
  214. }
  215. private UIElement GetBotaTool()
  216. {
  217. return BotaToolContainer.Child;
  218. }
  219. private void SetBotaTool(UIElement newChild)
  220. {
  221. BotaToolContainer.Child = newChild;
  222. }
  223. private void ExpandTool(bool isExpand)
  224. {
  225. BotaToolContainer.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
  226. }
  227. private void ClearToolState(UIElement ignoreTool)
  228. {
  229. foreach (UIElement child in BOTABarTitleGrid.Children)
  230. {
  231. if (child != ignoreTool && child is ToggleButton buttonTool)
  232. {
  233. buttonTool.IsChecked = false;
  234. }
  235. }
  236. }
  237. private void SearchButton_Click(object sender, RoutedEventArgs e)
  238. {
  239. SelectBotaTool(BOTATools.Search);
  240. }
  241. private void AnnotButton_Click(object sender, RoutedEventArgs e)
  242. {
  243. SelectBotaTool(BOTATools.Annotation);
  244. }
  245. private void ThumbnailButton_Click(object sender, RoutedEventArgs e)
  246. {
  247. SelectBotaTool(BOTATools.Thumbnail);
  248. }
  249. private void OutlineButton_Click(object sender, RoutedEventArgs e)
  250. {
  251. SelectBotaTool(BOTATools.Outline);
  252. }
  253. private void BookmarkButton_Click(object sender, RoutedEventArgs e)
  254. {
  255. SelectBotaTool(BOTATools.Bookmark);
  256. }
  257. private void SignatureButton_Click(object sender, RoutedEventArgs e)
  258. {
  259. SelectBotaTool(BOTATools.Signature);
  260. }
  261. public void SelectBotaTool(BOTATools tool)
  262. {
  263. UIElement botaTool = GetBotaTool();
  264. ToggleButton checkBtn = null;
  265. switch(tool)
  266. {
  267. case BOTATools.Thumbnail:
  268. {
  269. if(thumbnailButton != null)
  270. {
  271. thumbnailButton.IsChecked = true;
  272. if (botaTool == null || !(botaTool is CPDFThumbnailControl))
  273. {
  274. CPDFThumbnailControl thumbnailControl = new CPDFThumbnailControl();
  275. if (pdfViewer != null && pdfViewer.Document != null)
  276. {
  277. thumbnailControl.InitWithPDFViewer(pdfViewer);
  278. thumbnailControl.LoadThumb();
  279. }
  280. SetBotaTool(thumbnailControl);
  281. checkBtn = thumbnailButton;
  282. }
  283. }
  284. }
  285. break;
  286. case BOTATools.Outline:
  287. {
  288. if(outlineButton != null)
  289. {
  290. outlineButton.IsChecked = true;
  291. if (botaTool == null || !(botaTool is CPDFOutlineControl))
  292. {
  293. CPDFOutlineControl outlineControl = new CPDFOutlineControl();
  294. if (pdfViewer != null && pdfViewer.Document != null)
  295. {
  296. outlineControl.InitWithPDFViewer(pdfViewer);
  297. }
  298. SetBotaTool(outlineControl);
  299. checkBtn = outlineButton;
  300. }
  301. }
  302. }
  303. break;
  304. case BOTATools.Bookmark:
  305. {
  306. if(bookmarkButton!=null)
  307. {
  308. bookmarkButton.IsChecked = true;
  309. if (botaTool == null || !(botaTool is CPDFBookmarkControl))
  310. {
  311. CPDFBookmarkControl pdfBookmarkControl = new CPDFBookmarkControl();
  312. if (pdfViewer != null && pdfViewer.Document != null)
  313. {
  314. pdfBookmarkControl.InitWithPDFViewer(pdfViewer);
  315. pdfBookmarkControl.LoadBookmark();
  316. }
  317. SetBotaTool(pdfBookmarkControl);
  318. }
  319. checkBtn = bookmarkButton;
  320. }
  321. }
  322. break;
  323. case BOTATools.Search:
  324. {
  325. if(searchButton!=null)
  326. {
  327. searchButton.IsChecked = true;
  328. if (botaTool == null || !(botaTool is CPDFSearchControl))
  329. {
  330. if (searchControl == null)
  331. {
  332. searchControl = new CPDFSearchControl();
  333. if (pdfViewer != null && pdfViewer.Document != null)
  334. {
  335. searchControl.InitWithPDFViewer(pdfViewer);
  336. }
  337. }
  338. SetBotaTool(searchControl);
  339. checkBtn = searchButton;
  340. }
  341. }
  342. }
  343. break;
  344. case BOTATools.Annotation:
  345. {
  346. if(annotButton!=null)
  347. {
  348. annotButton.IsChecked = true;
  349. if (botaTool == null || !(botaTool is CPDFAnnotationListControl))
  350. {
  351. CPDFAnnotationListControl annotationListControl = new CPDFAnnotationListControl();
  352. if (pdfViewer != null && pdfViewer.Document != null)
  353. {
  354. annotationListControl.InitWithPDFViewer(pdfViewer);
  355. annotationListControl.LoadAnnotationList();
  356. }
  357. SetBotaTool(annotationListControl);
  358. checkBtn = annotButton;
  359. }
  360. }
  361. }
  362. break;
  363. case BOTATools.Signature:
  364. {
  365. if (signatureButton != null)
  366. {
  367. signatureButton.IsChecked = true;
  368. if (botaTool == null || !(botaTool is CPDFSignatureListControl))
  369. {
  370. CPDFSignatureListControl signatureControl = new CPDFSignatureListControl();
  371. if (pdfViewer != null && pdfViewer.Document != null)
  372. {
  373. signatureControl.InitWithPDFViewer(pdfViewer);
  374. signatureControl.LoadSignatureList();
  375. }
  376. SetBotaTool(signatureControl);
  377. checkBtn = signatureButton;
  378. }
  379. }
  380. }
  381. break;
  382. default:
  383. break;
  384. }
  385. if(checkBtn!=null)
  386. {
  387. ExpandTool(checkBtn.IsChecked == true);
  388. ClearToolState(checkBtn);
  389. }
  390. }
  391. public void LoadAnnotationList()
  392. {
  393. UIElement currentBotaTool = GetBotaTool();
  394. if (currentBotaTool is CPDFAnnotationListControl)
  395. {
  396. ((CPDFAnnotationListControl)currentBotaTool).LoadAnnotationList();
  397. }
  398. }
  399. }
  400. }