PolygonalProperty.xaml.cs 15 KB

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