CPDFBOTABarControl.xaml.cs 20 KB

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