TextEditPropertyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. namespace PDF_Office.ViewModels.PropertyPanel
  18. {
  19. public class TextEditPropertyViewModel : BindableBase, INavigationAware
  20. {
  21. #region 属性
  22. private Brush selectColor = new SolidColorBrush(Colors.Black);
  23. public Brush SelectColor
  24. {
  25. get { return selectColor; }
  26. set
  27. {
  28. SetProperty(ref selectColor, value);
  29. if (TextEditEvent != null)
  30. {
  31. bool isok = TextEditEvent.FontColor.A != (SelectColor as SolidColorBrush).Color.A ||
  32. TextEditEvent.FontColor.B != (SelectColor as SolidColorBrush).Color.B ||
  33. TextEditEvent.FontColor.G != (SelectColor as SolidColorBrush).Color.G ||
  34. TextEditEvent.FontColor.R != (SelectColor as SolidColorBrush).Color.R;
  35. if (isok)
  36. {
  37. TextEditEvent.FontColor = (SelectColor as SolidColorBrush).Color;
  38. TextEditEvent.UpdatePDFEditByEventArgs();
  39. }
  40. }
  41. }
  42. }
  43. private FontFamily fontFamily = new FontFamily("Courier");
  44. public FontFamily TextFontFamily
  45. {
  46. get { return fontFamily; }
  47. set
  48. {
  49. SetProperty(ref fontFamily, value);
  50. if (TextEditEvent != null)
  51. {
  52. TextEditEvent.FontFamily = fontFamily;
  53. TextEditEvent.UpdatePDFEditByEventArgs();
  54. }
  55. }
  56. }
  57. private FontWeight fontWeights = FontWeights.Normal;
  58. public FontWeight TextFontWeights
  59. {
  60. get { return fontWeights; }
  61. set
  62. {
  63. SetProperty(ref fontWeights, value);
  64. if (TextEditEvent != null)
  65. {
  66. TextEditEvent.FontWeight = fontWeights;
  67. TextEditEvent.UpdatePDFEditByEventArgs();
  68. }
  69. }
  70. }
  71. private FontStyle fontStyle = FontStyles.Normal;
  72. public FontStyle TextFontStyle
  73. {
  74. get { return fontStyle; }
  75. set
  76. {
  77. SetProperty(ref fontStyle, value);
  78. if (TextEditEvent != null)
  79. {
  80. TextEditEvent.FontStyle = fontStyle;
  81. TextEditEvent.UpdatePDFEditByEventArgs();
  82. }
  83. }
  84. }
  85. private int fontSize = 24;
  86. public int TextFontSize
  87. {
  88. get { return fontSize; }
  89. set
  90. {
  91. SetProperty(ref fontSize, value);
  92. if (TextEditEvent != null)
  93. {
  94. TextEditEvent.FontSize = fontSize;
  95. TextEditEvent.UpdatePDFEditByEventArgs();
  96. }
  97. }
  98. }
  99. private List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
  100. public List<FontStyleItem> FontStyleList
  101. {
  102. get { return fontStyleList; }
  103. set
  104. {
  105. SetProperty(ref fontStyleList, value);
  106. }
  107. }
  108. #endregion
  109. #region Command
  110. public DelegateCommand<object> SelectedColorCommand { get; set; }
  111. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  112. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  113. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  114. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  115. #endregion
  116. private ComPDFKitViewer.PDFEditEvent TextEditEvent;
  117. public TextEditPropertyViewModel()
  118. {
  119. InitVariable();
  120. InitCommand();
  121. }
  122. private void InitVariable()
  123. {
  124. InitFontStyles();
  125. }
  126. private void InitFontStyles()
  127. {
  128. FontStyleList = LoadFontStyle.Load();
  129. }
  130. private void InitCommand()
  131. {
  132. SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
  133. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  134. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged);
  135. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged);
  136. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
  137. }
  138. private void SelectedColor(object obj)
  139. {
  140. if (obj != null)
  141. {
  142. var colorValue = (Color)obj;
  143. if (colorValue != null)
  144. {
  145. SelectColor = new SolidColorBrush(colorValue);
  146. }
  147. }
  148. }
  149. private void SelectedFontStyle(object obj)
  150. {
  151. if (obj != null && (FontStyleItem)obj != null)
  152. {
  153. var item = (FontStyleItem)obj;
  154. }
  155. }
  156. private void FontFamilyChanged(object obj)
  157. {
  158. if (obj != null)
  159. {
  160. if ((int)obj > -1)
  161. {
  162. if ((int)obj == 0)
  163. {
  164. TextFontFamily = new FontFamily("Courier");
  165. }
  166. if ((int)obj == 1)
  167. {
  168. TextFontFamily = new FontFamily("Helvetica");
  169. }
  170. if ((int)obj == 2)
  171. {
  172. TextFontFamily = new FontFamily("Times");
  173. }
  174. }
  175. }
  176. }
  177. private void FontStyleChanged(object obj)
  178. {
  179. if (obj != null)
  180. {
  181. var item = (ComboBoxItem)obj;
  182. var content = (string)item.Content;
  183. if (content != null)
  184. {
  185. if (content == "Regular")
  186. {
  187. TextFontWeights = FontWeights.Normal;
  188. TextFontStyle = FontStyles.Normal;
  189. }
  190. if (content == "Bold")
  191. {
  192. TextFontWeights = FontWeights.Bold;
  193. TextFontStyle = FontStyles.Normal;
  194. }
  195. if (content == "Italic")
  196. {
  197. TextFontWeights = FontWeights.Normal;
  198. TextFontStyle = FontStyles.Italic;
  199. }
  200. if (content == "Bold Italic")
  201. {
  202. TextFontWeights = FontWeights.Bold;
  203. TextFontStyle = FontStyles.Italic;
  204. }
  205. }
  206. }
  207. }
  208. private void FontSizeChanged(object obj)
  209. {
  210. if (obj != null)
  211. {
  212. var item = (ComboBoxItem)obj;
  213. var content = (string)item.Content;
  214. if (content != null)
  215. {
  216. var intData = int.Parse(content);
  217. TextFontSize = intData;
  218. }
  219. }
  220. }
  221. public void OnNavigatedTo(NavigationContext navigationContext)
  222. {
  223. }
  224. public bool IsNavigationTarget(NavigationContext navigationContext)
  225. {
  226. return true;
  227. }
  228. private CPDFViewer PDFViewer;
  229. public void OnNavigatedFrom(NavigationContext navigationContext)
  230. {
  231. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  232. if(PDFViewer != null)
  233. {
  234. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  235. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  236. PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
  237. PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
  238. }
  239. }
  240. private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
  241. {
  242. ContextMenu popMenu = new ContextMenu();
  243. var menu = new MenuItem();
  244. menu.Header = "Cut";
  245. popMenu.Items.Add(menu);
  246. menu = new MenuItem();
  247. menu.Header = "Copy";
  248. popMenu.Items.Add(menu);
  249. menu = new MenuItem();
  250. menu.Header = "Paste";
  251. popMenu.Items.Add(menu);
  252. menu = new MenuItem();
  253. menu.Header = "Delete";
  254. popMenu.Items.Add(menu);
  255. menu = new MenuItem();
  256. menu.Header = "SelectAll";
  257. popMenu.Items.Add(menu);
  258. switch (e.CommandType)
  259. {
  260. case CommandType.Context:
  261. if (popMenu.Items.Count == 5)
  262. {
  263. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  264. menuItem.CommandTarget = (UIElement)sender;
  265. menuItem.Command = ApplicationCommands.Cut;
  266. menuItem = popMenu.Items[1] as MenuItem;
  267. menuItem.CommandTarget = (UIElement)sender;
  268. menuItem.Command = ApplicationCommands.Copy;
  269. menuItem = popMenu.Items[2] as MenuItem;
  270. menuItem.CommandTarget = (UIElement)sender;
  271. menuItem.Command = ApplicationCommands.Paste;
  272. menuItem = popMenu.Items[3] as MenuItem;
  273. menuItem.CommandTarget = (UIElement)sender;
  274. menuItem.Command = ApplicationCommands.Delete;
  275. menuItem = popMenu.Items[4] as MenuItem;
  276. menuItem.CommandTarget = (UIElement)sender;
  277. menuItem.Command = ApplicationCommands.SelectAll;
  278. e.PopupMenu = popMenu;
  279. if (e.PopupMenu != null)
  280. {
  281. e.Handle = true;
  282. }
  283. }
  284. break;
  285. default:
  286. e.DoCommand();
  287. break;
  288. }
  289. }
  290. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  291. {
  292. if(e != null && e.Count > 0)
  293. {
  294. TextEditEvent = e[0];
  295. }
  296. else
  297. {
  298. // TextEditEvent = null;
  299. }
  300. }
  301. }
  302. }