CPDFShapeUI.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.SetCheckedForColor((Color)annotAttribEvent.Attribs[AnnotAttrib.Color]);
  155. }
  156. if (annotAttribEvent.Attribs != null && annotAttribEvent.Attribs.ContainsKey(AnnotAttrib.FillColor))
  157. {
  158. FillColorPickerControl.SetCheckedForColor((Color)annotAttribEvent.Attribs[AnnotAttrib.FillColor]);
  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. public void InitWhenRectAndRound()
  179. {
  180. FillColorStackPanel.Visibility = Visibility.Visible;
  181. ArrowStackPanel.Visibility = Visibility.Collapsed;
  182. FillColorPickerControl.ColorChanged += FillColorPickerControl_ColorChanged;
  183. CPDFArrowControl.ArrowChanged -= CPDFArrowControl_ArrowChanged;
  184. }
  185. public void InitWhenArrowAndLine()
  186. {
  187. FillColorStackPanel.Visibility = Visibility.Collapsed;
  188. ArrowStackPanel.Visibility = Visibility.Visible;
  189. CPDFArrowControl.ArrowChanged += CPDFArrowControl_ArrowChanged;
  190. FillColorPickerControl.ColorChanged -= FillColorPickerControl_ColorChanged;
  191. LineType lineType;
  192. if (currentAnnotationType == CPDFAnnotationType.Arrow)
  193. {
  194. lineType = new LineType()
  195. {
  196. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  197. TailLineType = C_LINE_TYPE.LINETYPE_ARROW
  198. };
  199. }
  200. else
  201. {
  202. lineType = new LineType()
  203. {
  204. HeadLineType = C_LINE_TYPE.LINETYPE_NONE,
  205. TailLineType = C_LINE_TYPE.LINETYPE_NONE
  206. };
  207. }
  208. CPDFArrowControl.LineType = lineType;
  209. }
  210. public void InitWithAnnotationType(CPDFAnnotationType annotationType)
  211. {
  212. currentAnnotationType = annotationType;
  213. switch (annotationType)
  214. {
  215. case CPDFAnnotationType.Square:
  216. TitleTextBlock.Text = "Rectangle";
  217. InitWhenRectAndRound();
  218. break;
  219. case CPDFAnnotationType.Circle:
  220. TitleTextBlock.Text = "Circle";
  221. InitWhenRectAndRound();
  222. break;
  223. case CPDFAnnotationType.Arrow:
  224. TitleTextBlock.Text = "Arrow";
  225. InitWhenArrowAndLine();
  226. break;
  227. case CPDFAnnotationType.Line:
  228. TitleTextBlock.Text = "Line";
  229. InitWhenArrowAndLine();
  230. break;
  231. default:
  232. throw new ArgumentException("Not Excepted Argument");
  233. }
  234. CPDFAnnotationPreviewerControl.DrawShapePreview(GetShapeData());
  235. }
  236. }
  237. }