CPDFCloudUI.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using ComPDFKit.Controls.Common;
  2. using ComPDFKit.Controls.Data;
  3. using ComPDFKit.Controls.PDFControl;
  4. using ComPDFKit.PDFAnnotation;
  5. using ComPDFKit.Tool;
  6. using ComPDFKit.Tool.Help;
  7. using ComPDFKit.Tool.UndoManger;
  8. using ComPDFKitViewer.Helper;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Windows.Controls;
  13. using System.Windows.Media;
  14. namespace ComPDFKit.Controls.PDFControlUI
  15. {
  16. /// <summary>
  17. /// Interaction logic for CPDFCloudUI.xaml
  18. /// </summary>
  19. public partial class CPDFCloudUI : UserControl
  20. {
  21. bool IsLoadedData = false;
  22. private AnnotParam annotParam;
  23. private CPDFAnnotation annotCore;
  24. private PDFViewControl viewControl;
  25. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  26. public CPDFCloudUI()
  27. {
  28. InitializeComponent();
  29. ctlBorderColorPicker.ColorChanged -= CtlBorderColorPicker_ColorChanged;
  30. ctlFillColorPicker.ColorChanged -= CtlFillColorPicker_ColorChanged;
  31. CPDFOpacityControl.OpacityChanged -= CPDFOpacityControl_OpacityChanged;
  32. CPDFThicknessControl.ThicknessChanged -= CPDFThicknessControl_ThicknessChanged;
  33. CPDFLineShapeControl.LineShapeChanged -= CPDFLineShapeControl_LineShapeChanged;
  34. ctlLineStyle.LineStyleChanged -= CtlLineStyle_LineStyleChanged;
  35. ctlBorderColorPicker.ColorChanged += CtlBorderColorPicker_ColorChanged;
  36. ctlFillColorPicker.ColorChanged += CtlFillColorPicker_ColorChanged;
  37. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  38. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  39. CPDFLineShapeControl.LineShapeChanged += CPDFLineShapeControl_LineShapeChanged;
  40. ctlLineStyle.LineStyleChanged += CtlLineStyle_LineStyleChanged;
  41. CPDFAnnotationPreviewerControl.DrawCloudPreview();
  42. }
  43. private void CtlLineStyle_LineStyleChanged(object sender, EventArgs e)
  44. {
  45. if (annotParam == null)
  46. {
  47. PropertyChanged?.Invoke(this, GetPolygonData());
  48. }
  49. else
  50. {
  51. if (annotCore != null && annotCore.IsValid())
  52. {
  53. float[] dashArray = null;
  54. C_BORDER_STYLE borderStyle;
  55. if (ctlLineStyle.DashStyle == DashStyles.Solid || ctlLineStyle.DashStyle == null)
  56. {
  57. dashArray = new float[0];
  58. borderStyle = C_BORDER_STYLE.BS_SOLID;
  59. }
  60. else
  61. {
  62. List<float> floatArray = new List<float>();
  63. foreach (double num in ctlLineStyle.DashStyle.Dashes)
  64. {
  65. floatArray.Add((float)num);
  66. }
  67. dashArray = floatArray.ToArray();
  68. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  69. }
  70. if (viewControl != null && viewControl.PDFViewTool != null)
  71. {
  72. PolygonMeasureAnnotHistory history = new PolygonMeasureAnnotHistory();
  73. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  74. history.Action = HistoryAction.Update;
  75. CPDFPolygonAnnotation polygonAnnotation = annotCore as CPDFPolygonAnnotation;
  76. if (polygonAnnotation == null || polygonAnnotation.Dash.SequenceEqual(dashArray)) return;
  77. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  78. polygonAnnotation.SetBorderStyle(borderStyle, dashArray);
  79. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  80. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  81. annotCore.UpdateAp();
  82. viewControl.UpdateAnnotFrame();
  83. }
  84. }
  85. }
  86. }
  87. private void CPDFLineShapeControl_LineShapeChanged(object sender, CPDFBorderEffector e)
  88. {
  89. if (annotParam == null)
  90. {
  91. PropertyChanged?.Invoke(this, GetPolygonData());
  92. }
  93. if (IsLoadedData)
  94. {
  95. if (annotCore != null && annotCore.IsValid() && annotCore is CPDFPolygonAnnotation polygonAnnotation)
  96. {
  97. PolygonMeasureAnnotHistory history = new PolygonMeasureAnnotHistory();
  98. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  99. history.Action = HistoryAction.Update;
  100. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  101. polygonAnnotation.SetAnnotBorderEffector(e);
  102. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  103. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  104. annotCore.UpdateAp();
  105. viewControl.UpdateAnnotFrame();
  106. }
  107. }
  108. }
  109. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  110. {
  111. if (annotParam == null)
  112. {
  113. PropertyChanged?.Invoke(this, GetPolygonData());
  114. }
  115. if (IsLoadedData)
  116. {
  117. if (annotCore != null && annotCore.IsValid() && annotCore is CPDFPolygonAnnotation polygonAnnotation)
  118. {
  119. PolygonMeasureAnnotHistory history = new PolygonMeasureAnnotHistory();
  120. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  121. history.Action = HistoryAction.Update;
  122. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, annotCore);
  123. polygonAnnotation.SetLineWidth((sender as CPDFThicknessControl).Thickness);
  124. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, annotCore);
  125. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  126. annotCore.UpdateAp();
  127. viewControl.UpdateAnnotFrame();
  128. }
  129. }
  130. }
  131. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  132. {
  133. if (annotParam == null)
  134. {
  135. PropertyChanged?.Invoke(this, GetPolygonData());
  136. }
  137. if (IsLoadedData)
  138. {
  139. if (annotCore != null && annotCore.IsValid())
  140. {
  141. double opacity = (sender as CPDFOpacityControl).OpacityValue / 100.0;
  142. if (opacity > 0 && opacity <= 1)
  143. {
  144. opacity *= 255;
  145. }
  146. PolygonMeasureAnnotHistory history = new PolygonMeasureAnnotHistory();
  147. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  148. history.Action = HistoryAction.Update;
  149. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, annotCore);
  150. annotCore.SetTransparency((byte)opacity);
  151. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, annotCore);
  152. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  153. annotCore.UpdateAp();
  154. viewControl.UpdateAnnotFrame();
  155. }
  156. }
  157. }
  158. private void CtlFillColorPicker_ColorChanged(object sender, EventArgs e)
  159. {
  160. if (annotParam == null)
  161. {
  162. PropertyChanged?.Invoke(this, GetPolygonData());
  163. }
  164. if (IsLoadedData)
  165. {
  166. if (annotCore != null && annotCore.IsValid())
  167. {
  168. if (annotCore is CPDFPolygonAnnotation polygonAnnotation)
  169. {
  170. PolygonMeasureAnnotHistory history = new PolygonMeasureAnnotHistory();
  171. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  172. history.Action = HistoryAction.Update;
  173. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  174. SolidColorBrush brush = (sender as ColorPickerControl)?.Brush as SolidColorBrush;
  175. polygonAnnotation.SetBgColor(new byte[3]
  176. {
  177. brush.Color.R,
  178. brush.Color.G,
  179. brush.Color.B
  180. });
  181. if(brush.Color.A == 0)
  182. {
  183. polygonAnnotation.ClearBgColor();
  184. }
  185. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  186. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  187. annotCore.UpdateAp();
  188. viewControl.UpdateAnnotFrame();
  189. }
  190. }
  191. }
  192. }
  193. private void CtlBorderColorPicker_ColorChanged(object sender, EventArgs e)
  194. {
  195. if (annotParam == null)
  196. {
  197. PropertyChanged?.Invoke(this, GetPolygonData());
  198. }
  199. if (IsLoadedData)
  200. {
  201. if (annotCore != null && annotCore.IsValid())
  202. {
  203. if (annotCore is CPDFPolygonAnnotation polygonAnnotation)
  204. {
  205. PolygonMeasureAnnotHistory history = new PolygonMeasureAnnotHistory();
  206. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  207. history.Action = HistoryAction.Update;
  208. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  209. SolidColorBrush brush = (sender as ColorPickerControl)?.Brush as SolidColorBrush;
  210. polygonAnnotation.SetLineColor(new byte[3]
  211. {
  212. brush.Color.R,
  213. brush.Color.G,
  214. brush.Color.B
  215. });
  216. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  217. annotCore.UpdateAp();
  218. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  219. viewControl.UpdateAnnotFrame();
  220. }
  221. }
  222. }
  223. }
  224. public void SetPresentAnnotAttrib(PolygonMeasureParam polygonParam, CPDFPolygonAnnotation annotation, PDFViewControl view, int PageCount)
  225. {
  226. annotParam = polygonParam;
  227. annotCore = annotation;
  228. viewControl = view;
  229. if (polygonParam == null)
  230. {
  231. return;
  232. }
  233. Color lineColor = Color.FromRgb(polygonParam.LineColor[0], polygonParam.LineColor[1], polygonParam.LineColor[2]);
  234. Color fillColor;
  235. if (annotation.HasBgColor)
  236. {
  237. fillColor = Color.FromRgb(polygonParam.FillColor[0], polygonParam.FillColor[1], polygonParam.FillColor[2]);
  238. }
  239. else
  240. {
  241. fillColor = Colors.Transparent;
  242. }
  243. ctlBorderColorPicker.SetCheckedForColor(lineColor);
  244. ctlFillColorPicker.SetCheckedForColor(fillColor);
  245. double opacity = polygonParam.Transparency / 255.0 * 100.0;
  246. CPDFOpacityControl.OpacityValue = (int)Math.Ceiling(opacity);
  247. float thickness = polygonParam.LineWidth;
  248. CPDFThicknessControl.Thickness = (int)Math.Ceiling(thickness);
  249. CPDFLineShapeControl.BorderEffector = annotation.GetAnnotBorderEffector();
  250. if (polygonParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  251. {
  252. ctlLineStyle.DashStyle = DashStyles.Solid;
  253. }
  254. else
  255. {
  256. List<double> dashArray = new List<double>();
  257. foreach (double num in polygonParam.LineDash)
  258. {
  259. dashArray.Add(num);
  260. }
  261. ctlLineStyle.DashStyle = new DashStyle(dashArray, 0);
  262. }
  263. NoteTextBox.Text = polygonParam.Content;
  264. }
  265. public CPDFAnnotationData GetPolygonData()
  266. {
  267. CPDFPolygonData polygonData = new CPDFPolygonData
  268. {
  269. AnnotationType = CPDFAnnotationType.Polygon,
  270. BorderColor = ((SolidColorBrush)ctlBorderColorPicker.Brush).Color,
  271. FillColor = ((SolidColorBrush)ctlFillColorPicker.Brush).Color,
  272. BorderEffector = CPDFLineShapeControl.BorderEffector,
  273. DashStyle = ctlLineStyle.DashStyle,
  274. IsMeasured = false,
  275. Thickness = CPDFThicknessControl.Thickness,
  276. Opacity = CPDFOpacityControl.OpacityValue,
  277. Note = NoteTextBox.Text
  278. };
  279. return polygonData;
  280. }
  281. private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
  282. {
  283. IsLoadedData = true;
  284. }
  285. private void UserControl_Unloaded(object sender, System.Windows.RoutedEventArgs e)
  286. {
  287. IsLoadedData = false;
  288. }
  289. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  290. {
  291. if (IsLoadedData)
  292. {
  293. if (annotCore != null && annotCore.IsValid())
  294. {
  295. annotCore.SetContent(NoteTextBox.Text);
  296. annotCore.UpdateAp();
  297. viewControl?.UpdateAnnotFrame();
  298. }
  299. }
  300. }
  301. }
  302. }