ComboBoxProperty.xaml.cs 21 KB

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