StraightnessProperty.xaml.cs 13 KB

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