CPDFShapeUI.xaml.cs 12 KB

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