ComboBoxFormProperty.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using ComPDFKit.PDFAnnotation;
  15. using ComPDFKit.PDFAnnotation.Form;
  16. using ComPDFKitViewer;
  17. using ComPDFKitViewer.AnnotEvent;
  18. namespace ComPDFKitDemo.WidgetForm
  19. {
  20. public partial class ComboBoxFormProperty : Window
  21. {
  22. public WidgetAttribEvent CurrentAttribEvent;
  23. private Dictionary<string, string> optionsList;
  24. public ComboBoxFormProperty()
  25. {
  26. InitializeComponent();
  27. }
  28. private void NavClick(object sender, MouseButtonEventArgs e)
  29. {
  30. foreach (TextBlock child in FormStack.Children)
  31. {
  32. child.Foreground = Brushes.Black;
  33. child.Background = Brushes.Transparent;
  34. }
  35. TextBlock clickBlock = sender as TextBlock;
  36. clickBlock.Background = new SolidColorBrush(Color.FromRgb(0xD1, 0xE2, 0xF2));
  37. RegularPanel.Visibility = Visibility.Collapsed;
  38. AppearancePanel.Visibility = Visibility.Collapsed;
  39. OptionPanel.Visibility = Visibility.Collapsed;
  40. switch (clickBlock.Tag.ToString())
  41. {
  42. case "Regular":
  43. RegularPanel.Visibility = Visibility.Visible;
  44. break;
  45. case "Appearance":
  46. AppearancePanel.Visibility = Visibility.Visible;
  47. break;
  48. case "Option":
  49. OptionPanel.Visibility = Visibility.Visible;
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. public void SetFormData(WidgetAttribEvent annotEvent)
  56. {
  57. optionsList = null;
  58. if (annotEvent != null)
  59. {
  60. FontStyleBox.SelectedIndex = 0;
  61. bool hideOptions = true;
  62. foreach (AnnotAttrib attr in annotEvent.Attribs.Keys)
  63. {
  64. switch (attr)
  65. {
  66. case AnnotAttrib.FieldName:
  67. GroupNameText.Text = annotEvent.Attribs[attr].ToString();
  68. break;
  69. case AnnotAttrib.Color:
  70. BorderColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr];
  71. break;
  72. case AnnotAttrib.FillColor:
  73. FillColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr];
  74. break;
  75. case AnnotAttrib.FontFamily:
  76. List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
  77. string currentFont = annotEvent.Attribs[attr].ToString();
  78. FontFamilyBox.SelectedIndex = -1;
  79. for (int i = 0; i < fontFamilyList.Count; i++)
  80. {
  81. if (fontFamilyList[i].ToLower().Contains(currentFont.ToLower()))
  82. {
  83. FontFamilyBox.SelectedIndex = i;
  84. }
  85. }
  86. break;
  87. case AnnotAttrib.FontSize:
  88. FontSizeBox.Text = annotEvent.Attribs[attr].ToString();
  89. break;
  90. case AnnotAttrib.FontColor:
  91. {
  92. FontColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr];
  93. }
  94. break;
  95. case AnnotAttrib.FontStyle:
  96. {
  97. FontStyle currentStyle = (FontStyle)annotEvent.Attribs[attr];
  98. if (currentStyle == FontStyles.Italic)
  99. {
  100. FontStyleBox.SelectedIndex = FontStyleBox.SelectedIndex + 1;
  101. }
  102. }
  103. break;
  104. case AnnotAttrib.FontWeight:
  105. {
  106. FontWeight currentWeight = (FontWeight)annotEvent.Attribs[attr];
  107. if (currentWeight == FontWeights.Bold)
  108. {
  109. FontStyleBox.SelectedIndex = FontStyleBox.SelectedIndex + 1;
  110. }
  111. }
  112. break;
  113. case AnnotAttrib.Thickness:
  114. ThicknessText.SelectedIndex = Convert.ToInt32(annotEvent.Attribs[attr]) - 1;
  115. break;
  116. case AnnotAttrib.LineStyle:
  117. if ((C_BORDER_STYLE)annotEvent.Attribs[attr] == C_BORDER_STYLE.BS_DASHDED)
  118. {
  119. LineStyleBox.SelectedIndex = 1;
  120. }
  121. else
  122. {
  123. LineStyleBox.SelectedIndex = 0;
  124. }
  125. break;
  126. case AnnotAttrib.Locked:
  127. {
  128. LockBox.IsChecked = (bool)annotEvent.Attribs[attr] == true;
  129. EnableControls(!(LockBox.IsChecked == true));
  130. }
  131. break;
  132. case AnnotAttrib.ReadOnly:
  133. {
  134. ReadOnlyBox.IsChecked = (bool)annotEvent.Attribs[attr] == true;
  135. }
  136. break;
  137. case AnnotAttrib.FormField:
  138. {
  139. FormField field = (FormField)annotEvent.Attribs[attr];
  140. FormFieldBox.SelectedIndex = (int)field;
  141. }
  142. break;
  143. case AnnotAttrib.ListOptions:
  144. {
  145. optionsList = (Dictionary<string, string>)annotEvent.Attribs[attr];
  146. if (optionsList != null)
  147. {
  148. foreach (string key in optionsList.Keys)
  149. {
  150. ListBoxItem item = new ListBoxItem();
  151. item.Content = key;
  152. item.Tag = optionsList[key];
  153. OptionsListBox.Items.Add(item);
  154. }
  155. hideOptions = false;
  156. }
  157. }
  158. break;
  159. case AnnotAttrib.DefaultChoice:
  160. {
  161. List<int> selectedIndexList = (List<int>)annotEvent.Attribs[attr];
  162. if (selectedIndexList != null)
  163. {
  164. foreach (int index in selectedIndexList)
  165. {
  166. if (OptionsListBox.Items.Count > index)
  167. {
  168. (OptionsListBox.Items[index] as ListBoxItem).IsSelected = true;
  169. }
  170. }
  171. hideOptions = false;
  172. }
  173. }
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. OptinTab.Visibility = hideOptions == false ? Visibility.Visible : Visibility.Collapsed;
  180. if (hideOptions)
  181. {
  182. foreach (UIElement child in OptionPanel.Children)
  183. {
  184. child.Visibility = Visibility.Collapsed;
  185. }
  186. OptinTab.Visibility = Visibility.Collapsed;
  187. }
  188. }
  189. }
  190. private void OptionsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  191. {
  192. ListBoxItem selectItem = OptionsListBox.SelectedItem as ListBoxItem;
  193. if (selectItem != null)
  194. {
  195. OptionName.Text = selectItem.Content.ToString();
  196. OptionValue.Text = selectItem.Tag.ToString();
  197. }
  198. }
  199. private void GroupNameText_TextChanged(object sender, TextChangedEventArgs e)
  200. {
  201. if (GroupNameText.Text.Length > 0)
  202. {
  203. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FieldName, GroupNameText.Text);
  204. CurrentAttribEvent?.UpdateAnnot();
  205. }
  206. }
  207. private void BorderColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  208. {
  209. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Color, e.NewValue);
  210. CurrentAttribEvent?.UpdateAnnot();
  211. }
  212. private void FillColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  213. {
  214. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FillColor, e.NewValue);
  215. CurrentAttribEvent?.UpdateAnnot();
  216. }
  217. private void FontFamilyBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  218. {
  219. string fontName = (FontFamilyBox.SelectedItem as ComboBoxItem).Content.ToString();
  220. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontFamily, fontName);
  221. CurrentAttribEvent?.UpdateAnnot();
  222. }
  223. private void FontSizeBox_TextChanged(object sender, TextChangedEventArgs e)
  224. {
  225. int currentSize = 12;
  226. if (int.TryParse(FontSizeBox.Text, out currentSize))
  227. {
  228. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontSize, currentSize);
  229. CurrentAttribEvent?.UpdateAnnot();
  230. }
  231. }
  232. private void FontStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  233. {
  234. switch (FontStyleBox.SelectedIndex)
  235. {
  236. case 0:
  237. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  238. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  239. break;
  240. case 1:
  241. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  242. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  243. break;
  244. case 2:
  245. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  246. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  247. break;
  248. case 3:
  249. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  250. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  251. break;
  252. }
  253. CurrentAttribEvent?.UpdateAnnot();
  254. }
  255. private void FontColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  256. {
  257. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontColor, e.NewValue);
  258. CurrentAttribEvent?.UpdateAnnot();
  259. }
  260. private void OptionName_TextChanged(object sender, TextChangedEventArgs e)
  261. {
  262. string currentKey = OptionName.Text.Trim();
  263. AddOptionBtn.IsEnabled = true;
  264. if (optionsList != null && optionsList.ContainsKey(currentKey))
  265. {
  266. AddOptionBtn.IsEnabled = false;
  267. }
  268. if (currentKey == string.Empty)
  269. {
  270. AddOptionBtn.IsEnabled = false;
  271. }
  272. }
  273. private void OptionValue_TextChanged(object sender, TextChangedEventArgs e)
  274. {
  275. string currentKey = OptionName.Text.Trim();
  276. string currentValue = OptionValue.Text.Trim();
  277. if (currentKey == string.Empty || currentValue == string.Empty)
  278. {
  279. AddOptionBtn.IsEnabled = false;
  280. return;
  281. }
  282. AddOptionBtn.IsEnabled = true;
  283. if (optionsList != null)
  284. {
  285. if (optionsList.ContainsKey(currentKey))
  286. {
  287. AddOptionBtn.IsEnabled = false;
  288. return;
  289. }
  290. if (optionsList.Values.Contains(currentValue))
  291. {
  292. AddOptionBtn.IsEnabled = false;
  293. return;
  294. }
  295. }
  296. }
  297. private void Button_Click(object sender, RoutedEventArgs e)
  298. {
  299. string currentKey = OptionName.Text.Trim();
  300. string currentValue = OptionValue.Text.Trim();
  301. if (currentKey == string.Empty || currentValue == string.Empty)
  302. {
  303. return;
  304. }
  305. if (optionsList == null)
  306. {
  307. optionsList = new Dictionary<string, string>();
  308. }
  309. optionsList[currentKey] = currentValue;
  310. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.ListOptions, optionsList);
  311. CurrentAttribEvent?.UpdateAnnot();
  312. OptionsListBox.Items.Clear();
  313. foreach (string key in optionsList.Keys)
  314. {
  315. ListBoxItem item = new ListBoxItem();
  316. item.Content = key;
  317. item.Tag = optionsList[key];
  318. OptionsListBox.Items.Add(item);
  319. }
  320. }
  321. private void ButtonDel_Click(object sender, RoutedEventArgs e)
  322. {
  323. if (OptionsListBox.SelectedIndex != -1)
  324. {
  325. OptionsListBox.Items.RemoveAt(OptionsListBox.SelectedIndex);
  326. Dictionary<string, string> currentList = new Dictionary<string, string>();
  327. foreach (ListBoxItem item in OptionsListBox.Items)
  328. {
  329. currentList[item.Content.ToString()] = item.Tag.ToString();
  330. }
  331. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.ListOptions, currentList);
  332. CurrentAttribEvent?.UpdateAnnot();
  333. optionsList = currentList;
  334. }
  335. }
  336. private void ThicknessText_SelectionChanged(object sender, SelectionChangedEventArgs e)
  337. {
  338. if (ThicknessText.SelectedIndex >= 0 && ThicknessText.SelectedIndex < 3)
  339. {
  340. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Thickness, ThicknessText.SelectedIndex + 1);
  341. CurrentAttribEvent?.UpdateAnnot();
  342. }
  343. }
  344. private void LineStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  345. {
  346. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, LineStyleBox.SelectedIndex == 0 ? C_BORDER_STYLE.BS_SOLID : C_BORDER_STYLE.BS_DASHDED);
  347. CurrentAttribEvent?.UpdateAnnot();
  348. }
  349. public void EnableControls(bool isEnable)
  350. {
  351. GroupNameText.IsEnabled = isEnable;
  352. FormFieldBox.IsEnabled = isEnable;
  353. ReadOnlyBox.IsEnabled = isEnable;
  354. BorderColorPicker.IsEnabled = isEnable;
  355. ThicknessText.IsEnabled = isEnable;
  356. FillColorPicker.IsEnabled = isEnable;
  357. LineStyleBox.IsEnabled = isEnable;
  358. FontFamilyBox.IsEnabled = isEnable;
  359. FontSizeBox.IsEnabled = isEnable;
  360. FontStyleBox.IsEnabled = isEnable;
  361. FontColorPicker.IsEnabled = isEnable;
  362. OptionName.IsEnabled = isEnable;
  363. OptionValue.IsEnabled = isEnable;
  364. AddOptionBtn.IsEnabled = isEnable;
  365. OptionsListBox.IsEnabled = isEnable;
  366. OptionDelBtn.IsEnabled = isEnable;
  367. }
  368. private void LockBox_Checked(object sender, RoutedEventArgs e)
  369. {
  370. EnableControls(!(LockBox.IsChecked == true));
  371. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Locked, (LockBox.IsChecked == true));
  372. CurrentAttribEvent?.UpdateAnnot();
  373. }
  374. private void ReadOnlyBox_Checked(object sender, RoutedEventArgs e)
  375. {
  376. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.ReadOnly, (ReadOnlyBox.IsChecked == true));
  377. CurrentAttribEvent?.UpdateAnnot();
  378. }
  379. private void FormFieldBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  380. {
  381. if (FormFieldBox.SelectedIndex != -1)
  382. {
  383. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FormField, (FormField)FormFieldBox.SelectedIndex);
  384. CurrentAttribEvent?.UpdateAnnot();
  385. }
  386. }
  387. }
  388. }