PushButtonProperty.xaml.cs 11 KB

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