PushButtonProperty.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using ComPDFKitViewer.AnnotEvent;
  2. using ComPDFKitViewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Media;
  9. using System.Collections.ObjectModel;
  10. using ComPDFKit.PDFDocument.Action;
  11. using ComPDFKitViewer.PdfViewer;
  12. namespace Compdfkit_Tools.PDFControl
  13. {
  14. public partial class PushButtonProperty : UserControl
  15. {
  16. private WidgetPushButtonArgs widgetArgs = null;
  17. private AnnotAttribEvent annotAttribEvent = null;
  18. private CPDFViewer pdfViewer = null;
  19. public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
  20. {
  21. 6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
  22. };
  23. bool IsLoadedData = false;
  24. public PushButtonProperty()
  25. {
  26. InitializeComponent();
  27. }
  28. #region Loaded
  29. public void SetProperty(WidgetArgs Args, AnnotAttribEvent e, CPDFViewer cPDFViewer)
  30. {
  31. pdfViewer = cPDFViewer;
  32. widgetArgs = (WidgetPushButtonArgs)Args;
  33. annotAttribEvent = e;
  34. }
  35. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  36. {
  37. Binding SizeListbinding = new Binding();
  38. SizeListbinding.Source = this;
  39. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  40. FontSizeCombox.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  41. TopTabControl.SelectedIndex = 2;
  42. FieldNameText.Text = widgetArgs.FieldName;
  43. FormFieldCombox.SelectedIndex = (int)widgetArgs.FormField;
  44. BorderColorPickerControl.SetCheckedForColor(widgetArgs.LineColor);
  45. BackgroundColorPickerControl.SetCheckedForColor(widgetArgs.BgColor);
  46. TextColorPickerControl.SetCheckedForColor(widgetArgs.FontColor);
  47. SetFontName(widgetArgs.FontName);
  48. SetFontStyle(widgetArgs.IsItalic, widgetArgs.IsBold);
  49. SetFontSize(widgetArgs.FontSize);
  50. ItemText.Text = widgetArgs.Text;
  51. SetActionContext(widgetArgs.ActionDict);
  52. IsLoadedData = true;
  53. }
  54. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  55. {
  56. IsLoadedData = false;
  57. }
  58. private void SetActionContext(Dictionary<C_ACTION_TYPE, string> keyValuePairs)
  59. {
  60. foreach (C_ACTION_TYPE key in keyValuePairs.Keys)
  61. {
  62. if (key == C_ACTION_TYPE.ACTION_TYPE_GOTO)
  63. {
  64. TextAlignmentCombox.SelectedIndex = 1;
  65. ActionContentText.Visibility = Visibility.Visible;
  66. ActionContentText.Text = (Convert.ToInt32( keyValuePairs[key])+1).ToString();
  67. break;
  68. }
  69. if (key == C_ACTION_TYPE.ACTION_TYPE_URI)
  70. {
  71. TextAlignmentCombox.SelectedIndex = 2;
  72. ActionContentText.Visibility = Visibility.Visible;
  73. ActionContentText.Text = keyValuePairs[key];
  74. break;
  75. }
  76. else
  77. {
  78. TextAlignmentCombox.SelectedIndex = 0;
  79. ActionContentText.Text = "";
  80. break;
  81. }
  82. }
  83. }
  84. private void SetFontSize(double size)
  85. {
  86. int index = SizeList.IndexOf((int)size);
  87. FontSizeCombox.SelectedIndex = index;
  88. }
  89. private void SetFontStyle(bool IsItalic, bool IsBold)
  90. {
  91. int index = 0;
  92. if (IsItalic && IsBold)
  93. {
  94. index = 3;
  95. }
  96. else if (IsItalic)
  97. {
  98. index = 2;
  99. }
  100. else if (IsBold)
  101. {
  102. index = 1;
  103. }
  104. FontStyleCombox.SelectedIndex = index;
  105. }
  106. private void SetFontName(string fontName)
  107. {
  108. int index = -1;
  109. List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
  110. for (int i = 0; i < fontFamilyList.Count; i++)
  111. {
  112. if (fontFamilyList[i].ToLower().Contains(fontName.ToLower())
  113. || fontName.ToLower().Contains(fontFamilyList[i].ToLower()))
  114. {
  115. index = i;
  116. }
  117. }
  118. FontCombox.SelectedIndex = index;
  119. }
  120. #endregion
  121. #region Updata
  122. private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
  123. {
  124. if (IsLoadedData)
  125. {
  126. annotAttribEvent.UpdateAttrib(AnnotAttrib.FieldName, (sender as TextBox).Text);
  127. annotAttribEvent.UpdateAnnot();
  128. }
  129. }
  130. private void FormFieldCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  131. {
  132. if (IsLoadedData)
  133. {
  134. annotAttribEvent.UpdateAttrib(AnnotAttrib.FormField, (sender as ComboBox).SelectedIndex);
  135. annotAttribEvent.UpdateAnnot();
  136. }
  137. }
  138. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  139. {
  140. if (IsLoadedData)
  141. {
  142. annotAttribEvent.UpdateAttrib(AnnotAttrib.Color, ((SolidColorBrush)BorderColorPickerControl.Brush).Color);
  143. annotAttribEvent.UpdateAnnot();
  144. }
  145. }
  146. private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
  147. {
  148. if (IsLoadedData)
  149. {
  150. annotAttribEvent.UpdateAttrib(AnnotAttrib.FillColor, ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color);
  151. annotAttribEvent.UpdateAnnot();
  152. }
  153. }
  154. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  155. {
  156. if (IsLoadedData)
  157. {
  158. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontColor, ((SolidColorBrush)TextColorPickerControl.Brush).Color);
  159. annotAttribEvent.UpdateAnnot();
  160. }
  161. }
  162. private void FontCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  163. {
  164. if (IsLoadedData)
  165. {
  166. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontName, ((sender as ComboBox).SelectedItem as ComboBoxItem).Content.ToString());
  167. annotAttribEvent.UpdateAnnot();
  168. }
  169. }
  170. private void FontStyleCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  171. {
  172. if (IsLoadedData)
  173. {
  174. bool IsItalic = false;
  175. bool IsBold = false;
  176. switch ((sender as ComboBox).SelectedIndex)
  177. {
  178. case 0:
  179. break;
  180. case 1:
  181. IsBold = true;
  182. break;
  183. case 2:
  184. IsItalic = true;
  185. break;
  186. case 3:
  187. IsItalic = true;
  188. IsBold = true;
  189. break;
  190. default:
  191. break;
  192. }
  193. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsBold, IsBold);
  194. annotAttribEvent.UpdateAttrib(AnnotAttrib.IsItalic, IsItalic);
  195. annotAttribEvent.UpdateAnnot();
  196. }
  197. }
  198. private void FontSizeCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  199. {
  200. if (IsLoadedData)
  201. {
  202. annotAttribEvent.UpdateAttrib(AnnotAttrib.FontSize, (sender as ComboBox).SelectedItem);
  203. annotAttribEvent.UpdateAnnot();
  204. }
  205. }
  206. #endregion
  207. private void TextAlignmentCombox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  208. {
  209. if (IsLoadedData)
  210. {
  211. ActionContentText.Style = null;
  212. switch (TextAlignmentCombox.SelectedIndex)
  213. {
  214. case 0:
  215. ActionContentText.Visibility = Visibility.Collapsed;
  216. break;
  217. case 1:
  218. ActionContentText.Visibility = Visibility.Visible;
  219. ActionContentText.Text = "";
  220. break;
  221. case 2:
  222. ActionContentText.Visibility = Visibility.Visible;
  223. ActionContentText.Style = this.FindResource("txtboxStyle") as Style;
  224. ActionContentText.Text = "";
  225. break;
  226. default:
  227. break;
  228. }
  229. if (ActionContentText.Visibility != Visibility.Collapsed && ActionContentText != null)
  230. AddAction();
  231. }
  232. }
  233. private void AddAction()
  234. {
  235. Dictionary<C_ACTION_TYPE, string> ActionDict = new Dictionary<C_ACTION_TYPE, string>();
  236. if (TextAlignmentCombox.SelectedIndex == 1 && !string.IsNullOrEmpty(TextAlignmentCombox.Text))
  237. {
  238. int page = 0;
  239. int.TryParse(ActionContentText.Text.Trim(), out page);
  240. if (page <= 0 || page > pdfViewer.Document.PageCount)
  241. page = 1;
  242. if (page - 1 >= 0)
  243. ActionDict[C_ACTION_TYPE.ACTION_TYPE_GOTO] = (page - 1).ToString();
  244. }
  245. if (TextAlignmentCombox.SelectedIndex == 2)
  246. {
  247. if (string.IsNullOrEmpty(ActionContentText.Text.Trim()))
  248. {
  249. ActionDict[C_ACTION_TYPE.ACTION_TYPE_URI] = @"https://www.compdf.com";
  250. }
  251. else
  252. {
  253. ActionDict[C_ACTION_TYPE.ACTION_TYPE_URI] = ActionContentText.Text.Trim();
  254. }
  255. }
  256. if (ActionDict != null && ActionDict.Count > 0)
  257. {
  258. annotAttribEvent?.UpdateAttrib(AnnotAttrib.FormAction, ActionDict);
  259. annotAttribEvent?.UpdateAnnot();
  260. }
  261. }
  262. private void ItemText_TextChanged(object sender, TextChangedEventArgs e)
  263. {
  264. if (IsLoadedData)
  265. {
  266. annotAttribEvent.UpdateAttrib(AnnotAttrib.Text, (sender as TextBox).Text);
  267. annotAttribEvent.UpdateAnnot();
  268. }
  269. }
  270. private void ActionContentText_TextChanged(object sender, TextChangedEventArgs e)
  271. {
  272. AddAction();
  273. }
  274. }
  275. }