TextFieldProperty.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 ComPDFKit.Tool.UndoManger;
  14. using ComPDFKitViewer.Helper;
  15. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  16. using System.Linq;
  17. using System.Windows.Input;
  18. namespace ComPDFKit.Controls.PDFControl
  19. {
  20. public partial class TextFieldProperty : UserControl
  21. {
  22. public bool isFontStyleCmbClicked;
  23. private TextBoxParam widgetParam = null;
  24. private CPDFTextWidget cPDFAnnotation = null;
  25. private PDFViewControl pdfViewerControl = null;
  26. private CPDFDocument cPDFDocument = null;
  27. public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
  28. {
  29. 6,8,9,10,12,14,18,20,24,26,28,30,32,48,72
  30. };
  31. bool IsLoadedData = false;
  32. public TextFieldProperty()
  33. {
  34. InitializeComponent();
  35. }
  36. #region Loaded
  37. public void SetProperty(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
  38. {
  39. widgetParam = (TextBoxParam)annotParam;
  40. cPDFAnnotation = (CPDFTextWidget)annotation;
  41. pdfViewerControl = cPDFViewer;
  42. cPDFDocument = doc;
  43. }
  44. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  45. {
  46. Binding SizeListbinding = new Binding();
  47. SizeListbinding.Source = this;
  48. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  49. FontSizeCmb.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  50. FieldNameText.Text = widgetParam.FieldName;
  51. FormFieldCmb.SelectedIndex = (int)ParamConverter.ConverterWidgetFormFlags(widgetParam.Flags, widgetParam.IsHidden);
  52. BorderColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.LineColor));
  53. BackgroundColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.BgColor));
  54. TextColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.FontColor));
  55. string familyName = string.Empty;
  56. string styleName = string.Empty;
  57. CPDFFont.GetFamilyStyleName(widgetParam.FontName, ref familyName, ref styleName);
  58. FontCmb.ItemsSource = CPDFFont.GetFontNameDictionary().Keys.ToList();
  59. SetFontName(familyName);
  60. SetFontStyle(styleName);
  61. SetFontSize(widgetParam.FontSize);
  62. TextAlignmentCmb.SelectedIndex = (int)widgetParam.Alignment;
  63. DefaultText.Text = widgetParam.Text;
  64. chkMutiline.IsChecked = widgetParam.IsMultiLine;
  65. IsLoadedData = true;
  66. }
  67. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  68. {
  69. IsLoadedData = false;
  70. }
  71. private void SetFontSize(double size)
  72. {
  73. int index = SizeList.IndexOf((int)size);
  74. FontSizeCmb.SelectedIndex = index;
  75. }
  76. private void SetFontStyle(string fontStyle)
  77. {
  78. FontStyleCmb.SelectedValue = fontStyle;
  79. }
  80. private void SetFontName(string fontName)
  81. {
  82. FontCmb.SelectedValue = fontName;
  83. if (FontCmb.SelectedValue != null)
  84. {
  85. FontStyleCmb.ItemsSource = CPDFFont.GetFontNameDictionary()[FontCmb.SelectedValue.ToString()];
  86. }
  87. }
  88. #endregion
  89. #region Updata
  90. private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
  91. {
  92. if (IsLoadedData)
  93. {
  94. var history = GetNewHistory();
  95. cPDFAnnotation.SetFieldName((sender as TextBox).Text);
  96. pdfViewerControl.UpdateAnnotFrame();
  97. AddHistory(history);
  98. }
  99. }
  100. private void FormFieldCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  101. {
  102. if (IsLoadedData)
  103. {
  104. var history = GetNewHistory();
  105. cPDFAnnotation.SetFlags(ParamConverter.GetFormFlags((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation));
  106. pdfViewerControl.UpdateAnnotFrame();
  107. AddHistory(history);
  108. }
  109. }
  110. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  111. {
  112. if (IsLoadedData)
  113. {
  114. var history = GetNewHistory();
  115. byte[] Color = new byte[3];
  116. Color[0] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.R;
  117. Color[1] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.G;
  118. Color[2] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.B;
  119. cPDFAnnotation.SetWidgetBorderRGBColor(Color);
  120. pdfViewerControl.UpdateAnnotFrame();
  121. AddHistory(history);
  122. }
  123. }
  124. private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
  125. {
  126. if (IsLoadedData)
  127. {
  128. var history = GetNewHistory();
  129. byte[] Color = new byte[3];
  130. Color[0] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.R;
  131. Color[1] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.G;
  132. Color[2] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.B;
  133. cPDFAnnotation.SetWidgetBgRGBColor(Color);
  134. pdfViewerControl.UpdateAnnotFrame();
  135. AddHistory(history);
  136. }
  137. }
  138. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  139. {
  140. if (IsLoadedData)
  141. {
  142. var history = GetNewHistory();
  143. byte[] Color = new byte[3];
  144. Color[0] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.R;
  145. Color[1] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.G;
  146. Color[2] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.B;
  147. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  148. cTextAttribute.FontColor = Color;
  149. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  150. cPDFAnnotation.UpdateFormAp();
  151. pdfViewerControl.UpdateAnnotFrame();
  152. AddHistory(history);
  153. }
  154. }
  155. private void FontCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  156. {
  157. if (IsLoadedData)
  158. {
  159. FontStyleCmb.ItemsSource = CPDFFont.GetFontNameDictionary()[FontCmb.SelectedValue.ToString()];
  160. FontStyleCmb.SelectedIndex = 0;
  161. var history = GetNewHistory();
  162. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  163. string psFontName = string.Empty;
  164. CPDFFont.GetPostScriptName(FontCmb.SelectedValue.ToString(), FontStyleCmb.SelectedValue.ToString(), ref psFontName);
  165. cTextAttribute.FontName = psFontName;
  166. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  167. cPDFAnnotation.UpdateFormAp();
  168. pdfViewerControl.UpdateAnnotFrame();
  169. AddHistory(history);
  170. }
  171. }
  172. private void FontStyleCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  173. {
  174. if (IsLoadedData)
  175. {
  176. var history = GetNewHistory();
  177. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  178. string psFontName = string.Empty;
  179. CPDFFont.GetPostScriptName(FontCmb.SelectedValue.ToString(), FontStyleCmb.SelectedValue?.ToString(), ref psFontName);
  180. cTextAttribute.FontName = psFontName;
  181. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  182. cPDFAnnotation.UpdateFormAp();
  183. pdfViewerControl.UpdateAnnotFrame();
  184. AddHistory(history);
  185. }
  186. else
  187. {
  188. FontStyleCmb.ItemsSource = CPDFFont.GetFontNameDictionary()[FontCmb.SelectedValue.ToString()];
  189. }
  190. }
  191. private void FontSizeCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  192. {
  193. if (IsLoadedData)
  194. {
  195. var history = GetNewHistory();
  196. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  197. cTextAttribute.FontSize = Convert.ToSingle((sender as ComboBox).SelectedItem);
  198. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  199. cPDFAnnotation.UpdateFormAp();
  200. pdfViewerControl.UpdateAnnotFrame();
  201. AddHistory(history);
  202. }
  203. }
  204. private void TextAlignmentCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  205. {
  206. if (IsLoadedData)
  207. {
  208. var history = GetNewHistory();
  209. cPDFAnnotation.SetJustification((C_TEXT_ALIGNMENT)(sender as ComboBox).SelectedIndex);
  210. pdfViewerControl.UpdateAnnotFrame();
  211. AddHistory(history);
  212. }
  213. }
  214. private void DefaultText_TextChanged(object sender, TextChangedEventArgs e)
  215. {
  216. if (IsLoadedData)
  217. {
  218. var history = GetNewHistory();
  219. cPDFAnnotation.SetText((sender as TextBox).Text);
  220. pdfViewerControl.UpdateAnnotFrame();
  221. AddHistory(history);
  222. }
  223. }
  224. private void chkMutiline_Checked(object sender, RoutedEventArgs e)
  225. {
  226. if (IsLoadedData)
  227. {
  228. var history = GetNewHistory();
  229. cPDFAnnotation.SetMultiLine(true);
  230. pdfViewerControl.UpdateAnnotFrame();
  231. AddHistory(history);
  232. }
  233. }
  234. private void chkMutiline_Unchecked(object sender, RoutedEventArgs e)
  235. {
  236. if (IsLoadedData)
  237. {
  238. var history = GetNewHistory();
  239. cPDFAnnotation.SetMultiLine(false);
  240. pdfViewerControl.UpdateAnnotFrame();
  241. AddHistory(history);
  242. }
  243. }
  244. private TextBoxHistory GetNewHistory()
  245. {
  246. TextBoxHistory history = new TextBoxHistory();
  247. history.Action = HistoryAction.Update;
  248. history.PDFDoc = pdfViewerControl.GetCPDFViewer().GetDocument();
  249. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  250. return history;
  251. }
  252. private void AddHistory(TextBoxHistory history)
  253. {
  254. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, cPDFAnnotation.Page.PageIndex, cPDFAnnotation);
  255. pdfViewerControl.GetCPDFViewer().UndoManager.AddHistory(history);
  256. }
  257. #endregion
  258. }
  259. }