CPDFCloudUI.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. }
  57. else
  58. {
  59. if (annotCore != null && annotCore.IsValid())
  60. {
  61. float[] dashArray = null;
  62. C_BORDER_STYLE borderStyle;
  63. if (ctlLineStyle.DashStyle == DashStyles.Solid || ctlLineStyle.DashStyle == null)
  64. {
  65. dashArray = new float[0];
  66. borderStyle = C_BORDER_STYLE.BS_SOLID;
  67. }
  68. else
  69. {
  70. List<float> floatArray = new List<float>();
  71. foreach (double num in ctlLineStyle.DashStyle.Dashes)
  72. {
  73. floatArray.Add((float)num);
  74. }
  75. dashArray = floatArray.ToArray();
  76. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  77. }
  78. if (viewControl != null && viewControl.PDFViewTool != null)
  79. {
  80. AnnotHistory history = GetHistory();
  81. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  82. history.Action = HistoryAction.Update;
  83. CPDFPolygonAnnotation polygonAnnotation = annotCore as CPDFPolygonAnnotation;
  84. if (polygonAnnotation == null || polygonAnnotation.Dash.SequenceEqual(dashArray)) return;
  85. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  86. polygonAnnotation.SetBorderStyle(borderStyle, dashArray);
  87. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  88. annotCore.UpdateAp();
  89. viewControl.UpdateAnnotFrame();
  90. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  91. }
  92. }
  93. }
  94. }
  95. private void CPDFLineShapeControl_LineShapeChanged(object sender, CPDFBorderEffector e)
  96. {
  97. if (IsLoadedData)
  98. {
  99. if (annotCore != null && annotCore.IsValid())
  100. {
  101. (annotCore as CPDFPolygonAnnotation).SetAnnotBorderEffector(e);
  102. annotCore.UpdateAp();
  103. viewControl.UpdateAnnotFrame();
  104. }
  105. }
  106. }
  107. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  108. {
  109. if (IsLoadedData)
  110. {
  111. if (annotCore != null && annotCore.IsValid())
  112. {
  113. double thickness = (sender as CPDFThicknessControl).Thickness;
  114. if (Math.Abs(thickness - annotCore.GetTransparency()) > 1)
  115. {
  116. annotCore.SetBorderWidth((byte)thickness);
  117. annotCore.UpdateAp();
  118. viewControl.UpdateAnnotFrame();
  119. }
  120. }
  121. }
  122. }
  123. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  124. {
  125. if (IsLoadedData)
  126. {
  127. if (annotCore != null && annotCore.IsValid())
  128. {
  129. double opacity = (sender as CPDFOpacityControl).OpacityValue / 100.0;
  130. if (opacity > 0 && opacity <= 1)
  131. {
  132. opacity *= 255;
  133. }
  134. if (Math.Abs(opacity - annotCore.GetTransparency()) > 0.01)
  135. {
  136. annotCore.SetTransparency((byte)opacity);
  137. annotCore.UpdateAp();
  138. viewControl.UpdateAnnotFrame();
  139. }
  140. }
  141. }
  142. }
  143. private void CtlFillColorPicker_ColorChanged(object sender, EventArgs e)
  144. {
  145. if (IsLoadedData)
  146. {
  147. if (annotCore != null && annotCore.IsValid())
  148. {
  149. if (annotCore is CPDFPolygonAnnotation polygonAnnotation)
  150. {
  151. PolygonAnnotHistory history = new PolygonAnnotHistory();
  152. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  153. history.Action = ComPDFKitViewer.Helper.HistoryAction.Update;
  154. SolidColorBrush brush = (sender as ColorPickerControl)?.Brush as SolidColorBrush;
  155. polygonAnnotation.SetBgColor(new byte[3]
  156. {
  157. brush.Color.R,
  158. brush.Color.G,
  159. brush.Color.B
  160. });
  161. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  162. annotCore.UpdateAp();
  163. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  164. viewControl.UpdateAnnotFrame();
  165. }
  166. }
  167. }
  168. }
  169. private void CtlBorderColorPicker_ColorChanged(object sender, EventArgs e)
  170. {
  171. if (IsLoadedData)
  172. {
  173. if (annotCore != null && annotCore.IsValid())
  174. {
  175. if (annotCore is CPDFPolygonAnnotation polygonAnnotation)
  176. {
  177. PolygonAnnotHistory history = new PolygonAnnotHistory();
  178. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  179. history.Action = ComPDFKitViewer.Helper.HistoryAction.Update;
  180. SolidColorBrush brush = (sender as ColorPickerControl)?.Brush as SolidColorBrush;
  181. polygonAnnotation.SetLineColor(new byte[3]
  182. {
  183. brush.Color.R,
  184. brush.Color.G,
  185. brush.Color.B
  186. });
  187. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, annotCore.Page.PageIndex, polygonAnnotation);
  188. annotCore.UpdateAp();
  189. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  190. viewControl.UpdateAnnotFrame();
  191. }
  192. }
  193. }
  194. }
  195. public void SetPresentAnnotAttrib(PolygonMeasureParam polygonParam, CPDFPolygonAnnotation annotation, PDFViewControl view, int PageCount)
  196. {
  197. annotParam = polygonParam;
  198. annotCore = annotation;
  199. viewControl = view;
  200. if (polygonParam == null)
  201. {
  202. return;
  203. }
  204. Color lineColor = Color.FromRgb(polygonParam.LineColor[0], polygonParam.LineColor[1], polygonParam.LineColor[2]);
  205. ctlBorderColorPicker.SetCheckedForColor(lineColor);
  206. double opacity = polygonParam.Transparency / 255.0 * 100.0;
  207. CPDFOpacityControl.OpacityValue = (int)Math.Ceiling(opacity);
  208. float thickness = polygonParam.LineWidth;
  209. CPDFThicknessControl.Thickness = (int)Math.Ceiling(thickness);
  210. CPDFLineShapeControl.BorderEffector = annotation.GetAnnotBorderEffector();
  211. if (polygonParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  212. {
  213. ctlLineStyle.DashStyle = DashStyles.Solid;
  214. }
  215. else
  216. {
  217. List<double> dashArray = new List<double>();
  218. foreach (double num in polygonParam.LineDash)
  219. {
  220. dashArray.Add(num);
  221. }
  222. ctlLineStyle.DashStyle = new DashStyle(dashArray, 0);
  223. }
  224. NoteTextBox.Text = polygonParam.Content;
  225. }
  226. public CPDFAnnotationData GetPolygonData()
  227. {
  228. CPDFPolygonData polygonData = new CPDFPolygonData
  229. {
  230. AnnotationType = CPDFAnnotationType.Polygon,
  231. BorderColor = ((SolidColorBrush)ctlBorderColorPicker.Brush).Color,
  232. FillColor = ((SolidColorBrush)ctlFillColorPicker.Brush).Color,
  233. IsMeasured = false,
  234. Thickness = CPDFThicknessControl.Thickness,
  235. Opacity = CPDFOpacityControl.Opacity / 100,
  236. Note = NoteTextBox.Text
  237. };
  238. return polygonData;
  239. }
  240. private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
  241. {
  242. IsLoadedData = true;
  243. }
  244. private void UserControl_Unloaded(object sender, System.Windows.RoutedEventArgs e)
  245. {
  246. IsLoadedData = false;
  247. }
  248. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  249. {
  250. if (IsLoadedData)
  251. {
  252. if (annotCore != null && annotCore.IsValid())
  253. {
  254. annotCore.SetContent(NoteTextBox.Text);
  255. annotCore.UpdateAp();
  256. viewControl?.UpdateAnnotFrame();
  257. }
  258. }
  259. }
  260. }
  261. }