CPDFCloudUI.xaml.cs 13 KB

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