PolygonalProperty.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using ComPDFKit.Controls.Common;
  2. using ComPDFKitViewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using ComPDFKit.Controls.PDFControl;
  19. using ComPDFKit.PDFAnnotation;
  20. using ComPDFKit.Tool;
  21. using CFontNameHelper = ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  22. namespace ComPDFKit.Controls.Measure.Property
  23. {
  24. public partial class PolygonalProperty : UserControl
  25. {
  26. public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
  27. {
  28. 6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
  29. };
  30. bool IsLoadedData = false;
  31. private PolygonMeasureParam polygonMeasureParam;
  32. public CPDFPolygonAnnotation Annotation{ get; set; }
  33. public PDFViewControl ViewControl{ get; set; }
  34. //private AnnotAttribEvent PolygonalEvent { get; set; }
  35. public PolygonalProperty()
  36. {
  37. InitializeComponent();
  38. }
  39. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  40. {
  41. if (IsLoadedData)
  42. {
  43. if (Annotation != null && ViewControl != null)
  44. {
  45. Annotation.SetContent(NoteTextBox.Text);
  46. Annotation.UpdateAp();
  47. ViewControl?.UpdateAnnotFrame();
  48. }
  49. }
  50. }
  51. private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  52. {
  53. if (!IsLoadedData) return;
  54. int selectIndex = Math.Max(0, FontStyleCombox.SelectedIndex);
  55. bool isBold = false;
  56. bool isItalic = false;
  57. switch (selectIndex)
  58. {
  59. case 0:
  60. isBold = false;
  61. isItalic = false;
  62. break;
  63. case 1:
  64. isBold = true;
  65. isItalic = false;
  66. break;
  67. case 2:
  68. isBold = false;
  69. isItalic = true;
  70. break;
  71. case 3:
  72. isBold = true;
  73. isItalic = true;
  74. break;
  75. default:
  76. break;
  77. }
  78. if(Annotation != null)
  79. {
  80. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  81. var fontType = CFontNameHelper.GetFontType((FontCombox.SelectedItem as ComboBoxItem).Content.ToString());
  82. var newName = CFontNameHelper.ObtainFontName(fontType, isBold, isItalic);
  83. textAttribute.FontName = newName;
  84. Annotation.SetTextAttribute(textAttribute);
  85. Annotation.UpdateAp();
  86. ViewControl?.UpdateAnnotFrame();
  87. }
  88. }
  89. private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  90. {
  91. if (IsLoadedData)
  92. {
  93. if (FontSizeComboBox.SelectedItem != null)
  94. {
  95. if (Annotation != null && ViewControl != null)
  96. {
  97. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  98. textAttribute.FontSize = (float)Convert.ToDouble(FontSizeComboBox.SelectedItem);
  99. Annotation.SetTextAttribute(textAttribute);
  100. Annotation.UpdateAp();
  101. ViewControl?.UpdateAnnotFrame();
  102. }
  103. }
  104. }
  105. }
  106. private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  107. {
  108. if (IsLoadedData)
  109. {
  110. if (Annotation != null && ViewControl != null)
  111. {
  112. ComboBoxItem selectItem = FontCombox.SelectedItem as ComboBoxItem;
  113. if (selectItem != null && selectItem.Content != null)
  114. {
  115. CTextAttribute textAttr = Annotation.GetTextAttribute();
  116. bool isBold = CFontNameHelper.IsBold(textAttr.FontName);
  117. bool isItalic = CFontNameHelper.IsItalic(textAttr.FontName);
  118. var fontType = CFontNameHelper.GetFontType(selectItem.Content.ToString());
  119. textAttr.FontName = CFontNameHelper.ObtainFontName(fontType, isBold, isItalic);
  120. Annotation.SetTextAttribute(textAttr);
  121. Annotation.UpdateAp();
  122. ViewControl.UpdateAnnotFrame();
  123. }
  124. }
  125. }
  126. }
  127. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  128. {
  129. Binding SizeListbinding = new Binding();
  130. SizeListbinding.Source = this;
  131. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  132. FontSizeComboBox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  133. IsLoadedData = true;
  134. }
  135. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  136. {
  137. IsLoadedData = false;
  138. }
  139. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  140. {
  141. if (IsLoadedData)
  142. {
  143. if (Annotation != null && ViewControl != null)
  144. {
  145. if (BorderColorPickerControl.GetBrush() is SolidColorBrush checkBrush)
  146. {
  147. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  148. Annotation.SetLineColor(color);
  149. Annotation.UpdateAp();
  150. ViewControl.UpdateAnnotFrame();
  151. }
  152. }
  153. }
  154. }
  155. private void FillColorPickerControl_ColorChanged(object sender, EventArgs e)
  156. {
  157. if (IsLoadedData)
  158. {
  159. if (Annotation != null && ViewControl != null)
  160. {
  161. SolidColorBrush checkBrush = FillColorPickerControl.GetBrush() as SolidColorBrush;
  162. if (checkBrush != null)
  163. {
  164. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  165. Annotation.SetBgColor(color);
  166. Annotation.UpdateAp();
  167. ViewControl.UpdateAnnotFrame();
  168. }
  169. }
  170. }
  171. }
  172. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  173. {
  174. if (IsLoadedData)
  175. {
  176. if (Annotation != null && ViewControl != null)
  177. {
  178. double opacity = CPDFOpacityControl.OpacityValue / 100.0;
  179. if (opacity > 0 && opacity <= 1)
  180. {
  181. opacity = opacity * 255;
  182. }
  183. if (Math.Abs(opacity - Annotation.GetTransparency()) > 0.01)
  184. {
  185. Annotation.SetTransparency((byte)opacity);
  186. Annotation.UpdateAp();
  187. ViewControl.UpdateAnnotFrame();
  188. }
  189. }
  190. }
  191. }
  192. private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
  193. {
  194. if (!IsLoadedData) return;
  195. if (Annotation != null && ViewControl != null)
  196. {
  197. float[] dashArray = null;
  198. C_BORDER_STYLE borderStyle;
  199. if (CPDFLineStyleControl.DashStyle == DashStyles.Solid || CPDFLineStyleControl.DashStyle == null)
  200. {
  201. dashArray = new float[0];
  202. borderStyle = C_BORDER_STYLE.BS_SOLID;
  203. }
  204. else
  205. {
  206. List<float> floatArray = new List<float>();
  207. foreach (double num in CPDFLineStyleControl.DashStyle.Dashes)
  208. {
  209. floatArray.Add((float)num);
  210. }
  211. dashArray = floatArray.ToArray();
  212. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  213. }
  214. Annotation.SetBorderStyle(borderStyle, dashArray);
  215. Annotation.UpdateAp();
  216. ViewControl.UpdateAnnotFrame();
  217. }
  218. }
  219. private void FontColorPickerControl_ColorChanged(object sender, EventArgs e)
  220. {
  221. if (!IsLoadedData) return;
  222. SolidColorBrush checkBrush = FontColorPickerControl.GetBrush() as SolidColorBrush;
  223. if (checkBrush != null && Annotation != null && ViewControl != null)
  224. {
  225. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  226. if (Annotation != null)
  227. {
  228. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  229. textAttribute.FontColor = color;
  230. Annotation.SetTextAttribute(textAttribute);
  231. Annotation.UpdateAp();
  232. ViewControl.UpdateAnnotFrame();
  233. }
  234. }
  235. }
  236. public void SetFontStyle(bool isBold, bool isItalic)
  237. {
  238. if (isBold == false && isItalic == false)
  239. {
  240. FontStyleCombox.SelectedIndex = 0;
  241. return;
  242. }
  243. if (isBold && isItalic == false)
  244. {
  245. FontStyleCombox.SelectedIndex = 1;
  246. return;
  247. }
  248. if (isBold == false && isItalic)
  249. {
  250. FontStyleCombox.SelectedIndex = 2;
  251. return;
  252. }
  253. if (isBold && isItalic)
  254. {
  255. FontStyleCombox.SelectedIndex = 3;
  256. }
  257. }
  258. private void SetFontSize(double size)
  259. {
  260. int index = SizeList.IndexOf((int)size);
  261. FontSizeComboBox.SelectedIndex = index;
  262. }
  263. public void SetFontName(string fontName)
  264. {
  265. foreach (ComboBoxItem item in FontCombox.Items)
  266. {
  267. if (item.Content.ToString() == fontName)
  268. {
  269. FontCombox.SelectedItem = item;
  270. break;
  271. }
  272. }
  273. }
  274. public void SetAnnotParam(PolygonMeasureParam param, CPDFAnnotation annot, PDFViewControl viewControl)
  275. {
  276. Annotation = annot as CPDFPolygonAnnotation;
  277. ViewControl = viewControl;
  278. polygonMeasureParam = param;
  279. if (param == null)
  280. {
  281. return;
  282. }
  283. Color lineColor = Color.FromRgb(param.LineColor[0], param.LineColor[1], param.LineColor[2]);
  284. BorderColorPickerControl.SetCheckedForColor(lineColor);
  285. if (polygonMeasureParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  286. {
  287. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  288. }
  289. else
  290. {
  291. List<double> dashArray = new List<double>();
  292. foreach (double num in param.LineDash)
  293. {
  294. dashArray.Add(num);
  295. }
  296. CPDFLineStyleControl.DashStyle = new DashStyle(dashArray, 0);
  297. }
  298. double opacity = param.Transparency / 255.0 * 100.0;
  299. CPDFOpacityControl.OpacityValue = (int)Math.Ceiling(opacity);
  300. NoteTextBox.Text = param.Content;
  301. SetFontSize(param.FontSize);
  302. SetFontStyle(param.IsBold,param.IsItalic);
  303. SetFontName(param.FontName);
  304. }
  305. }
  306. }