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