TextFieldProperty.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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().UpDateAnnotFrame();
  106. }
  107. }
  108. private void FormFieldCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  109. {
  110. if (IsLoadedData)
  111. {
  112. cPDFAnnotation.SetFlags(ParamConverter.GetFormFlags((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation));
  113. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  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().UpDateAnnotFrame();
  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().UpDateAnnotFrame();
  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. cPDFAnnotation.UpdateFormAp();
  152. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  153. }
  154. }
  155. private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  156. {
  157. if (IsLoadedData)
  158. {
  159. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  160. bool isBold = IsBold(cTextAttribute.FontName);
  161. bool isItalic = IsItalic(cTextAttribute.FontName);
  162. FontType fontType = GetFontType((sender as ComboBox).SelectedItem?.ToString());
  163. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  164. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  165. cPDFAnnotation.UpdateFormAp();
  166. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  167. }
  168. }
  169. private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  170. {
  171. if (IsLoadedData)
  172. {
  173. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  174. bool isItalic = false;
  175. bool isBold = false;
  176. switch ((sender as ComboBox).SelectedIndex)
  177. {
  178. case 0:
  179. break;
  180. case 1:
  181. isItalic = false;
  182. isBold = true;
  183. break;
  184. case 2:
  185. isItalic = true;
  186. isBold = false;
  187. break;
  188. case 3:
  189. isItalic = true;
  190. isBold = true;
  191. break;
  192. default:
  193. break;
  194. }
  195. FontType fontType = GetFontType(cTextAttribute.FontName);
  196. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  197. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  198. cPDFAnnotation.UpdateFormAp();
  199. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  200. }
  201. }
  202. private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  203. {
  204. if (IsLoadedData)
  205. {
  206. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  207. cTextAttribute.FontSize = Convert.ToSingle((sender as ComboBox).SelectedItem);
  208. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  209. cPDFAnnotation.UpdateFormAp();
  210. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  211. }
  212. }
  213. private void TextAlignmentCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  214. {
  215. if (IsLoadedData)
  216. {
  217. cPDFAnnotation.SetJustification((C_TEXT_ALIGNMENT)(sender as ComboBox).SelectedIndex);
  218. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  219. }
  220. }
  221. private void DefaultText_TextChanged(object sender, TextChangedEventArgs e)
  222. {
  223. if (IsLoadedData)
  224. {
  225. cPDFAnnotation.SetText((sender as TextBox).Text);
  226. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  227. }
  228. }
  229. private void chkMutiline_Checked(object sender, RoutedEventArgs e)
  230. {
  231. if (IsLoadedData)
  232. {
  233. cPDFAnnotation.SetMultiLine(true);
  234. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  235. }
  236. }
  237. private void chkMutiline_Unchecked(object sender, RoutedEventArgs e)
  238. {
  239. if (IsLoadedData)
  240. {
  241. cPDFAnnotation.SetMultiLine(false);
  242. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  243. }
  244. }
  245. #endregion
  246. }
  247. }