MultilineProperty.xaml.cs 12 KB

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