ComboBoxProperty.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 ComboBoxProperty : UserControl
  17. {
  18. private ComboBoxParam widgetParam = null;
  19. private CPDFComboBoxWidget cPDFAnnotation = null;
  20. private PDFViewControl pdfViewerControl = null;
  21. private CPDFDocument cPDFDocument = null;
  22. private Dictionary<string, string> itemlists = null;
  23. public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
  24. {
  25. 6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
  26. };
  27. bool IsLoadedData = false;
  28. public ComboBoxProperty()
  29. {
  30. InitializeComponent();
  31. }
  32. #region Loaded
  33. public void SetProperty(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
  34. {
  35. widgetParam = (ComboBoxParam)annotParam;
  36. cPDFAnnotation = (CPDFComboBoxWidget)annotation;
  37. pdfViewerControl = cPDFViewer;
  38. cPDFDocument = doc;
  39. }
  40. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  41. {
  42. Binding SizeListbinding = new Binding();
  43. SizeListbinding.Source = this;
  44. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  45. FontSizeCombox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  46. FieldNameText.Text = widgetParam.FieldName;
  47. FormFieldCombox.SelectedIndex = (int)ParamConverter.ConverterWidgetFormFlags(widgetParam.Flags, widgetParam.IsHidden);
  48. BorderColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.LineColor));
  49. BackgroundColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.BgColor));
  50. TextColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.FontColor));
  51. SetFontName(widgetParam.FontName);
  52. SetFontStyle(widgetParam.IsItalic, widgetParam.IsBold);
  53. SetFontSize(widgetParam.FontSize);
  54. itemlists = widgetParam.OptionItems;
  55. if (itemlists != null)
  56. {
  57. foreach (string key in itemlists.Keys)
  58. {
  59. ListBoxItem item = new ListBoxItem();
  60. item.Content = key;
  61. item.Tag = itemlists[key];
  62. itemsListBox.Items.Add(item);
  63. }
  64. CheckListCount();
  65. }
  66. TopTabControl.SelectedIndex = 2;
  67. IsLoadedData = true;
  68. }
  69. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  70. {
  71. IsLoadedData = false;
  72. }
  73. private void SetFontSize(double size)
  74. {
  75. int index = SizeList.IndexOf((int)size);
  76. FontSizeCombox.SelectedIndex = index;
  77. }
  78. private void SetFontStyle(bool IsItalic, bool IsBold)
  79. {
  80. int index = 0;
  81. if (IsItalic && IsBold)
  82. {
  83. index = 3;
  84. }
  85. else if (IsItalic)
  86. {
  87. index = 2;
  88. }
  89. else if (IsBold)
  90. {
  91. index = 1;
  92. }
  93. FontStyleCombox.SelectedIndex = index;
  94. }
  95. private void SetFontName(string fontName)
  96. {
  97. int index = -1;
  98. List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
  99. for (int i = 0; i < fontFamilyList.Count; i++)
  100. {
  101. if (fontFamilyList[i].ToLower().Contains(fontName.ToLower())
  102. || fontName.ToLower().Contains(fontFamilyList[i].ToLower()))
  103. {
  104. index = i;
  105. }
  106. }
  107. FontCombox.SelectedIndex = index;
  108. }
  109. #endregion
  110. private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
  111. {
  112. if (IsLoadedData)
  113. {
  114. cPDFAnnotation.SetFieldName((sender as TextBox).Text);
  115. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  116. }
  117. }
  118. private void FormFieldCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  119. {
  120. if (IsLoadedData)
  121. {
  122. cPDFAnnotation.SetFlags(ParamConverter.GetFormFlags((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation));
  123. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  124. }
  125. }
  126. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  127. {
  128. if (IsLoadedData)
  129. {
  130. byte[] Color = new byte[3];
  131. Color[0] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.R;
  132. Color[1] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.G;
  133. Color[2] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.B;
  134. cPDFAnnotation.SetWidgetBorderRGBColor(Color);
  135. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  136. }
  137. }
  138. private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
  139. {
  140. if (IsLoadedData)
  141. {
  142. byte[] Color = new byte[3];
  143. Color[0] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.R;
  144. Color[1] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.G;
  145. Color[2] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.B;
  146. cPDFAnnotation.SetWidgetBgRGBColor(Color);
  147. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  148. }
  149. }
  150. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  151. {
  152. if (IsLoadedData)
  153. {
  154. byte[] Color = new byte[3];
  155. Color[0] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.R;
  156. Color[1] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.G;
  157. Color[2] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.B;
  158. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  159. cTextAttribute.FontColor = Color;
  160. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  161. cPDFAnnotation.UpdateFormAp();
  162. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  163. }
  164. }
  165. private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  166. {
  167. if (IsLoadedData)
  168. {
  169. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  170. bool isBold = IsBold(cTextAttribute.FontName);
  171. bool isItalic = IsItalic(cTextAttribute.FontName);
  172. FontType fontType = GetFontType((sender as ComboBox).SelectedItem?.ToString());
  173. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  174. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  175. cPDFAnnotation.UpdateFormAp();
  176. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  177. }
  178. }
  179. private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  180. {
  181. if (IsLoadedData)
  182. {
  183. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  184. bool isItalic = false;
  185. bool isBold = false;
  186. switch ((sender as ComboBox).SelectedIndex)
  187. {
  188. case 0:
  189. break;
  190. case 1:
  191. isItalic = IsItalic(cTextAttribute.FontName);
  192. isBold = true;
  193. break;
  194. case 2:
  195. isItalic = true;
  196. isBold = IsBold(cTextAttribute.FontName);
  197. break;
  198. case 3:
  199. isItalic = true;
  200. isBold = true;
  201. break;
  202. default:
  203. break;
  204. }
  205. FontType fontType = GetFontType(cTextAttribute.FontName);
  206. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  207. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  208. cPDFAnnotation.UpdateFormAp();
  209. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  210. }
  211. }
  212. private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  213. {
  214. if (IsLoadedData)
  215. {
  216. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  217. cTextAttribute.FontSize = Convert.ToSingle((sender as ComboBox).SelectedItem);
  218. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  219. cPDFAnnotation.UpdateFormAp();
  220. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  221. }
  222. }
  223. private void txtItemInput_TextChanged(object sender, TextChangedEventArgs e)
  224. {
  225. if (itemlists.ContainsKey(txtItemInput.Text.Trim()))
  226. {
  227. btnAddItem.IsEnabled = false;
  228. }
  229. else
  230. {
  231. if (!string.IsNullOrEmpty(txtItemInput.Text))
  232. btnAddItem.IsEnabled = true;
  233. }
  234. }
  235. private void btnAddItem_Click(object sender, RoutedEventArgs e)
  236. {
  237. itemlists.Add(txtItemInput.Text, txtItemInput.Text);
  238. ListBoxItem item = new ListBoxItem();
  239. item.Content = txtItemInput.Text;
  240. item.Tag = txtItemInput.Text;
  241. itemsListBox.Items.Add(item);
  242. UpdateListItems();
  243. txtItemInput.Text = "";
  244. txtItemInput.Focus();
  245. btnAddItem.IsEnabled = false;
  246. }
  247. private void UpdateListItems()
  248. {
  249. Dictionary<string, string> pairs = new Dictionary<string, string>();
  250. foreach (ListBoxItem item in itemsListBox.Items)
  251. {
  252. if (item.Content != null && item.Tag != null)
  253. {
  254. pairs.Add(item.Content.ToString(), item.Tag.ToString());
  255. }
  256. }
  257. int optionsCount = cPDFAnnotation.GetItemsCount();
  258. for (int i = 0; i < optionsCount; i++)
  259. {
  260. cPDFAnnotation.RemoveOptionItem(0);
  261. }
  262. int addIndex = 0;
  263. foreach (string key in pairs.Keys)
  264. {
  265. cPDFAnnotation.AddOptionItem(addIndex, pairs[key], key);
  266. addIndex++;
  267. }
  268. if (itemsListBox.SelectedIndex > -1)
  269. {
  270. cPDFAnnotation.SelectItem(itemsListBox.SelectedIndex);
  271. }
  272. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  273. CheckListCount();
  274. }
  275. private void CheckListCount()
  276. {
  277. if (itemsListBox.Items.Count > 0)
  278. TipPanel.Visibility = Visibility.Visible;
  279. else
  280. TipPanel.Visibility = Visibility.Collapsed;
  281. }
  282. private void itemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  283. {
  284. if (e.AddedItems.Count > 0)
  285. {
  286. btnDelete.IsEnabled = true;
  287. if (itemsListBox.SelectedIndex <= 0)
  288. {
  289. btnMoveUp.IsEnabled = false;
  290. }
  291. else
  292. {
  293. btnMoveUp.IsEnabled = true;
  294. }
  295. if (itemsListBox.SelectedIndex >= itemsListBox.Items.Count - 1)
  296. {
  297. btnMoveDown.IsEnabled = false;
  298. }
  299. else
  300. {
  301. btnMoveDown.IsEnabled = true;
  302. }
  303. txtItemInput.Text = (itemsListBox.SelectedItem as ListBoxItem).Content.ToString();
  304. txtItemInput.SelectAll();
  305. }
  306. if (itemsListBox.SelectedItems.Count <= 0)
  307. {
  308. btnDelete.IsEnabled = false;
  309. btnMoveDown.IsEnabled = false;
  310. btnMoveUp.IsEnabled = false;
  311. }
  312. if (itemsListBox.SelectedIndex >= 0)
  313. {
  314. cPDFAnnotation.SelectItem(itemsListBox.SelectedIndex);
  315. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDateAnnotFrame();
  316. }
  317. }
  318. private void btnDelete_Click(object sender, RoutedEventArgs e)
  319. {
  320. if (itemsListBox.SelectedItems.Count > 0)
  321. {
  322. if ((itemsListBox.SelectedItem as ListBoxItem).Content != null)
  323. itemlists.Remove((itemsListBox.SelectedItem as ListBoxItem).Content.ToString());
  324. itemsListBox.Items.Remove(itemsListBox.SelectedItem as ListBoxItem);
  325. btnDelete.IsEnabled = false;
  326. UpdateListItems();
  327. txtItemInput.Text = "";
  328. }
  329. }
  330. private void btnMoveUp_Click(object sender, RoutedEventArgs e)
  331. {
  332. ListBoxItem newitem = new ListBoxItem();
  333. newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
  334. newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
  335. int index = itemsListBox.SelectedIndex;
  336. if (index - 1 >= 0)
  337. {
  338. itemsListBox.Items.Insert(index - 1, newitem);
  339. itemsListBox.Items.Remove(itemsListBox.SelectedItem);
  340. itemsListBox.SelectedIndex = index - 1;
  341. itemsListBox.Focus();
  342. UpdateListItems();
  343. }
  344. }
  345. private void btnMoveDown_Click(object sender, RoutedEventArgs e)
  346. {
  347. ListBoxItem newitem = new ListBoxItem();
  348. newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
  349. newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
  350. int index = itemsListBox.SelectedIndex;
  351. if (index + 1 <= itemsListBox.Items.Count)
  352. {
  353. itemsListBox.Items.Remove(itemsListBox.SelectedItem);
  354. itemsListBox.Items.Insert(index + 1, newitem);
  355. itemsListBox.SelectedIndex = index + 1;
  356. itemsListBox.Focus();
  357. UpdateListItems();
  358. }
  359. }
  360. }
  361. }