PushButtonProperty.xaml.cs 13 KB

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