ComboBoxProperty.xaml.cs 20 KB

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