CPDFShapeUI.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using compdfkit_tools.Common;
  2. using compdfkit_tools.Data;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using ComPDFKitViewer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  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.PDFAnnotation;
  20. namespace compdfkit_tools.PDFControlUI
  21. {
  22. /// <summary>
  23. /// CPDFRectUI.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class CPDFShapeUI : UserControl
  26. {
  27. private CPDFAnnotationType currentAnnotationType;
  28. private AnnotAttribEvent annotAttribEvent;
  29. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  30. public CPDFShapeUI()
  31. {
  32. InitializeComponent();
  33. BorderColorPickerControl.ColorChanged += BorderColorPickerControl_ColorChanged;
  34. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  35. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  36. CPDFLineStyleControl.LineStyleChanged += CPDFLineStyleControl_LineStyleChanged;
  37. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  38. }
  39. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  40. {
  41. if (annotAttribEvent == null)
  42. {
  43. PropertyChanged?.Invoke(this, GetShapeData());
  44. }
  45. else
  46. {
  47. annotAttribEvent.UpdateAttrib(AnnotAttrib.Color, ((SolidColorBrush)BorderColorPickerControl.Brush).Color);
  48. annotAttribEvent.UpdateAnnot();
  49. }
  50. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  51. }
  52. private void FillColorPickerControl_ColorChanged(object sender, EventArgs e)
  53. {
  54. if (annotAttribEvent == null)
  55. {
  56. PropertyChanged?.Invoke(this, GetShapeData());
  57. }
  58. else
  59. {
  60. annotAttribEvent.UpdateAttrib(AnnotAttrib.FillColor, ((SolidColorBrush)FillColorPickerControl.Brush).Color);
  61. annotAttribEvent.UpdateAnnot();
  62. }
  63. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  64. }
  65. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  66. {
  67. if (annotAttribEvent == null)
  68. {
  69. PropertyChanged?.Invoke(this, GetShapeData());
  70. }
  71. else
  72. {
  73. annotAttribEvent.UpdateAttrib(AnnotAttrib.Thickness, CPDFThicknessControl.Thickness);
  74. annotAttribEvent.UpdateAnnot();
  75. }
  76. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  77. }
  78. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  79. {
  80. if (annotAttribEvent == null)
  81. {
  82. PropertyChanged?.Invoke(this, GetShapeData());
  83. }
  84. else
  85. {
  86. annotAttribEvent.UpdateAttrib(AnnotAttrib.Transparency, CPDFOpacityControl.OpacityValue / 100.0);
  87. annotAttribEvent.UpdateAnnot();
  88. }
  89. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  90. }
  91. private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
  92. {
  93. if (annotAttribEvent == null)
  94. {
  95. PropertyChanged?.Invoke(this, GetShapeData());
  96. }
  97. else
  98. {
  99. annotAttribEvent.UpdateAttrib(AnnotAttrib.LineStyle, CPDFLineStyleControl.DashStyle);
  100. annotAttribEvent.UpdateAnnot();
  101. }
  102. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  103. }
  104. private void CPDFArrowControl_ArrowChanged(object sender, EventArgs e)
  105. {
  106. if (annotAttribEvent == null)
  107. {
  108. PropertyChanged?.Invoke(PropertyChanged, GetShapeData());
  109. }
  110. else
  111. {
  112. annotAttribEvent.UpdateAttrib(AnnotAttrib.LineStart, CPDFArrowControl.LineType.HeadLineType);
  113. annotAttribEvent.UpdateAttrib(AnnotAttrib.LineEnd, CPDFArrowControl.LineType.TailLineType);
  114. annotAttribEvent.UpdateAnnot();
  115. }
  116. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  117. }
  118. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  119. {
  120. if (annotAttribEvent == null)
  121. {
  122. PropertyChanged?.Invoke(this, GetShapeData());
  123. }
  124. else
  125. {
  126. annotAttribEvent.UpdateAttrib(AnnotAttrib.NoteText, NoteTextBox.Text);
  127. annotAttribEvent.UpdateAnnot();
  128. }
  129. }
  130. public CPDFAnnotationData GetShapeData()
  131. {
  132. if (currentAnnotationType == CPDFAnnotationType.Circle || currentAnnotationType == CPDFAnnotationType.Square)
  133. {
  134. CPDFShapeData pdfShapeData = new CPDFShapeData();
  135. pdfShapeData.AnnotationType = currentAnnotationType;
  136. pdfShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  137. pdfShapeData.FillColor = ((SolidColorBrush)FillColorPickerControl.Brush).Color;
  138. pdfShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  139. pdfShapeData.Thickness = CPDFThicknessControl.Thickness;
  140. pdfShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
  141. pdfShapeData.Note = NoteTextBox.Text;
  142. return pdfShapeData;
  143. }
  144. else
  145. {
  146. CPDFLineShapeData pdfLineShapeData = new CPDFLineShapeData();
  147. pdfLineShapeData.AnnotationType = currentAnnotationType;
  148. pdfLineShapeData.BorderColor = ((SolidColorBrush)BorderColorPickerControl.Brush).Color;
  149. pdfLineShapeData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  150. pdfLineShapeData.LineType = CPDFArrowControl.LineType;
  151. pdfLineShapeData.Thickness = CPDFThicknessControl.Thickness;
  152. pdfLineShapeData.DashStyle = CPDFLineStyleControl.DashStyle;
  153. pdfLineShapeData.LineType = CPDFArrowControl.LineType;
  154. pdfLineShapeData.Note = NoteTextBox.Text;
  155. return pdfLineShapeData;
  156. }
  157. }
  158. public void SetPresentAnnotAttrib(AnnotAttribEvent annotAttribEvent)
  159. {
  160. this.annotAttribEvent = null;
  161. BorderColorPickerControl.Brush = new SolidColorBrush((Color)annotAttribEvent.Attribs[AnnotAttrib.Color]);
  162. CPDFOpacityControl.OpacityValue = (int)((double)annotAttribEvent.Attribs[AnnotAttrib.Transparency] * 100);
  163. CPDFThicknessControl.Thickness = Convert.ToInt16(annotAttribEvent.Attribs[AnnotAttrib.Thickness]);
  164. NoteTextBox.Text = (string)annotAttribEvent.Attribs[AnnotAttrib.NoteText];
  165. CPDFLineStyleControl.DashStyle = (DashStyle)(annotAttribEvent.Attribs[AnnotAttrib.LineStyle]);
  166. if (annotAttribEvent.GetAnnotTypes() == AnnotArgsType.AnnotSquare ||
  167. annotAttribEvent.GetAnnotTypes() == AnnotArgsType.AnnotCircle)
  168. {
  169. FillColorPickerControl.Brush = new SolidColorBrush((Color)annotAttribEvent.Attribs[AnnotAttrib.FillColor]);
  170. }
  171. else
  172. {
  173. LineType lineType = new LineType()
  174. {
  175. HeadLineType = (C_LINE_TYPE)annotAttribEvent.Attribs[AnnotAttrib.LineStart],
  176. TailLineType = (C_LINE_TYPE)annotAttribEvent.Attribs[AnnotAttrib.LineEnd]
  177. };
  178. CPDFArrowControl.LineType = lineType;
  179. }
  180. this.annotAttribEvent = annotAttribEvent;
  181. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  182. }
  183. public void InitWhenRectAndRound()
  184. {
  185. FillColorStackPanel.Visibility = Visibility.Visible;
  186. ArrowStackPanel.Visibility = Visibility.Collapsed;
  187. FillColorPickerControl.ColorChanged += FillColorPickerControl_ColorChanged;
  188. CPDFArrowControl.ArrowChanged -= CPDFArrowControl_ArrowChanged;
  189. }
  190. public void InitWhenArrowAndLine()
  191. {
  192. FillColorStackPanel.Visibility = Visibility.Collapsed;
  193. ArrowStackPanel.Visibility = Visibility.Visible;
  194. CPDFArrowControl.ArrowChanged += CPDFArrowControl_ArrowChanged;
  195. FillColorPickerControl.ColorChanged -= FillColorPickerControl_ColorChanged;
  196. LineType lineType;
  197. if (currentAnnotationType == CPDFAnnotationType.Arrow)
  198. {
  199. lineType = new LineType()
  200. {
  201. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  202. TailLineType = C_LINE_TYPE.LINETYPE_ARROW
  203. };
  204. }
  205. else
  206. {
  207. lineType = new LineType()
  208. {
  209. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  210. TailLineType = C_LINE_TYPE.LINETYPE_NONE
  211. };
  212. }
  213. CPDFArrowControl.LineType = lineType;
  214. }
  215. public void InitWithAnnotationType(CPDFAnnotationType annotationType)
  216. {
  217. currentAnnotationType = annotationType;
  218. switch (annotationType)
  219. {
  220. case CPDFAnnotationType.Square:
  221. TitleTextBlock.Text = "Rectangle";
  222. InitWhenRectAndRound();
  223. break;
  224. case CPDFAnnotationType.Circle:
  225. TitleTextBlock.Text = "Circle";
  226. InitWhenRectAndRound();
  227. break;
  228. case CPDFAnnotationType.Arrow:
  229. TitleTextBlock.Text = "Arrow";
  230. InitWhenArrowAndLine();
  231. break;
  232. case CPDFAnnotationType.Line:
  233. TitleTextBlock.Text = "Line";
  234. InitWhenArrowAndLine();
  235. break;
  236. default:
  237. throw new ArgumentException("Not Excepted Argument");
  238. }
  239. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  240. }
  241. }
  242. }