ComboBoxProperty.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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().UpDataAnnotFrame();
  116. }
  117. }
  118. private void FormFieldCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  119. {
  120. if (IsLoadedData)
  121. {
  122. ParamConverter.SetFormField((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation);
  123. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  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().UpDataAnnotFrame();
  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().UpDataAnnotFrame();
  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. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  162. }
  163. }
  164. private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  165. {
  166. if (IsLoadedData)
  167. {
  168. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  169. bool isBold = IsBold(cTextAttribute.FontName);
  170. bool isItalic = IsItalic(cTextAttribute.FontName);
  171. FontType fontType = GetFontType(cTextAttribute.FontName);
  172. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  173. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  174. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  175. }
  176. }
  177. private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  178. {
  179. if (IsLoadedData)
  180. {
  181. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  182. bool isItalic = false;
  183. bool isBold = false;
  184. switch ((sender as ComboBox).SelectedIndex)
  185. {
  186. case 0:
  187. break;
  188. case 1:
  189. isItalic = IsItalic(cTextAttribute.FontName);
  190. isBold = true;
  191. break;
  192. case 2:
  193. isItalic = true;
  194. isBold = IsBold(cTextAttribute.FontName);
  195. break;
  196. case 3:
  197. isItalic = true;
  198. isBold = true;
  199. break;
  200. default:
  201. break;
  202. }
  203. FontType fontType = GetFontType(cTextAttribute.FontName);
  204. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  205. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  206. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  207. }
  208. }
  209. private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  210. {
  211. if (IsLoadedData)
  212. {
  213. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  214. cTextAttribute.FontSize = (float)(sender as ComboBox).SelectedItem;
  215. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  216. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  217. }
  218. }
  219. private void txtItemInput_TextChanged(object sender, TextChangedEventArgs e)
  220. {
  221. if (itemlists.ContainsKey(txtItemInput.Text.Trim()))
  222. {
  223. btnAddItem.IsEnabled = false;
  224. }
  225. else
  226. {
  227. if (!string.IsNullOrEmpty(txtItemInput.Text))
  228. btnAddItem.IsEnabled = true;
  229. }
  230. }
  231. private void btnAddItem_Click(object sender, RoutedEventArgs e)
  232. {
  233. itemlists.Add(txtItemInput.Text, txtItemInput.Text);
  234. ListBoxItem item = new ListBoxItem();
  235. item.Content = txtItemInput.Text;
  236. item.Tag = txtItemInput.Text;
  237. itemsListBox.Items.Add(item);
  238. UpdateListItems();
  239. txtItemInput.Text = "";
  240. txtItemInput.Focus();
  241. btnAddItem.IsEnabled = false;
  242. }
  243. private void UpdateListItems()
  244. {
  245. Dictionary<string, string> pairs = new Dictionary<string, string>();
  246. foreach (ListBoxItem item in itemsListBox.Items)
  247. {
  248. if (item.Content != null && item.Tag != null)
  249. {
  250. pairs.Add(item.Content.ToString(), item.Tag.ToString());
  251. }
  252. }
  253. int optionsCount = cPDFAnnotation.GetItemsCount();
  254. for (int i = 0; i < optionsCount; i++)
  255. {
  256. cPDFAnnotation.RemoveOptionItem(0);
  257. }
  258. int addIndex = 0;
  259. foreach (string key in pairs.Keys)
  260. {
  261. cPDFAnnotation.AddOptionItem(addIndex, pairs[key], key);
  262. addIndex++;
  263. }
  264. if (itemsListBox.SelectedIndex > -1)
  265. {
  266. cPDFAnnotation.SelectItem(itemsListBox.SelectedIndex);
  267. }
  268. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  269. CheckListCount();
  270. }
  271. private void CheckListCount()
  272. {
  273. if (itemsListBox.Items.Count > 0)
  274. TipPanel.Visibility = Visibility.Visible;
  275. else
  276. TipPanel.Visibility = Visibility.Collapsed;
  277. }
  278. private void itemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  279. {
  280. if (e.AddedItems.Count > 0)
  281. {
  282. btnDelete.IsEnabled = true;
  283. if (itemsListBox.SelectedIndex <= 0)
  284. {
  285. btnMoveUp.IsEnabled = false;
  286. }
  287. else
  288. {
  289. btnMoveUp.IsEnabled = true;
  290. }
  291. if (itemsListBox.SelectedIndex >= itemsListBox.Items.Count - 1)
  292. {
  293. btnMoveDown.IsEnabled = false;
  294. }
  295. else
  296. {
  297. btnMoveDown.IsEnabled = true;
  298. }
  299. txtItemInput.Text = (itemsListBox.SelectedItem as ListBoxItem).Content.ToString();
  300. txtItemInput.SelectAll();
  301. }
  302. if (itemsListBox.SelectedItems.Count <= 0)
  303. {
  304. btnDelete.IsEnabled = false;
  305. btnMoveDown.IsEnabled = false;
  306. btnMoveUp.IsEnabled = false;
  307. }
  308. if (itemsListBox.SelectedIndex >= 0)
  309. {
  310. cPDFAnnotation.SelectItem(itemsListBox.SelectedIndex);
  311. pdfViewerControl.PDFViewTool.GetCPDFViewer().UpDataAnnotFrame();
  312. }
  313. }
  314. private void btnDelete_Click(object sender, RoutedEventArgs e)
  315. {
  316. if (itemsListBox.SelectedItems.Count > 0)
  317. {
  318. if ((itemsListBox.SelectedItem as ListBoxItem).Content != null)
  319. itemlists.Remove((itemsListBox.SelectedItem as ListBoxItem).Content.ToString());
  320. itemsListBox.Items.Remove(itemsListBox.SelectedItem as ListBoxItem);
  321. btnDelete.IsEnabled = false;
  322. UpdateListItems();
  323. txtItemInput.Text = "";
  324. }
  325. }
  326. private void btnMoveUp_Click(object sender, RoutedEventArgs e)
  327. {
  328. ListBoxItem newitem = new ListBoxItem();
  329. newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
  330. newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
  331. int index = itemsListBox.SelectedIndex;
  332. if (index - 1 >= 0)
  333. {
  334. itemsListBox.Items.Insert(index - 1, newitem);
  335. itemsListBox.Items.Remove(itemsListBox.SelectedItem);
  336. itemsListBox.SelectedIndex = index - 1;
  337. itemsListBox.Focus();
  338. UpdateListItems();
  339. }
  340. }
  341. private void btnMoveDown_Click(object sender, RoutedEventArgs e)
  342. {
  343. ListBoxItem newitem = new ListBoxItem();
  344. newitem.Content = (itemsListBox.SelectedItem as ListBoxItem).Content;
  345. newitem.Tag = (itemsListBox.SelectedItem as ListBoxItem).Tag;
  346. int index = itemsListBox.SelectedIndex;
  347. if (index + 1 <= itemsListBox.Items.Count)
  348. {
  349. itemsListBox.Items.Remove(itemsListBox.SelectedItem);
  350. itemsListBox.Items.Insert(index + 1, newitem);
  351. itemsListBox.SelectedIndex = index + 1;
  352. itemsListBox.Focus();
  353. UpdateListItems();
  354. }
  355. }
  356. }
  357. }