PolygonalProperty.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. if (checkBrush.Color == Colors.Transparent)
  209. {
  210. Annotation.ClearBgColor();
  211. }
  212. else
  213. {
  214. Annotation.SetBgColor(color);
  215. }
  216. Annotation.UpdateAp();
  217. ViewControl.UpdateAnnotFrame();
  218. }
  219. }
  220. }
  221. }
  222. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  223. {
  224. if (IsLoadedData)
  225. {
  226. double opacity = CPDFOpacityControl.OpacityValue / 100.0;
  227. if (opacity > 0 && opacity <= 1)
  228. {
  229. opacity = opacity * 255;
  230. }
  231. if (Annotation == null)
  232. {
  233. polygonMeasureParam.Transparency = (byte)opacity;
  234. PolygonMeasureParamChanged?.Invoke(sender, polygonMeasureParam);
  235. }
  236. if (Annotation != null && ViewControl != null)
  237. {
  238. if (Math.Abs(opacity - Annotation.GetTransparency()) > 0.01)
  239. {
  240. Annotation.SetTransparency((byte)opacity);
  241. Annotation.UpdateAp();
  242. ViewControl.UpdateAnnotFrame();
  243. }
  244. }
  245. }
  246. }
  247. private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
  248. {
  249. if (!IsLoadedData) return;
  250. float[] dashArray = null;
  251. C_BORDER_STYLE borderStyle;
  252. if (CPDFLineStyleControl.DashStyle == DashStyles.Solid || CPDFLineStyleControl.DashStyle == null)
  253. {
  254. dashArray = new float[0];
  255. borderStyle = C_BORDER_STYLE.BS_SOLID;
  256. }
  257. else
  258. {
  259. List<float> floatArray = new List<float>();
  260. foreach (double num in CPDFLineStyleControl.DashStyle.Dashes)
  261. {
  262. floatArray.Add((float)num);
  263. }
  264. dashArray = floatArray.ToArray();
  265. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  266. }
  267. if (Annotation == null)
  268. {
  269. polygonMeasureParam.BorderStyle = borderStyle;
  270. polygonMeasureParam.LineDash = dashArray;
  271. PolygonMeasureParamChanged?.Invoke(sender, polygonMeasureParam);
  272. }
  273. if (Annotation != null && ViewControl != null)
  274. {
  275. Annotation.SetBorderStyle(borderStyle, dashArray);
  276. Annotation.UpdateAp();
  277. ViewControl.UpdateAnnotFrame();
  278. }
  279. }
  280. private void FontColorPickerControl_ColorChanged(object sender, EventArgs e)
  281. {
  282. if (!IsLoadedData) return;
  283. SolidColorBrush checkBrush = FontColorPickerControl.GetBrush() as SolidColorBrush;
  284. if (checkBrush != null)
  285. {
  286. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  287. if (Annotation == null)
  288. {
  289. polygonMeasureParam.FontColor = color;
  290. PolygonMeasureParamChanged?.Invoke(sender, polygonMeasureParam);
  291. }
  292. if (Annotation != null && ViewControl != null)
  293. {
  294. if (Annotation != null)
  295. {
  296. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  297. textAttribute.FontColor = color;
  298. Annotation.SetTextAttribute(textAttribute);
  299. Annotation.UpdateAp();
  300. ViewControl.UpdateAnnotFrame();
  301. }
  302. }
  303. }
  304. }
  305. public void SetFontStyle(bool isBold, bool isItalic)
  306. {
  307. if (isBold == false && isItalic == false)
  308. {
  309. FontStyleCombox.SelectedIndex = 0;
  310. return;
  311. }
  312. if (isBold && isItalic == false)
  313. {
  314. FontStyleCombox.SelectedIndex = 1;
  315. return;
  316. }
  317. if (isBold == false && isItalic)
  318. {
  319. FontStyleCombox.SelectedIndex = 2;
  320. return;
  321. }
  322. if (isBold && isItalic)
  323. {
  324. FontStyleCombox.SelectedIndex = 3;
  325. }
  326. }
  327. private void SetFontSize(double size)
  328. {
  329. int index = SizeList.IndexOf((int)size);
  330. FontSizeComboBox.SelectedIndex = index;
  331. }
  332. public void SetFontName(string fontName)
  333. {
  334. foreach (ComboBoxItem item in FontCombox.Items)
  335. {
  336. if (item.Content.ToString() == fontName)
  337. {
  338. FontCombox.SelectedItem = item;
  339. break;
  340. }
  341. }
  342. }
  343. public void SetAnnotParam(PolygonMeasureParam param, CPDFAnnotation annot, PDFViewControl viewControl)
  344. {
  345. Annotation = annot as CPDFPolygonAnnotation;
  346. ViewControl = viewControl;
  347. polygonMeasureParam = param;
  348. if (param == null)
  349. {
  350. return;
  351. }
  352. Color lineColor = Color.FromRgb(param.LineColor[0], param.LineColor[1], param.LineColor[2]);
  353. BorderColorPickerControl.SetCheckedForColor(lineColor);
  354. if(Annotation != null)
  355. {
  356. if (!Annotation.HasBgColor)
  357. {
  358. FillColorPickerControl.SetCheckedForColor(Colors.Transparent);
  359. }
  360. else
  361. {
  362. Color fillColor = Color.FromRgb(param.FillColor[0], param.FillColor[1], param.FillColor[2]);
  363. FillColorPickerControl.SetCheckedForColor(fillColor);
  364. }
  365. }
  366. else
  367. {
  368. if (!param.HasFillColor)
  369. {
  370. FillColorPickerControl.SetCheckedForColor(Colors.Transparent);
  371. }
  372. else
  373. {
  374. Color fillColor = Color.FromRgb(param.FillColor[0], param.FillColor[1], param.FillColor[2]);
  375. FillColorPickerControl.SetCheckedForColor(fillColor);
  376. }
  377. }
  378. Color FontColor = Color.FromRgb(param.FontColor[0], param.FontColor[1], param.FontColor[2]);
  379. FontColorPickerControl.SetCheckedForColor(FontColor);
  380. if (polygonMeasureParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  381. {
  382. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  383. }
  384. else
  385. {
  386. List<double> dashArray = new List<double>();
  387. foreach (double num in param.LineDash)
  388. {
  389. dashArray.Add(num);
  390. }
  391. CPDFLineStyleControl.DashStyle = new DashStyle(dashArray, 0);
  392. }
  393. double opacity = param.Transparency / 255.0 * 100.0;
  394. CPDFOpacityControl.OpacityValue = (int)Math.Ceiling(opacity);
  395. NoteTextBox.Text = param.Content;
  396. SetFontSize(param.FontSize);
  397. SetFontStyle(param.IsBold, param.IsItalic);
  398. SetFontName(param.FontName);
  399. }
  400. }
  401. }