PushButtonProperty.xaml.cs 22 KB

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