TextAnnotPropertyViewModel.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using ComPDFKitViewer.AnnotEvent;
  2. using PDF_Office.Model;
  3. using PDF_Office.ViewModels.Tools;
  4. using PDFSettings;
  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.Media;
  14. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  15. {
  16. public class TextAnnotPropertyViewModel:BindableBase, INavigationAware
  17. {
  18. #region 属性
  19. private double annotOpacity;
  20. public double AnnotOpacity
  21. {
  22. get { return annotOpacity; }
  23. set { SetProperty(ref annotOpacity, value); SampleTextBg.Opacity = annotOpacity; SetAnnotProperty();
  24. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  25. changeData[AnnotType] = annotOpacity;
  26. PropertyPanel.DataChangedInvoke(this, changeData);
  27. }
  28. }
  29. private AnnotArgsType annotType;
  30. public AnnotArgsType AnnotType
  31. {
  32. get { return annotType; }
  33. set
  34. {
  35. SetProperty(ref annotType, value);
  36. SetAnnotType();
  37. }
  38. }
  39. /// <summary>
  40. /// 示例背景
  41. /// </summary>
  42. private Brush sampleTextBg = new SolidColorBrush(Colors.Transparent);
  43. public Brush SampleTextBg
  44. {
  45. get { return sampleTextBg; }
  46. set { SetProperty(ref sampleTextBg, value); SetAnnotProperty(); }
  47. }
  48. private string annotTypeTitle;
  49. public string AnnotTypeTitle
  50. {
  51. get { return annotTypeTitle; }
  52. set
  53. {
  54. SetProperty(ref annotTypeTitle, value);
  55. }
  56. }
  57. private Color selectColor;
  58. public Color SelectColor
  59. {
  60. get { return selectColor; }
  61. set { SetProperty(ref selectColor, value); SetAnnotProperty(); }
  62. }
  63. #endregion
  64. public DelegateCommand<object> SelectedColorChangedCommand { get; set; }
  65. public DelegateCommand<object> OpacityItemCommand { get; set; }
  66. public TextAnnotPropertyViewModel()
  67. {
  68. SelectedColorChangedCommand = new DelegateCommand<object>(SelectedColorChanged_Click);
  69. OpacityItemCommand = new DelegateCommand<object>(OpacityItemCommand_Click);
  70. }
  71. private void OpacityItemCommand_Click(object obj)
  72. {
  73. if(obj!= null)
  74. {
  75. int opacity = -1;
  76. if(int.TryParse((string)obj, out opacity))
  77. {
  78. AnnotOpacity = opacity/100D;
  79. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  80. changeData[AnnotType] = AnnotOpacity;
  81. PropertyPanel.DataChangedInvoke(this, changeData);
  82. }
  83. }
  84. }
  85. public void SelectedColorChanged_Click(object color)
  86. {
  87. if (color != null)
  88. {
  89. var colorValue = (Color)color;
  90. if (colorValue != null)
  91. {
  92. SampleTextBg = new SolidColorBrush(colorValue);
  93. switch(AnnotType)
  94. {
  95. case AnnotArgsType.AnnotHighlight:
  96. SampleTextBg.Opacity = AnnotOpacity;
  97. break;
  98. case AnnotArgsType.AnnotUnderline:
  99. SampleTextBg.Opacity = AnnotOpacity;
  100. break;
  101. case AnnotArgsType.AnnotStrikeout:
  102. SampleTextBg.Opacity = AnnotOpacity;
  103. break;
  104. case AnnotArgsType.AnnotSquiggly:
  105. SampleTextBg.Opacity = AnnotOpacity;
  106. break;
  107. case AnnotArgsType.AnnotSticky:
  108. SampleTextBg.Opacity = AnnotOpacity;
  109. break;
  110. }
  111. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  112. changeData[AnnotType] = color;
  113. PropertyPanel.DataChangedInvoke(this, changeData);
  114. // PropertyPanel.DataChanged?.Invoke(this, changeData);
  115. }
  116. }
  117. }
  118. private void SetAnnotType()
  119. {
  120. switch (AnnotType)
  121. {
  122. case AnnotArgsType.AnnotHighlight:
  123. AnnotTypeTitle = "高亮";
  124. break;
  125. case AnnotArgsType.AnnotUnderline:
  126. AnnotTypeTitle = "下划线";
  127. break;
  128. case AnnotArgsType.AnnotStrikeout:
  129. AnnotTypeTitle = "删除线";
  130. break;
  131. case AnnotArgsType.AnnotSquiggly:
  132. AnnotTypeTitle = "波浪线";
  133. break;
  134. case AnnotArgsType.AnnotSticky:
  135. AnnotTypeTitle = "??";
  136. break;
  137. }
  138. }
  139. public bool IsNavigationTarget(NavigationContext navigationContext)
  140. {
  141. return true;
  142. }
  143. public void OnNavigatedFrom(NavigationContext navigationContext)
  144. {
  145. }
  146. private ComPDFKitViewer.PdfViewer.CPDFViewer PDFViewer;
  147. private AnnotHandlerEventArgs Annot;
  148. private AnnotPropertyPanel PropertyPanel;
  149. public void OnNavigatedTo(NavigationContext navigationContext)
  150. {
  151. navigationContext.Parameters.TryGetValue<AnnotPropertyPanel>(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel);
  152. navigationContext.Parameters.TryGetValue<ComPDFKitViewer.PdfViewer.CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  153. if(PropertyPanel != null)
  154. {
  155. Annot = PropertyPanel.annot;
  156. GetAnnotProperty();
  157. }
  158. }
  159. private void GetAnnotProperty()
  160. {
  161. if (Annot != null)
  162. {
  163. SampleTextBg = new SolidColorBrush(Colors.Transparent);
  164. switch (Annot.EventType)
  165. {
  166. case AnnotArgsType.AnnotHighlight:
  167. if (Annot is TextHighlightAnnotArgs)
  168. {
  169. var Hightlight = Annot as TextHighlightAnnotArgs;
  170. AnnotOpacity = Hightlight.Transparency;
  171. AnnotType = Hightlight.EventType;
  172. SampleTextBg = new SolidColorBrush(Hightlight.Color);
  173. }
  174. break;
  175. case AnnotArgsType.AnnotUnderline:
  176. {
  177. var Underline = Annot as TextUnderlineAnnotArgs;
  178. AnnotOpacity = Underline.Transparency;
  179. AnnotType = Underline.EventType;
  180. }
  181. break;
  182. case AnnotArgsType.AnnotSquiggly:
  183. {
  184. var Underline = Annot as TextSquigglyAnnotArgs;
  185. AnnotOpacity = Underline.Transparency;
  186. AnnotType = Underline.EventType;
  187. }
  188. break;
  189. //case AnnotArgsType.AnnotStrikeout:
  190. // {
  191. // var Underline = Annot as TextStrikeoutAnnotArgs;
  192. // AnnotOpacity = Underline.Transparency;
  193. // AnnotType = Underline.EventType;
  194. // }
  195. // break;
  196. }
  197. }
  198. }
  199. private void SetAnnotProperty()
  200. {
  201. if (Annot != null)
  202. {
  203. switch (Annot.EventType)
  204. {
  205. case AnnotArgsType.AnnotHighlight:
  206. if (Annot is TextHighlightAnnotArgs)
  207. {
  208. var Hightlight = Annot as TextHighlightAnnotArgs;
  209. if (Hightlight.Transparency != AnnotOpacity)
  210. Hightlight.Transparency = AnnotOpacity;
  211. var c = (SampleTextBg as SolidColorBrush).Color;
  212. if (Hightlight.Color.A != c.A || Hightlight.Color.B != c.B || Hightlight.Color.G != c.G || Hightlight.Color.R != c.R)
  213. Hightlight.Color = (SampleTextBg as SolidColorBrush).Color;
  214. }
  215. break;
  216. case AnnotArgsType.AnnotUnderline:
  217. {
  218. var Underline = Annot as TextUnderlineAnnotArgs;
  219. if (Underline.Transparency != AnnotOpacity)
  220. Underline.Transparency = AnnotOpacity;
  221. }
  222. break;
  223. case AnnotArgsType.AnnotSquiggly:
  224. {
  225. var Squiggly = Annot as TextSquigglyAnnotArgs;
  226. if (Squiggly.Transparency != AnnotOpacity)
  227. AnnotOpacity = Squiggly.Transparency;
  228. }
  229. break;
  230. case AnnotArgsType.AnnotStrikeout:
  231. {
  232. var Strikeout = Annot as TextStrikeoutAnnotArgs;
  233. if (Strikeout.Transparency != AnnotOpacity)
  234. Strikeout.Transparency = AnnotOpacity;
  235. }
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. }