TextFieldProperty.xaml.cs 12 KB

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