PushButtonProperty.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using ComPDFKitViewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Media;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Runtime.CompilerServices;
  11. using Compdfkit_Tools.Helper;
  12. using ComPDFKit.PDFDocument.Action;
  13. using ComPDFKit.PDFAnnotation;
  14. using ComPDFKit.PDFDocument;
  15. using ComPDFKit.Tool;
  16. using ComPDFKit.Tool.Help;
  17. using System.Windows.Input;
  18. using ComPDFKit.PDFAnnotation.Form;
  19. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  20. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
  21. namespace Compdfkit_Tools.PDFControl
  22. {
  23. public partial class PushButtonProperty : UserControl,INotifyPropertyChanged
  24. {
  25. private PushButtonParam widgetParam = null;
  26. private CPDFPushButtonWidget cPDFAnnotation = null;
  27. private PDFViewControl pdfViewerControl = null;
  28. private CPDFDocument cPDFDocument = null;
  29. public ObservableCollection<int> SizeList { get; set; } = new ObservableCollection<int>
  30. {
  31. 6,8,9,10,12,14,18,20,24,26,28,32,30,32,48,72
  32. };
  33. private string _HintText;
  34. public string HintText
  35. {
  36. get => _HintText;
  37. set => SetField(ref _HintText, value);
  38. }
  39. bool IsLoadedData = false;
  40. public PushButtonProperty()
  41. {
  42. InitializeComponent();
  43. DataContext = this;
  44. }
  45. #region Loaded
  46. public void SetProperty(AnnotParam annotParam, CPDFAnnotation annotation, CPDFDocument doc, PDFViewControl cPDFViewer)
  47. {
  48. widgetParam = (PushButtonParam)annotParam;
  49. cPDFAnnotation = (CPDFPushButtonWidget)annotation;
  50. pdfViewerControl = cPDFViewer;
  51. cPDFDocument = doc;
  52. }
  53. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  54. {
  55. Binding SizeListbinding = new Binding();
  56. SizeListbinding.Source = this;
  57. SizeListbinding.Path = new System.Windows.PropertyPath("SizeList");
  58. FontSizeCmb.SetBinding(ComboBox.ItemsSourceProperty, SizeListbinding);
  59. TopTabControl.SelectedIndex = 2;
  60. FieldNameText.Text = widgetParam.FieldName;
  61. FormFieldCmb.SelectedIndex = (int)ParamConverter.ConverterWidgetFormFlags(widgetParam.Flags, widgetParam.IsHidden);
  62. BorderColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.LineColor));
  63. BackgroundColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.BgColor));
  64. TextColorPickerControl.SetCheckedForColor(ParamConverter.ConverterByteForColor(widgetParam.FontColor));
  65. SetFontName(widgetParam.FontName);
  66. SetFontStyle(widgetParam.IsItalic, widgetParam.IsBold);
  67. SetFontSize(widgetParam.FontSize);
  68. ItemText.Text = widgetParam.Text;
  69. SetActionContext();
  70. IsLoadedData = true;
  71. }
  72. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  73. {
  74. IsLoadedData = false;
  75. }
  76. private void SetActionContext()
  77. {
  78. switch (widgetParam.Action)
  79. {
  80. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  81. TextAlignmentCmb.SelectedIndex = 1;
  82. ActionContentText.Visibility = Visibility.Visible;
  83. ActionContentText.Text = (widgetParam.DestinationPageIndex + 1).ToString();
  84. break;
  85. case C_ACTION_TYPE.ACTION_TYPE_URI:
  86. TextAlignmentCmb.SelectedIndex = 2;
  87. ActionContentText.Visibility = Visibility.Visible;
  88. ActionContentText.Text = widgetParam.Uri;
  89. break;
  90. default:
  91. TextAlignmentCmb.SelectedIndex = 0;
  92. ActionContentText.Text = "";
  93. break;
  94. }
  95. }
  96. private void SetFontSize(double size)
  97. {
  98. int index = SizeList.IndexOf((int)size);
  99. FontSizeCmb.SelectedIndex = index;
  100. }
  101. private void SetFontStyle(bool IsItalic, bool IsBold)
  102. {
  103. int index = 0;
  104. if (IsItalic && IsBold)
  105. {
  106. index = 3;
  107. }
  108. else if (IsItalic)
  109. {
  110. index = 2;
  111. }
  112. else if (IsBold)
  113. {
  114. index = 1;
  115. }
  116. FontStyleCmb.SelectedIndex = index;
  117. }
  118. private void SetFontName(string fontName)
  119. {
  120. int index = -1;
  121. List<string> fontFamilyList = new List<string>() { "Helvetica", "Courier", "Times" };
  122. for (int i = 0; i < fontFamilyList.Count; i++)
  123. {
  124. if (fontFamilyList[i].ToLower().Contains(fontName.ToLower())
  125. || fontName.ToLower().Contains(fontFamilyList[i].ToLower()))
  126. {
  127. index = i;
  128. }
  129. }
  130. FontCmb.SelectedIndex = index;
  131. }
  132. #endregion
  133. #region Updata
  134. private void FieldNameText_TextChanged(object sender, TextChangedEventArgs e)
  135. {
  136. if (IsLoadedData)
  137. {
  138. cPDFAnnotation.SetFieldName((sender as TextBox).Text);
  139. pdfViewerControl.UpdateAnnotFrame();
  140. }
  141. }
  142. private void FormFieldCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  143. {
  144. if (IsLoadedData)
  145. {
  146. cPDFAnnotation.SetFlags(ParamConverter.GetFormFlags((ParamConverter.FormField)(sender as ComboBox).SelectedIndex, cPDFAnnotation));
  147. pdfViewerControl.UpdateAnnotFrame();
  148. }
  149. }
  150. private void BorderColorPickerControl_ColorChanged(object sender, EventArgs e)
  151. {
  152. if (IsLoadedData)
  153. {
  154. byte[] Color = new byte[3];
  155. Color[0] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.R;
  156. Color[1] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.G;
  157. Color[2] = ((SolidColorBrush)BorderColorPickerControl.Brush).Color.B;
  158. cPDFAnnotation.SetWidgetBorderRGBColor(Color);
  159. pdfViewerControl.UpdateAnnotFrame();
  160. }
  161. }
  162. private void BackgroundColorPickerControl_ColorChanged(object sender, EventArgs e)
  163. {
  164. if (IsLoadedData)
  165. {
  166. byte[] Color = new byte[3];
  167. Color[0] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.R;
  168. Color[1] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.G;
  169. Color[2] = ((SolidColorBrush)BackgroundColorPickerControl.Brush).Color.B;
  170. cPDFAnnotation.SetWidgetBgRGBColor(Color);
  171. pdfViewerControl.UpdateAnnotFrame();
  172. }
  173. }
  174. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  175. {
  176. if (IsLoadedData)
  177. {
  178. byte[] Color = new byte[3];
  179. Color[0] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.R;
  180. Color[1] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.G;
  181. Color[2] = ((SolidColorBrush)TextColorPickerControl.Brush).Color.B;
  182. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  183. cTextAttribute.FontColor = Color;
  184. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  185. cPDFAnnotation.UpdateFormAp();
  186. pdfViewerControl.UpdateAnnotFrame();
  187. }
  188. }
  189. private void FontCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  190. {
  191. if (IsLoadedData)
  192. {
  193. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  194. bool isBold = IsBold(cTextAttribute.FontName);
  195. bool isItalic = IsItalic(cTextAttribute.FontName);
  196. FontType fontType = GetFontType((sender as ComboBox).SelectedItem?.ToString());
  197. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  198. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  199. cPDFAnnotation.UpdateFormAp();
  200. pdfViewerControl.UpdateAnnotFrame();
  201. }
  202. }
  203. private void FontStyleCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  204. {
  205. if (IsLoadedData)
  206. {
  207. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  208. bool isItalic = false;
  209. bool isBold = false;
  210. switch ((sender as ComboBox).SelectedIndex)
  211. {
  212. case 0:
  213. break;
  214. case 1:
  215. isItalic = IsItalic(cTextAttribute.FontName);
  216. isBold = true;
  217. break;
  218. case 2:
  219. isItalic = true;
  220. isBold = IsBold(cTextAttribute.FontName);
  221. break;
  222. case 3:
  223. isItalic = true;
  224. isBold = true;
  225. break;
  226. default:
  227. break;
  228. }
  229. FontType fontType = GetFontType(cTextAttribute.FontName);
  230. cTextAttribute.FontName = ObtainFontName(fontType, isBold, isItalic);
  231. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  232. cPDFAnnotation.UpdateFormAp();
  233. pdfViewerControl.UpdateAnnotFrame();
  234. }
  235. }
  236. private void FontSizeCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  237. {
  238. if (IsLoadedData)
  239. {
  240. CTextAttribute cTextAttribute = cPDFAnnotation.GetTextAttribute();
  241. cTextAttribute.FontSize = Convert.ToSingle((sender as ComboBox).SelectedItem);
  242. cPDFAnnotation.SetTextAttribute(cTextAttribute);
  243. cPDFAnnotation.UpdateFormAp();
  244. pdfViewerControl.UpdateAnnotFrame();
  245. }
  246. }
  247. #endregion
  248. private void TextAlignmentCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
  249. {
  250. if (IsLoadedData)
  251. {
  252. ActionContentText.Style = null;
  253. switch (TextAlignmentCmb.SelectedIndex)
  254. {
  255. case 0:
  256. ActionContentText.Visibility = Visibility.Collapsed;
  257. break;
  258. case 1:
  259. ActionContentText.Visibility = Visibility.Visible;
  260. HintText = LanguageHelper.PropertyPanelManager.GetString("Holder_Jump") + pdfViewerControl.GetCPDFViewer().GetDocument().PageCount;
  261. ActionContentText.Style = this.FindResource("txtboxStyle") as Style;
  262. ActionContentText.Text = "";
  263. break;
  264. case 2:
  265. ActionContentText.Visibility = Visibility.Visible;
  266. HintText = "https://www.compdf.com";
  267. ActionContentText.Style = this.FindResource("txtboxStyle") as Style;
  268. ActionContentText.Text = "";
  269. break;
  270. default:
  271. break;
  272. }
  273. if (ActionContentText.Visibility != Visibility.Collapsed && ActionContentText != null)
  274. AddAction();
  275. }
  276. }
  277. private void AddAction()
  278. {
  279. Dictionary<C_ACTION_TYPE, string> ActionDict = new Dictionary<C_ACTION_TYPE, string>();
  280. if (TextAlignmentCmb.SelectedIndex == 1 && !string.IsNullOrEmpty(TextAlignmentCmb.Text))
  281. {
  282. int page = 0;
  283. int.TryParse(ActionContentText.Text.Trim(), out page);
  284. if (page <= 0 || page > pdfViewerControl.PDFViewTool.GetCPDFViewer().GetDocument().PageCount)
  285. {
  286. page = 1;
  287. }
  288. if (page - 1 >= 0)
  289. {
  290. CPDFGoToAction gotoAction = new CPDFGoToAction();
  291. CPDFDestination destination = new CPDFDestination();
  292. destination.Position_X = 0;
  293. destination.Position_Y = 0;
  294. destination.PageIndex = page;
  295. gotoAction.SetDestination(cPDFDocument, destination);
  296. cPDFAnnotation.SetButtonAction(gotoAction);
  297. }
  298. }
  299. if (TextAlignmentCmb.SelectedIndex == 2)
  300. {
  301. CPDFUriAction uriAction = new CPDFUriAction();
  302. if (string.IsNullOrEmpty(ActionContentText.Text.Trim()))
  303. {
  304. uriAction.SetUri(@"https://www.compdf.com");
  305. }
  306. else
  307. {
  308. uriAction.SetUri(ActionContentText.Text.Trim());
  309. }
  310. cPDFAnnotation.SetButtonAction(uriAction);
  311. }
  312. pdfViewerControl.UpdateAnnotFrame();
  313. }
  314. private void ItemText_TextChanged(object sender, TextChangedEventArgs e)
  315. {
  316. if (IsLoadedData)
  317. {
  318. cPDFAnnotation.SetButtonTitle((sender as TextBox).Text);
  319. pdfViewerControl.UpdateAnnotFrame();
  320. }
  321. }
  322. private void ActionContentText_TextChanged(object sender, TextChangedEventArgs e)
  323. {
  324. if (IsLoadedData)
  325. {
  326. AddAction();
  327. }
  328. }
  329. public event PropertyChangedEventHandler PropertyChanged;
  330. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  331. {
  332. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  333. }
  334. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  335. {
  336. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  337. field = value;
  338. OnPropertyChanged(propertyName);
  339. return true;
  340. }
  341. }
  342. }