PushButtonProperty.xaml.cs 21 KB

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