CPDFAnnotationBarControl.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using compdfkit_tools.Data;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. namespace compdfkit_tools.PDFControl
  17. {
  18. /// <summary>
  19. /// PDFAnnotationBarControl.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class CPDFAnnotationBarControl : UserControl
  22. {
  23. private int annotationCounter = 0;
  24. private Brush brush = null;
  25. public event EventHandler<AnnotationType> AnnotationPropertyChanged;
  26. public event EventHandler AnnotationCancel;
  27. public CPDFAnnotationBarControl()
  28. {
  29. InitializeComponent();
  30. }
  31. private void CreateAnnotationButton(ToggleButton toggleButton)
  32. {
  33. toggleButton.Width = 50;
  34. toggleButton.Background = brush;
  35. Geometry annotationGeometry = Geometry.Parse(""); ;
  36. ImageBrush imageBrush = new ImageBrush();
  37. string path = string.Empty;
  38. if (toggleButton.Tag.ToString() == AnnotationType.Highlight.ToString())
  39. {
  40. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Highlight.png";
  41. }
  42. else if (toggleButton.Tag.ToString() == AnnotationType.Underline.ToString())
  43. {
  44. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Underline.png";
  45. }
  46. else if (toggleButton.Tag.ToString() == AnnotationType.Strikeout.ToString())
  47. {
  48. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Strikeout.png";
  49. }
  50. else if (toggleButton.Tag.ToString() == AnnotationType.Squiggly.ToString())
  51. {
  52. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Squiggly.png";
  53. }
  54. else if (toggleButton.Tag.ToString() == AnnotationType.FreeText.ToString())
  55. {
  56. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Freetext.png";
  57. }
  58. else if (toggleButton.Tag.ToString() == AnnotationType.Note.ToString())
  59. {
  60. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Note.png";
  61. }
  62. else if (toggleButton.Tag.ToString() == AnnotationType.Square.ToString())
  63. {
  64. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Rect.png";
  65. }
  66. else if (toggleButton.Tag.ToString() == AnnotationType.Circle.ToString())
  67. {
  68. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Round.png";
  69. }
  70. else if (toggleButton.Tag.ToString() == AnnotationType.Arrow.ToString())
  71. {
  72. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Arrow.png";
  73. }
  74. else if (toggleButton.Tag.ToString() == AnnotationType.Line.ToString())
  75. {
  76. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/StraightLine.png";
  77. }
  78. else if (toggleButton.Tag.ToString() == AnnotationType.Freehand.ToString())
  79. {
  80. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Freehand.png";
  81. }
  82. else if (toggleButton.Tag.ToString() == AnnotationType.Stamp.ToString())
  83. {
  84. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Stamp.png";
  85. }
  86. else if (toggleButton.Tag.ToString() == AnnotationType.Signature.ToString())
  87. {
  88. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Signature.png";
  89. }
  90. else if (toggleButton.Tag.ToString() == AnnotationType.Link.ToString())
  91. {
  92. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Link.png";
  93. }
  94. else if (toggleButton.Tag.ToString() == AnnotationType.Sound.ToString())
  95. {
  96. path = "pack://application:,,,/com.compdfkit.tools;component/Asset/Resource/Annotation/Sound.png";
  97. }
  98. if (path != string.Empty)
  99. {
  100. BitmapImage bitmapImage = new BitmapImage(new Uri(path, UriKind.Absolute));
  101. Image image = new Image();
  102. image.Source = bitmapImage;
  103. imageBrush.ImageSource = bitmapImage;
  104. Grid grid = new Grid();
  105. grid.Height = 20;
  106. grid.Width = 20;
  107. grid.Children.Add(image);
  108. toggleButton.Content = grid;
  109. toggleButton.Click += ToggleButton_Click;
  110. Grid.SetColumn(toggleButton, annotationCounter++);
  111. AnnotationGrid.Children.Add(toggleButton);
  112. }
  113. }
  114. private void ClearToolState(UIElement sender)
  115. {
  116. foreach (UIElement child in AnnotationGrid.Children)
  117. {
  118. if (child is ToggleButton toggle && (child as ToggleButton) != (sender as ToggleButton))
  119. {
  120. toggle.IsChecked = false;
  121. }
  122. }
  123. }
  124. private void ToggleButton_Click(object sender, RoutedEventArgs e)
  125. {
  126. ClearToolState(sender as ToggleButton);
  127. if((bool)(sender as ToggleButton).IsChecked)
  128. {
  129. AnnotationPropertyChanged?.Invoke(sender, CPDFAnnotationDictionary.GetAnnotationFromTag[(sender as ToggleButton).Tag.ToString()]);
  130. }
  131. else
  132. {
  133. AnnotationCancel?.Invoke(sender, EventArgs.Empty);
  134. }
  135. }
  136. public void InitAnnotationBar(AnnotationType[] annotationProperties)
  137. {
  138. brush = (Brush)FindResource("btn.bg.bota");
  139. for (int i = 0; i < annotationProperties.Length; i++)
  140. {
  141. AnnotationGrid.ColumnDefinitions.Add(new ColumnDefinition());
  142. AnnotationType annotation = annotationProperties[i];
  143. ToggleButton toggleButton = new ToggleButton();
  144. toggleButton.Tag = annotation.ToString();
  145. CreateAnnotationButton(toggleButton);
  146. AnnotationGrid.Width += 50;
  147. }
  148. }
  149. }
  150. }