PolygonalProperty.xaml.cs 12 KB

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