StraightnessProperty.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. CPDFLineStyleControl.DashStyle = DashStyles.Dash;
  188. }
  189. LineType lineType = new LineType()
  190. {
  191. HeadLineType = param.HeadLineType,
  192. TailLineType = param.TailLineType
  193. };
  194. CPDFArrowControl.LineType = lineType;
  195. double opacity = param.Transparency / 255.0 * 100.0;
  196. CPDFOpacityControl.OpacityValue = (int)Math.Ceiling(opacity);
  197. NoteTextBox.Text = param.Content;
  198. SetFontSize(param.FontSize);
  199. SetFontStyle(param.IsBold,param.IsItalic);
  200. SetFontName(param.FontName);
  201. }
  202. public void SetFontName(string fontName)
  203. {
  204. foreach (ComboBoxItem item in FontCombox.Items)
  205. {
  206. if (item.Content.ToString() == fontName)
  207. {
  208. FontCombox.SelectedItem = item;
  209. break;
  210. }
  211. }
  212. }
  213. public void SetFontStyle(bool isBold, bool isItalic)
  214. {
  215. if (isBold == false && isItalic == false)
  216. {
  217. FontStyleCombox.SelectedIndex = 0;
  218. return;
  219. }
  220. if (isBold && isItalic == false)
  221. {
  222. FontStyleCombox.SelectedIndex = 1;
  223. return;
  224. }
  225. if (isBold == false && isItalic)
  226. {
  227. FontStyleCombox.SelectedIndex = 2;
  228. return;
  229. }
  230. if (isBold && isItalic)
  231. {
  232. FontStyleCombox.SelectedIndex = 3;
  233. }
  234. }
  235. private void SetFontSize(double size)
  236. {
  237. int index = SizeList.IndexOf((int)size);
  238. FontSizeComboBox.SelectedIndex = index;
  239. }
  240. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  241. {
  242. if (IsLoadedData)
  243. {
  244. if (Annotation != null && ViewControl != null)
  245. {
  246. Annotation.SetLineWidth(CPDFThicknessControl.Thickness);
  247. Annotation.UpdateAp();
  248. ViewControl.UpdateAnnotFrame();
  249. }
  250. }
  251. }
  252. private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
  253. {
  254. if (!IsLoadedData) return;
  255. if (Annotation != null && ViewControl != null)
  256. {
  257. float[] dashArray = null;
  258. C_BORDER_STYLE borderStyle;
  259. if (CPDFLineStyleControl.DashStyle == DashStyles.Solid || CPDFLineStyleControl.DashStyle == null)
  260. {
  261. dashArray = new float[0];
  262. borderStyle = C_BORDER_STYLE.BS_SOLID;
  263. }
  264. else
  265. {
  266. List<float> floatArray = new List<float>();
  267. foreach (double num in CPDFLineStyleControl.DashStyle.Dashes)
  268. {
  269. floatArray.Add((float)num);
  270. }
  271. dashArray = floatArray.ToArray();
  272. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  273. }
  274. Annotation.SetBorderStyle(borderStyle, dashArray);
  275. Annotation.UpdateAp();
  276. ViewControl.UpdateAnnotFrame();
  277. }
  278. }
  279. private void CPDFArrowControl_ArrowChanged(object sender, EventArgs e)
  280. {
  281. if (!IsLoadedData) return;
  282. if (Annotation != null && ViewControl != null)
  283. {
  284. LineType lineType = new LineType()
  285. {
  286. HeadLineType = CPDFArrowControl.LineType.HeadLineType,
  287. TailLineType = CPDFArrowControl.LineType.TailLineType
  288. };
  289. Annotation.SetLineType(lineType.HeadLineType, lineType.TailLineType);
  290. Annotation.UpdateAp();
  291. ViewControl.UpdateAnnotFrame();
  292. }
  293. }
  294. private void FontColorPickerControl_ColorChanged(object sender, EventArgs e)
  295. {
  296. if (!IsLoadedData) return;
  297. SolidColorBrush checkBrush = FontColorPickerControl.GetBrush() as SolidColorBrush;
  298. if (checkBrush != null && Annotation != null && ViewControl != null)
  299. {
  300. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  301. if (Annotation != null)
  302. {
  303. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  304. textAttribute.FontColor = color;
  305. Annotation.SetTextAttribute(textAttribute);
  306. Annotation.UpdateAp();
  307. ViewControl.UpdateAnnotFrame();
  308. }
  309. }
  310. }
  311. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  312. {
  313. Binding SizeListbinding = new Binding();
  314. SizeListbinding.Source = this;
  315. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  316. FontSizeComboBox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  317. IsLoadedData = true;
  318. }
  319. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  320. {
  321. IsLoadedData = false;
  322. }
  323. }
  324. }