FreetextAnnotPropertyViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  5. using PDF_Office.ViewModels.Tools;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Media;
  17. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  18. {
  19. public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware
  20. {
  21. private double fillOpacity = 1;
  22. public double FillOpacity
  23. {
  24. get { return fillOpacity; }
  25. set
  26. {
  27. SetProperty(ref fillOpacity, value);
  28. }
  29. }
  30. private Brush selectColor = new SolidColorBrush(Colors.Black);
  31. public Brush SelectColor
  32. {
  33. get { return selectColor; }
  34. set
  35. {
  36. SetProperty(ref selectColor, value);
  37. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, (selectColor as SolidColorBrush).Color);
  38. AnnotEvent?.UpdateAnnot();
  39. }
  40. }
  41. private Brush fillColor = new SolidColorBrush(Colors.Transparent);
  42. public Brush FillColor
  43. {
  44. get { return fillColor; }
  45. set
  46. {
  47. SetProperty(ref fillColor, value);
  48. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, (fillColor as SolidColorBrush).Color);
  49. AnnotEvent?.UpdateAnnot();
  50. }
  51. }
  52. private FontFamily fontFamily = new FontFamily("Courier");
  53. public FontFamily TextFontFamily
  54. {
  55. get { return fontFamily; }
  56. set
  57. {
  58. SetProperty(ref fontFamily, value);
  59. }
  60. }
  61. private FontWeight fontWeights = FontWeights.Normal;
  62. public FontWeight TextFontWeights
  63. {
  64. get { return fontWeights; }
  65. set
  66. {
  67. SetProperty(ref fontWeights, value);
  68. }
  69. }
  70. private FontStyle fontStyle = FontStyles.Normal;
  71. public FontStyle TextFontStyle
  72. {
  73. get { return fontStyle; }
  74. set
  75. {
  76. SetProperty(ref fontStyle, value);
  77. }
  78. }
  79. private int fontSize = 24;
  80. public int TextFontSize
  81. {
  82. get { return fontSize; }
  83. set
  84. {
  85. SetProperty(ref fontSize, value);
  86. }
  87. }
  88. private List<FontStyleItem> fontStyleList = new List<FontStyleItem>();
  89. public List<FontStyleItem> FontStyleList
  90. {
  91. get { return fontStyleList; }
  92. set
  93. {
  94. SetProperty(ref fontStyleList, value);
  95. }
  96. }
  97. public DelegateCommand<object> SelectedFillOpacityCommand { get; set; }
  98. public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
  99. public DelegateCommand<object> SelectedColorCommand { get; set; }
  100. public DelegateCommand<object> SelectedFillColorCommand { get; set; }
  101. public DelegateCommand<object> FontFamilyChangedCommand { get; set; }
  102. public DelegateCommand<object> FontStyleChangedCommand { get; set; }
  103. public DelegateCommand<object> FontSizeChangedCommand { get; set; }
  104. public DelegateCommand<object> TextAlignChecked { get; set; }
  105. public event EventHandler<object> LoadPropertyHandler;
  106. public FreetextAnnotPropertyViewModel()
  107. {
  108. SelectedFillOpacityCommand = new DelegateCommand<object>(SelectedFillOpacity);
  109. SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
  110. SelectedColorCommand = new DelegateCommand<object>(SelectedColor_Command);
  111. SelectedFillColorCommand = new DelegateCommand<object>(SelectedFillColor_Command);
  112. FontFamilyChangedCommand = new DelegateCommand<object>(FontFamilyChanged_Command);
  113. FontStyleChangedCommand = new DelegateCommand<object>(FontStyleChanged_Command);
  114. FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged_Command);
  115. TextAlignChecked = new DelegateCommand<object>(TextAlign_Checked);
  116. InitVariable();
  117. }
  118. private void InitVariable()
  119. {
  120. InitFontStyles();
  121. }
  122. private void InitFontStyles()
  123. {
  124. FontStyleItem custom = new FontStyleItem();
  125. custom.mFontSize = 32;
  126. custom.mFontStyleName = "自定义";
  127. FontStyleItem h1 = new FontStyleItem();
  128. h1.mFontSize = 24;
  129. h1.mFontStyleName = "H1大标题";
  130. FontStyleItem h2 = new FontStyleItem();
  131. h2.mFontSize = 16;
  132. h2.mFontStyleName = "h2(标准)";
  133. FontStyleItem h3 = new FontStyleItem();
  134. h3.mFontSize = 10;
  135. h3.mFontStyleName = "H3小标题";
  136. FontStyleItem b1 = new FontStyleItem();
  137. b1.mFontSize = 8;
  138. b1.mFontStyleName = "B1标题";
  139. FontStyleItem b2 = new FontStyleItem();
  140. b2.mFontSize = 6;
  141. b2.mFontStyleName = "B2标题";
  142. FontStyleItem b3 = new FontStyleItem();
  143. b3.mFontSize = 4;
  144. b3.mFontStyleName = "B3标题";
  145. FontStyleList.Add(custom);
  146. FontStyleList.Add(h1);
  147. FontStyleList.Add(h2);
  148. FontStyleList.Add(h3);
  149. FontStyleList.Add(b1);
  150. FontStyleList.Add(b2);
  151. FontStyleList.Add(b3);
  152. }
  153. private void TextAlign_Checked(object obj)
  154. {
  155. if (obj != null && (string)obj != null)
  156. {
  157. var tag = (string)obj;
  158. switch (tag)
  159. {
  160. case "AlignLeft":
  161. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  162. break;
  163. case "AlignCenter":
  164. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  165. break;
  166. case "AlignRight":
  167. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  168. break;
  169. }
  170. AnnotEvent?.UpdateAnnot();
  171. }
  172. }
  173. private void SelectedFontStyle(object obj)
  174. {
  175. if (obj != null && (FontStyleItem)obj != null)
  176. {
  177. var item = (FontStyleItem)obj;
  178. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, item.mFontSize);
  179. AnnotEvent?.UpdateAnnot();
  180. }
  181. }
  182. private void SelectedFillOpacity(object obj)
  183. {
  184. if (obj != null)
  185. {
  186. FillOpacity = (double)obj;
  187. SelectColor.Opacity = FillOpacity;
  188. AnnotEvent?.UpdateAttrib(AnnotAttrib.Transparency, FillOpacity);
  189. AnnotEvent?.UpdateAnnot();
  190. }
  191. }
  192. private void FontSizeChanged_Command(object obj)
  193. {
  194. if (obj != null)
  195. {
  196. var item = (ComboBoxItem)obj;
  197. var content = (string)item.Content;
  198. if (content != null)
  199. {
  200. var intData = int.Parse(content);
  201. TextFontSize = intData;
  202. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontSize, intData);
  203. AnnotEvent?.UpdateAnnot();
  204. }
  205. }
  206. }
  207. private void FontStyleChanged_Command(object obj)
  208. {
  209. if (obj != null)
  210. {
  211. var item = (ComboBoxItem)obj;
  212. var content = (string)item.Content;
  213. if (content != null)
  214. {
  215. if (content == "Regular")
  216. {
  217. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  218. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  219. TextFontWeights = FontWeights.Normal;
  220. TextFontStyle = FontStyles.Normal;
  221. }
  222. if (content == "Bold")
  223. {
  224. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Normal);
  225. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  226. TextFontWeights = FontWeights.Bold;
  227. TextFontStyle = FontStyles.Normal;
  228. }
  229. if (content == "Italic")
  230. {
  231. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  232. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Normal);
  233. TextFontWeights = FontWeights.Normal;
  234. TextFontStyle = FontStyles.Italic;
  235. }
  236. if (content == "Bold Italic")
  237. {
  238. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontStyle, FontStyles.Italic);
  239. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontWeight, FontWeights.Bold);
  240. TextFontWeights = FontWeights.Bold;
  241. TextFontStyle = FontStyles.Italic;
  242. }
  243. AnnotEvent?.UpdateAnnot();
  244. }
  245. }
  246. }
  247. private void FontFamilyChanged_Command(object obj)
  248. {
  249. if (obj != null)
  250. {
  251. if ((int)obj > -1)
  252. {
  253. if ((int)obj == 0)
  254. {
  255. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Courier"));
  256. TextFontFamily = new FontFamily("Courier");
  257. }
  258. if ((int)obj == 1)
  259. {
  260. TextFontFamily = new FontFamily("Helvetica");
  261. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Helvetica"));
  262. }
  263. if ((int)obj == 2)
  264. {
  265. TextFontFamily = new FontFamily("Times");
  266. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily("Times"));
  267. }
  268. AnnotEvent?.UpdateAnnot();
  269. }
  270. }
  271. }
  272. private void SelectedFillColor_Command(object obj)
  273. {
  274. if (obj != null)
  275. {
  276. var colorValue = (Color)obj;
  277. if (colorValue != null)
  278. {
  279. FillColor = new SolidColorBrush(colorValue);
  280. FillColor.Opacity = FillOpacity;
  281. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  282. changeData[AnnotArgsType.AnnotFreehand] = obj;
  283. PropertyPanel.DataChangedInvoke(this, changeData);
  284. }
  285. }
  286. }
  287. private void SelectedColor_Command(object obj)
  288. {
  289. if (obj != null)
  290. {
  291. var colorValue = (Color)obj;
  292. if (colorValue != null)
  293. {
  294. SelectColor = new SolidColorBrush(colorValue);
  295. SelectColor.Opacity = FillOpacity;
  296. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  297. changeData[AnnotArgsType.AnnotFreehand] = obj;
  298. PropertyPanel.DataChangedInvoke(this, changeData);
  299. }
  300. }
  301. }
  302. public bool IsNavigationTarget(NavigationContext navigationContext)
  303. {
  304. return true;
  305. }
  306. public void OnNavigatedFrom(NavigationContext navigationContext)
  307. {
  308. }
  309. public AnnotAttribEvent AnnotEvent { get; set; }
  310. private FreeTextAnnotArgs Annot;
  311. private AnnotPropertyPanel PropertyPanel;
  312. public void OnNavigatedTo(NavigationContext navigationContext)
  313. {
  314. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  315. if (PropertyPanel != null)
  316. {
  317. AnnotEvent = PropertyPanel.AnnotEvent;
  318. Annot = PropertyPanel.annot as FreeTextAnnotArgs;
  319. LoadPropertyHandler?.Invoke(this, Annot);
  320. }
  321. }
  322. }
  323. }