MultilineProperty.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Controls.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.Controls.PDFControl;
  20. using ComPDFKit.Tool;
  21. using CFontNameHelper = ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  22. using System.Runtime.CompilerServices;
  23. using System.IO.Ports;
  24. namespace ComPDFKit.Controls.Measure.Property
  25. {
  26. public partial class MultilineProperty : UserControl
  27. {
  28. //private AnnotAttribEvent MultilineEvent { 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 PolyLineMeasureParam polyLineMeasureParam;
  35. public CPDFPolylineAnnotation Annotation { get; set; }
  36. public PDFViewControl ViewControl { get; set; }
  37. public event EventHandler<PolyLineMeasureParam> PolyLineMeasureParamChanged;
  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)
  47. {
  48. polyLineMeasureParam.Content = NoteTextBox.Text;
  49. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  50. }
  51. if (Annotation != null && ViewControl != null)
  52. {
  53. Annotation.SetContent(NoteTextBox.Text);
  54. Annotation.UpdateAp();
  55. ViewControl?.UpdateAnnotFrame();
  56. }
  57. }
  58. }
  59. private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  60. {
  61. if (!IsLoadedData) return;
  62. int selectIndex = Math.Max(0, FontStyleCombox.SelectedIndex);
  63. bool isBold = false;
  64. bool isItalic = false;
  65. switch (selectIndex)
  66. {
  67. case 0:
  68. isBold = false;
  69. isItalic = false;
  70. break;
  71. case 1:
  72. isBold = true;
  73. isItalic = false;
  74. break;
  75. case 2:
  76. isBold = false;
  77. isItalic = true;
  78. break;
  79. case 3:
  80. isBold = true;
  81. isItalic = true;
  82. break;
  83. default:
  84. break;
  85. }
  86. if (Annotation == null)
  87. {
  88. polyLineMeasureParam.IsBold = isBold;
  89. polyLineMeasureParam.IsItalic = isItalic;
  90. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  91. }
  92. if (Annotation != null)
  93. {
  94. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  95. var fontType = CFontNameHelper.GetFontType((FontCombox.SelectedItem as ComboBoxItem).Content.ToString());
  96. var newName = CFontNameHelper.ObtainFontName(fontType, isBold, isItalic);
  97. textAttribute.FontName = newName;
  98. Annotation.SetTextAttribute(textAttribute);
  99. Annotation.UpdateAp();
  100. ViewControl?.UpdateAnnotFrame();
  101. }
  102. }
  103. private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  104. {
  105. if (IsLoadedData)
  106. {
  107. ComboBoxItem selectItem = FontCombox.SelectedItem as ComboBoxItem;
  108. if (Annotation == null)
  109. {
  110. var fontType = CFontNameHelper.GetFontType(selectItem.Content.ToString());
  111. string FontName = CFontNameHelper.ObtainFontName(fontType, polyLineMeasureParam.IsBold, polyLineMeasureParam.IsItalic);
  112. polyLineMeasureParam.FontName = FontName;
  113. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  114. }
  115. if (Annotation != null && ViewControl != null)
  116. {
  117. if (selectItem != null && selectItem.Content != null)
  118. {
  119. CTextAttribute textAttr = Annotation.GetTextAttribute();
  120. bool isBold = CFontNameHelper.IsBold(textAttr.FontName);
  121. bool isItalic = CFontNameHelper.IsItalic(textAttr.FontName);
  122. var fontType = CFontNameHelper.GetFontType(selectItem.Content.ToString());
  123. textAttr.FontName = CFontNameHelper.ObtainFontName(fontType, isBold, isItalic);
  124. Annotation.SetTextAttribute(textAttr);
  125. Annotation.UpdateAp();
  126. ViewControl.UpdateAnnotFrame();
  127. }
  128. }
  129. }
  130. }
  131. private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  132. {
  133. if (IsLoadedData)
  134. {
  135. if (FontSizeComboBox.SelectedItem != null)
  136. {
  137. if (Annotation == null)
  138. {
  139. polyLineMeasureParam.FontSize = (float)Convert.ToDouble(FontSizeComboBox.SelectedItem);
  140. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  141. }
  142. if (Annotation != null && ViewControl != null)
  143. {
  144. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  145. textAttribute.FontSize = (float)Convert.ToDouble(FontSizeComboBox.SelectedItem);
  146. Annotation.SetTextAttribute(textAttribute);
  147. Annotation.UpdateAp();
  148. ViewControl?.UpdateAnnotFrame();
  149. }
  150. }
  151. }
  152. }
  153. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  154. {
  155. if (IsLoadedData)
  156. {
  157. SolidColorBrush checkBrush = BorderColorPickerControl.GetBrush() as SolidColorBrush;
  158. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  159. if (Annotation == null)
  160. {
  161. polyLineMeasureParam.LineColor = color;
  162. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  163. }
  164. if (Annotation != null && ViewControl != null)
  165. {
  166. if (checkBrush != null)
  167. {
  168. Annotation.SetLineColor(color);
  169. Annotation.UpdateAp();
  170. ViewControl.UpdateAnnotFrame();
  171. }
  172. }
  173. }
  174. }
  175. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  176. {
  177. if (IsLoadedData)
  178. {
  179. double opacity = CPDFOpacityControl.OpacityValue / 100.0;
  180. if (Annotation == null)
  181. {
  182. polyLineMeasureParam.Transparency = (byte)(opacity * 255);
  183. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  184. }
  185. if (Annotation != null && ViewControl != null)
  186. {
  187. if (opacity > 0 && opacity <= 1)
  188. {
  189. opacity = opacity * 255;
  190. }
  191. if (Math.Abs(opacity - Annotation.GetTransparency()) > 0.01)
  192. {
  193. Annotation.SetTransparency((byte)opacity);
  194. Annotation.UpdateAp();
  195. ViewControl.UpdateAnnotFrame();
  196. }
  197. }
  198. }
  199. }
  200. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  201. {
  202. if (IsLoadedData)
  203. {
  204. if (Annotation == null)
  205. {
  206. polyLineMeasureParam.LineWidth = (byte)CPDFThicknessControl.Thickness;
  207. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  208. }
  209. if (Annotation != null && ViewControl != null)
  210. {
  211. Annotation.SetLineWidth(CPDFThicknessControl.Thickness);
  212. Annotation.UpdateAp();
  213. ViewControl.UpdateAnnotFrame();
  214. }
  215. }
  216. }
  217. private void CPDFLineStyleControl_LineStyleChanged(object sender, EventArgs e)
  218. {
  219. if (!IsLoadedData) return;
  220. float[] dashArray = null;
  221. C_BORDER_STYLE borderStyle;
  222. if (CPDFLineStyleControl.DashStyle == DashStyles.Solid || CPDFLineStyleControl.DashStyle == null)
  223. {
  224. dashArray = new float[0];
  225. borderStyle = C_BORDER_STYLE.BS_SOLID;
  226. }
  227. else
  228. {
  229. List<float> floatArray = new List<float>();
  230. foreach (double num in CPDFLineStyleControl.DashStyle.Dashes)
  231. {
  232. floatArray.Add((float)num);
  233. }
  234. dashArray = floatArray.ToArray();
  235. borderStyle = C_BORDER_STYLE.BS_DASHDED;
  236. }
  237. if (Annotation == null)
  238. {
  239. polyLineMeasureParam.BorderStyle = borderStyle;
  240. polyLineMeasureParam.LineDash = dashArray;
  241. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  242. }
  243. if (Annotation != null && ViewControl != null)
  244. {
  245. Annotation.SetBorderStyle(borderStyle, dashArray);
  246. Annotation.UpdateAp();
  247. ViewControl.UpdateAnnotFrame();
  248. }
  249. }
  250. private void FontColorPickerControl_ColorChanged(object sender, EventArgs e)
  251. {
  252. if (!IsLoadedData) return;
  253. SolidColorBrush checkBrush = FontColorPickerControl.GetBrush() as SolidColorBrush;
  254. if (checkBrush != null)
  255. {
  256. byte[] color = { checkBrush.Color.R, checkBrush.Color.G, checkBrush.Color.B };
  257. if (Annotation == null)
  258. {
  259. polyLineMeasureParam.FontColor = color;
  260. PolyLineMeasureParamChanged?.Invoke(sender, polyLineMeasureParam);
  261. }
  262. if (Annotation != null && ViewControl != null)
  263. {
  264. if (Annotation != null)
  265. {
  266. CTextAttribute textAttribute = Annotation.GetTextAttribute();
  267. textAttribute.FontColor = color;
  268. Annotation.SetTextAttribute(textAttribute);
  269. Annotation.UpdateAp();
  270. ViewControl.UpdateAnnotFrame();
  271. }
  272. }
  273. }
  274. }
  275. public void SetFontStyle(bool isBold, bool isItalic)
  276. {
  277. if (isBold == false && isItalic == false)
  278. {
  279. FontStyleCombox.SelectedIndex = 0;
  280. return;
  281. }
  282. if (isBold && isItalic == false)
  283. {
  284. FontStyleCombox.SelectedIndex = 1;
  285. return;
  286. }
  287. if (isBold == false && isItalic)
  288. {
  289. FontStyleCombox.SelectedIndex = 2;
  290. return;
  291. }
  292. if (isBold && isItalic)
  293. {
  294. FontStyleCombox.SelectedIndex = 3;
  295. }
  296. }
  297. private void SetFontSize(double size)
  298. {
  299. int index = SizeList.IndexOf((int)size);
  300. FontSizeComboBox.SelectedIndex = index;
  301. }
  302. public void SetFontName(string fontName)
  303. {
  304. foreach (ComboBoxItem item in FontCombox.Items)
  305. {
  306. if (item.Content.ToString() == fontName)
  307. {
  308. FontCombox.SelectedItem = item;
  309. break;
  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. public void SetAnnotParam(PolyLineMeasureParam param, CPDFAnnotation annot, PDFViewControl viewControl)
  326. {
  327. Annotation = annot as CPDFPolylineAnnotation;
  328. ViewControl = viewControl;
  329. polyLineMeasureParam = param;
  330. if (param == null)
  331. {
  332. return;
  333. }
  334. Color lineColor = Color.FromRgb(param.LineColor[0], param.LineColor[1], param.LineColor[2]);
  335. BorderColorPickerControl.SetCheckedForColor(lineColor);
  336. CPDFThicknessControl.Thickness = (int)param.LineWidth;
  337. if (polyLineMeasureParam.BorderStyle == C_BORDER_STYLE.BS_SOLID)
  338. {
  339. CPDFLineStyleControl.DashStyle = DashStyles.Solid;
  340. }
  341. else
  342. {
  343. List<double> dashArray = new List<double>();
  344. foreach (double num in param.LineDash)
  345. {
  346. dashArray.Add(num);
  347. }
  348. CPDFLineStyleControl.DashStyle = new DashStyle(dashArray, 0);
  349. }
  350. double opacity = param.Transparency / 255.0 * 100.0;
  351. CPDFOpacityControl.OpacityValue = (int)Math.Ceiling(opacity);
  352. NoteTextBox.Text = param.Content;
  353. SetFontSize(param.FontSize);
  354. SetFontStyle(param.IsBold, param.IsItalic);
  355. SetFontName(param.FontName);
  356. }
  357. }
  358. }