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