TextFieldProperty.xaml.cs 12 KB

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