ListBoxProperty.xaml.cs 19 KB

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