CPDFBOTABarControl.xaml.cs 19 KB

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