PolygonalProperty.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. SolidColorBrush checkBrush = BorderColorPickerControl.GetBrush() as SolidColorBrush;
  146. if (checkBrush != null)
  147. {
  148. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  149. Annotation.SetLineColor(color);
  150. Annotation.UpdateAp();
  151. ViewControl.UpdateAnnotFrame();
  152. }
  153. }
  154. }
  155. }
  156. private void FillColorPickerControl_ColorChanged(object sender, EventArgs e)
  157. {
  158. if (IsLoadedData)
  159. {
  160. if (Annotation != null && ViewControl != null)
  161. {
  162. SolidColorBrush checkBrush = FillColorPickerControl.GetBrush() as SolidColorBrush;
  163. if (checkBrush != null)
  164. {
  165. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  166. Annotation.SetBgColor(color);
  167. Annotation.UpdateAp();
  168. ViewControl.UpdateAnnotFrame();
  169. }
  170. }
  171. }
  172. }
  173. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  174. {
  175. if (IsLoadedData)
  176. {
  177. if (Annotation != null && ViewControl != null)
  178. {
  179. double opacity = CPDFOpacityControl.OpacityValue / 100.0;
  180. if (opacity > 0 && opacity <= 1)
  181. {
  182. opacity = opacity * 255;
  183. }
  184. if (Math.Abs(opacity - Annotation.GetTransparency()) > 0.01)
  185. {
  186. Annotation.SetTransparency((byte)opacity);
  187. Annotation.UpdateAp();
  188. ViewControl.UpdateAnnotFrame();
  189. }
  190. }
  191. }
  192. }
  193. private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
  194. {
  195. if (!IsLoadedData) return;
  196. if (Annotation != null && ViewControl != null)
  197. {
  198. float[] dashArray = null;
  199. C_BORDER_STYLE borderStyle;
  200. if (CPDFLineStyleControl.DashStyle == DashStyles.Solid || CPDFLineStyleControl.DashStyle == null)
  201. {
  202. dashArray = new float[0];
  203. borderStyle = C_BORDER_STYLE.BS_SOLID;
  204. }
  205. else
  206. {
  207. List<float> floatArray = new List<float>();
  208. foreach (double num in CPDFLineStyleControl.DashStyle.Dashes)
  209. {
  210. floatArray.Add((float)num);
  211. }
  212. dashArray = floatArray.ToArray();
  213. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  214. }
  215. Annotation.SetBorderStyle(borderStyle, dashArray);
  216. Annotation.UpdateAp();
  217. ViewControl.UpdateAnnotFrame();
  218. }
  219. }
  220. private void FontColorPickerControl_ColorChanged(object sender, EventArgs e)
  221. {
  222. if (!IsLoadedData) return;
  223. SolidColorBrush checkBrush = FontColorPickerControl.GetBrush() as SolidColorBrush;
  224. if (checkBrush != null && Annotation != null && ViewControl != null)
  225. {
  226. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  227. if (Annotation != null)
  228. {
  229. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  230. textAttribute.FontColor = color;
  231. Annotation.SetTextAttribute(textAttribute);
  232. Annotation.UpdateAp();
  233. ViewControl.UpdateAnnotFrame();
  234. }
  235. }
  236. }
  237. public void SetFontStyle(bool isBold, bool isItalic)
  238. {
  239. if (isBold == false && isItalic == false)
  240. {
  241. FontStyleCombox.SelectedIndex = 0;
  242. return;
  243. }
  244. if (isBold && isItalic == false)
  245. {
  246. FontStyleCombox.SelectedIndex = 1;
  247. return;
  248. }
  249. if (isBold == false && isItalic)
  250. {
  251. FontStyleCombox.SelectedIndex = 2;
  252. return;
  253. }
  254. if (isBold && isItalic)
  255. {
  256. FontStyleCombox.SelectedIndex = 3;
  257. }
  258. }
  259. private void SetFontSize(double size)
  260. {
  261. int index = SizeList.IndexOf((int)size);
  262. FontSizeComboBox.SelectedIndex = index;
  263. }
  264. public void SetFontName(string fontName)
  265. {
  266. foreach (ComboBoxItem item in FontCombox.Items)
  267. {
  268. if (item.Content.ToString() == fontName)
  269. {
  270. FontCombox.SelectedItem = item;
  271. break;
  272. }
  273. }
  274. }
  275. public void SetAnnotParam(PolygonMeasureParam param, CPDFAnnotation annot, PDFViewControl viewControl)
  276. {
  277. Annotation = annot as CPDFPolygonAnnotation;
  278. ViewControl = viewControl;
  279. polygonMeasureParam = param;
  280. if (param == null)
  281. {
  282. return;
  283. }
  284. Color lineColor = Color.FromRgb(param.LineColor[0], param.LineColor[1], param.LineColor[2]);
  285. BorderColorPickerControl.SetCheckedForColor(lineColor);
  286. if (polygonMeasureParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  287. {
  288. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  289. }
  290. else
  291. {
  292. List<double> dashArray = new List<double>();
  293. foreach (double num in param.LineDash)
  294. {
  295. dashArray.Add(num);
  296. }
  297. CPDFLineStyleControl.DashStyle = new DashStyle(dashArray, 0);
  298. }
  299. double opacity = param.Transparency / 255.0 * 100.0;
  300. CPDFOpacityControl.OpacityValue = (int)Math.Ceiling(opacity);
  301. NoteTextBox.Text = param.Content;
  302. SetFontSize(param.FontSize);
  303. SetFontStyle(param.IsBold,param.IsItalic);
  304. SetFontName(param.FontName);
  305. }
  306. }
  307. }