StraightnessProperty.xaml.cs 15 KB

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