TextFieldProperty.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFAnnotation.Form;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Tool.Help;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Media;
  13. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  14. namespace Compdfkit_Tools.PDFControl
  15. {
  16. public partial class TextFieldProperty : UserControl
  17. {
  18. private TextBoxParam widgetParam = null;
  19. private CPDFTextWidget cPDFAnnotation = null;
  20. private PDFViewControl pdfViewerControl = null;
  21. private CPDFDocument cPDFDocument = null;
  22. public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
  23. {
  24. 6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
  25. };
  26. bool IsLoadedData = false;
  27. public TextFieldProperty()
  28. {
  29. InitializeComponent();
  30. }
  31. #region Loaded
  32. public void SetProperty(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
  33. {
  34. widgetParam = (TextBoxParam)annotParam;
  35. cPDFAnnotation = (CPDFTextWidget)annotation;
  36. pdfViewerControl = cPDFViewer;
  37. cPDFDocument = doc;
  38. }
  39. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  40. {
  41. Binding SizeListbinding = new Binding();
  42. SizeListbinding.Source = this;
  43. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  44. FontSizeCombox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  45. FieldNameText.Text = widgetParam.FieldName;
  46. FormFieldCombox.SelectedIndex = (int)ParamConverter.ConverterWidgetFormFlags(widgetParam.Flags, widgetParam.IsHidden);
  47. BorderColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.LineColor));
  48. BackgroundColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.BgColor));
  49. TextColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.FontColor));
  50. SetFontName(widgetParam.FontName);
  51. SetFontStyle(widgetParam.IsItalic, widgetParam.IsBold);
  52. SetFontSize(widgetParam.FontSize);
  53. TextAlignmentCombox.SelectedIndex = (int)widgetParam.Alignment;
  54. DefaultText.Text = widgetParam.Text;
  55. chkMutiline.IsChecked = widgetParam.IsMultiLine;
  56. IsLoadedData = true;
  57. }
  58. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  59. {
  60. IsLoadedData = false;
  61. }
  62. private void SetFontSize(double size)
  63. {
  64. int index = SizeList.IndexOf((int)size);
  65. FontSizeCombox.SelectedIndex = index;
  66. }
  67. private void SetFontStyle(bool IsItalic, bool IsBold)
  68. {
  69. int index = 0;
  70. if (IsItalic && IsBold)
  71. {
  72. index = 3;
  73. }
  74. else if (IsItalic)
  75. {
  76. index = 2;
  77. }
  78. else if (IsBold)
  79. {
  80. index = 1;
  81. }
  82. FontStyleCombox.SelectedIndex = index;
  83. }
  84. private void SetFontName(string fontName)
  85. {
  86. int index = -1;
  87. List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
  88. for (int i = 0; i < fontFamilyList.Count; i++)
  89. {
  90. if (fontFamilyList[i].ToLower().Contains(fontName.ToLower())
  91. || fontName.ToLower().Contains(fontFamilyList[i].ToLower()))
  92. {
  93. index = i;
  94. }
  95. }
  96. FontCombox.SelectedIndex = index;
  97. }
  98. #endregion
  99. #region Updata
  100. private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
  101. {
  102. if (IsLoadedData)
  103. {
  104. cPDFAnnotation.SetFieldName((sender as TextBox).Text);
  105. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  106. }
  107. }
  108. private void FormFieldCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  109. {
  110. if (IsLoadedData)
  111. {
  112. ParamConverter.SetFormField((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation);
  113. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  114. }
  115. }
  116. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  117. {
  118. if (IsLoadedData)
  119. {
  120. byte[] Color = new byte[3];
  121. Color[0] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.R;
  122. Color[1] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.G;
  123. Color[2] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.B;
  124. cPDFAnnotation.SetWidgetBorderRGBColor(Color);
  125. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  126. }
  127. }
  128. private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
  129. {
  130. if (IsLoadedData)
  131. {
  132. byte[] Color = new byte[3];
  133. Color[0] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.R;
  134. Color[1] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.G;
  135. Color[2] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.B;
  136. cPDFAnnotation.SetWidgetBgRGBColor(Color);
  137. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  138. }
  139. }
  140. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  141. {
  142. if (IsLoadedData)
  143. {
  144. byte[] Color = new byte[3];
  145. Color[0] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.R;
  146. Color[1] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.G;
  147. Color[2] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.B;
  148. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  149. cTextAttribute.FontColor = Color;
  150. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  151. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  152. }
  153. }
  154. private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  155. {
  156. if (IsLoadedData)
  157. {
  158. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  159. bool isBold = IsBold(cTextAttribute.FontName);
  160. bool isItalic = IsItalic(cTextAttribute.FontName);
  161. FontType fontType = GetFontType(cTextAttribute.FontName);
  162. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  163. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  164. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  165. }
  166. }
  167. private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  168. {
  169. if (IsLoadedData)
  170. {
  171. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  172. bool isItalic = false;
  173. bool isBold = false;
  174. switch ((sender as ComboBox).SelectedIndex)
  175. {
  176. case 0:
  177. break;
  178. case 1:
  179. isItalic = IsItalic(cTextAttribute.FontName);
  180. isBold = true;
  181. break;
  182. case 2:
  183. isItalic = true;
  184. isBold = IsBold(cTextAttribute.FontName);
  185. break;
  186. case 3:
  187. isItalic = true;
  188. isBold = true;
  189. break;
  190. default:
  191. break;
  192. }
  193. FontType fontType = GetFontType(cTextAttribute.FontName);
  194. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  195. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  196. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  197. }
  198. }
  199. private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  200. {
  201. if (IsLoadedData)
  202. {
  203. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  204. cTextAttribute.FontSize = (float)(sender as ComboBox).SelectedItem;
  205. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  206. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  207. }
  208. }
  209. private void TextAlignmentCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  210. {
  211. if (IsLoadedData)
  212. {
  213. cPDFAnnotation.SetJustification((C_TEXT_ALIGNMENT)(sender as ComboBox).SelectedIndex);
  214. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  215. }
  216. }
  217. private void DefaultText_TextChanged(object sender, TextChangedEventArgs e)
  218. {
  219. if (IsLoadedData)
  220. {
  221. cPDFAnnotation.SetText((sender as TextBox).Text);
  222. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  223. }
  224. }
  225. private void chkMutiline_Checked(object sender, RoutedEventArgs e)
  226. {
  227. if (IsLoadedData)
  228. {
  229. cPDFAnnotation.SetMultiLine(true);
  230. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  231. }
  232. }
  233. private void chkMutiline_Unchecked(object sender, RoutedEventArgs e)
  234. {
  235. if (IsLoadedData)
  236. {
  237. cPDFAnnotation.SetMultiLine(false);
  238. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  239. }
  240. }
  241. #endregion
  242. }
  243. }