PushButtonFormProperty.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 ComPDFKit.PDFDocument.Action;
  17. using ComPDFKitViewer;
  18. using ComPDFKitViewer.AnnotEvent;
  19. namespace ComPDFKitDemo.WidgetForm
  20. {
  21. public partial class PushButtonFormProperty : Window
  22. {
  23. public WidgetAttribEvent CurrentAttribEvent;
  24. public PushButtonFormProperty()
  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. if (annotEvent != null)
  58. {
  59. FontStyleBox.SelectedIndex = 0;
  60. bool hideOptions = true;
  61. foreach (AnnotAttrib attr in annotEvent.Attribs.Keys)
  62. {
  63. switch (attr)
  64. {
  65. case AnnotAttrib.FieldName:
  66. GroupNameText.Text = annotEvent.Attribs[attr].ToString();
  67. break;
  68. case AnnotAttrib.Color:
  69. BorderColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr];
  70. break;
  71. case AnnotAttrib.FillColor:
  72. FillColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr];
  73. break;
  74. case AnnotAttrib.FontFamily:
  75. List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
  76. string currentFont = annotEvent.Attribs[attr].ToString();
  77. FontFamilyBox.SelectedIndex = -1;
  78. for (int i = 0; i < fontFamilyList.Count; i++)
  79. {
  80. if (fontFamilyList[i].ToLower().Contains(currentFont.ToLower()))
  81. {
  82. FontFamilyBox.SelectedIndex = i;
  83. }
  84. }
  85. break;
  86. case AnnotAttrib.FontSize:
  87. FontSizeBox.Text = annotEvent.Attribs[attr].ToString();
  88. break;
  89. case AnnotAttrib.FontColor:
  90. {
  91. FontColorPicker.SelectedColor = (Color)annotEvent.Attribs[attr];
  92. }
  93. break;
  94. case AnnotAttrib.FontStyle:
  95. {
  96. FontStyle currentStyle = (FontStyle)annotEvent.Attribs[attr];
  97. if (currentStyle == FontStyles.Italic)
  98. {
  99. FontStyleBox.SelectedIndex = FontStyleBox.SelectedIndex + 1;
  100. }
  101. }
  102. break;
  103. case AnnotAttrib.FontWeight:
  104. {
  105. FontWeight currentWeight = (FontWeight)annotEvent.Attribs[attr];
  106. if (currentWeight == FontWeights.Bold)
  107. {
  108. FontStyleBox.SelectedIndex = FontStyleBox.SelectedIndex + 1;
  109. }
  110. }
  111. break;
  112. case AnnotAttrib.Thickness:
  113. ThicknessText.SelectedIndex = Convert.ToInt32(annotEvent.Attribs[attr])-1;
  114. break;
  115. case AnnotAttrib.LineStyle:
  116. if ((C_BORDER_STYLE)annotEvent.Attribs[attr] == C_BORDER_STYLE.BS_DASHDED)
  117. {
  118. LineStyleBox.SelectedIndex = 1;
  119. }
  120. else
  121. {
  122. LineStyleBox.SelectedIndex = 0;
  123. }
  124. break;
  125. case AnnotAttrib.Locked:
  126. {
  127. LockBox.IsChecked = (bool)annotEvent.Attribs[attr] == true;
  128. EnableControls(!(LockBox.IsChecked == true));
  129. }
  130. break;
  131. case AnnotAttrib.ReadOnly:
  132. {
  133. ReadOnlyBox.IsChecked = (bool)annotEvent.Attribs[attr] == true;
  134. }
  135. break;
  136. case AnnotAttrib.FormField:
  137. {
  138. FormField field = (FormField)annotEvent.Attribs[attr];
  139. FormFieldBox.SelectedIndex = (int)field;
  140. }
  141. break;
  142. case AnnotAttrib.Text:
  143. TextContentBox.Text = (string)annotEvent.Attribs[attr];
  144. hideOptions = false;
  145. break;
  146. case AnnotAttrib.FormAction:
  147. {
  148. Dictionary<C_ACTION_TYPE, string> ActionDict = (Dictionary<C_ACTION_TYPE, string>)annotEvent.Attribs[attr];
  149. foreach (C_ACTION_TYPE key in ActionDict.Keys)
  150. {
  151. if (key == C_ACTION_TYPE.ACTION_TYPE_GOTO)
  152. {
  153. ActionBox.SelectedIndex = 0;
  154. ActionText.Text = ActionDict[key];
  155. break;
  156. }
  157. if (key == C_ACTION_TYPE.ACTION_TYPE_URI)
  158. {
  159. ActionBox.SelectedIndex = 1;
  160. ActionText.Text = ActionDict[key];
  161. break;
  162. }
  163. }
  164. hideOptions = false;
  165. }
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. OptionRadio.Visibility = hideOptions == false ? Visibility.Visible : Visibility.Collapsed;
  172. OptinTab.Visibility = hideOptions == false ? Visibility.Visible : Visibility.Collapsed;
  173. if (hideOptions)
  174. {
  175. foreach (UIElement child in OptionPanel.Children)
  176. {
  177. child.Visibility = Visibility.Collapsed;
  178. }
  179. OptinTab.Visibility = Visibility.Collapsed;
  180. }
  181. }
  182. }
  183. private void GroupNameText_TextChanged(object sender, TextChangedEventArgs e)
  184. {
  185. if (GroupNameText.Text.Length > 0)
  186. {
  187. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FieldName, GroupNameText.Text);
  188. CurrentAttribEvent?.UpdateAnnot();
  189. }
  190. }
  191. private void BorderColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  192. {
  193. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Color, e.NewValue);
  194. CurrentAttribEvent?.UpdateAnnot();
  195. }
  196. private void FillColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  197. {
  198. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FillColor, e.NewValue);
  199. CurrentAttribEvent?.UpdateAnnot();
  200. }
  201. private void FontFamilyBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  202. {
  203. string fontName = (FontFamilyBox.SelectedItem as ComboBoxItem).Content.ToString();
  204. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontFamily, fontName);
  205. CurrentAttribEvent?.UpdateAnnot();
  206. }
  207. private void FontSizeBox_TextChanged(object sender, TextChangedEventArgs e)
  208. {
  209. int currentSize = 12;
  210. if (int.TryParse(FontSizeBox.Text, out currentSize))
  211. {
  212. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontSize, currentSize);
  213. CurrentAttribEvent?.UpdateAnnot();
  214. }
  215. }
  216. private void FontStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  217. {
  218. switch (FontStyleBox.SelectedIndex)
  219. {
  220. case 0:
  221. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  222. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  223. break;
  224. case 1:
  225. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  226. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  227. break;
  228. case 2:
  229. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  230. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  231. break;
  232. case 3:
  233. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  234. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  235. break;
  236. }
  237. CurrentAttribEvent?.UpdateAnnot();
  238. }
  239. private void FontColorPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  240. {
  241. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FontColor, e.NewValue);
  242. CurrentAttribEvent?.UpdateAnnot();
  243. }
  244. private void TextContentBox_TextChanged(object sender, TextChangedEventArgs e)
  245. {
  246. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Text, TextContentBox.Text.Trim());
  247. CurrentAttribEvent?.UpdateAnnot();
  248. }
  249. private void ThicknessText_SelectionChanged(object sender, SelectionChangedEventArgs e)
  250. {
  251. if (ThicknessText.SelectedIndex >= 0 && ThicknessText.SelectedIndex < 3)
  252. {
  253. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Thickness, ThicknessText.SelectedIndex + 1);
  254. CurrentAttribEvent?.UpdateAnnot();
  255. }
  256. }
  257. private void LineStyleBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  258. {
  259. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.LineStyle, LineStyleBox.SelectedIndex == 0 ? C_BORDER_STYLE.BS_SOLID : C_BORDER_STYLE.BS_DASHDED);
  260. CurrentAttribEvent?.UpdateAnnot();
  261. }
  262. private void ActionAdd_Click(object sender, RoutedEventArgs e)
  263. {
  264. Dictionary<C_ACTION_TYPE, string> ActionDict = new Dictionary<C_ACTION_TYPE, string>();
  265. if (ActionBox.SelectedIndex == 0)
  266. {
  267. ActionDict[C_ACTION_TYPE.ACTION_TYPE_GOTO] = ActionText.Text;
  268. }
  269. if (ActionBox.SelectedIndex == 1)
  270. {
  271. ActionDict[C_ACTION_TYPE.ACTION_TYPE_URI] = ActionText.Text;
  272. }
  273. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FormAction, ActionDict);
  274. CurrentAttribEvent?.UpdateAnnot();
  275. }
  276. private void ActionDel_Click(object sender, RoutedEventArgs e)
  277. {
  278. //CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FormAction, null);
  279. //CurrentAttribEvent?.UpdateAnnot();
  280. }
  281. public void EnableControls(bool isEnable)
  282. {
  283. GroupNameText.IsEnabled = isEnable;
  284. FormFieldBox.IsEnabled = isEnable;
  285. ReadOnlyBox.IsEnabled = isEnable;
  286. BorderColorPicker.IsEnabled = isEnable;
  287. ThicknessText.IsEnabled = isEnable;
  288. FillColorPicker.IsEnabled = isEnable;
  289. LineStyleBox.IsEnabled = isEnable;
  290. FontFamilyBox.IsEnabled = isEnable;
  291. FontSizeBox.IsEnabled = isEnable;
  292. FontStyleBox.IsEnabled = isEnable;
  293. FontColorPicker.IsEnabled = isEnable;
  294. TextContentBox.IsEnabled = isEnable;
  295. ActionBox.IsEnabled = isEnable;
  296. ActionText.IsEnabled = isEnable;
  297. OptionAddBtn.IsEnabled = isEnable;
  298. }
  299. private void LockBox_Checked(object sender, RoutedEventArgs e)
  300. {
  301. EnableControls(!(LockBox.IsChecked == true));
  302. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.Locked, (LockBox.IsChecked == true));
  303. CurrentAttribEvent?.UpdateAnnot();
  304. }
  305. private void ReadOnlyBox_Checked(object sender, RoutedEventArgs e)
  306. {
  307. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.ReadOnly, (ReadOnlyBox.IsChecked == true));
  308. CurrentAttribEvent?.UpdateAnnot();
  309. }
  310. private void FormFieldBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  311. {
  312. if (FormFieldBox.SelectedIndex != -1)
  313. {
  314. CurrentAttribEvent?.UpdateAttrib(AnnotAttrib.FormField, (FormField)FormFieldBox.SelectedIndex);
  315. CurrentAttribEvent?.UpdateAnnot();
  316. }
  317. }
  318. }
  319. }