StraightnessProperty.xaml.cs 13 KB

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