CPDFShapeUI.xaml.cs 11 KB

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