PushButtonProperty.xaml.cs 12 KB

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