ScreenAnnotationDialogViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using Dropbox.Api.Users;
  4. using DryIoc;
  5. using ImTools;
  6. using Microsoft.Office.Interop.Word;
  7. using PDF_Office.CustomControl.CompositeControl;
  8. using PDF_Office.Model;
  9. using PDF_Office.Model.BOTA;
  10. using Prism.Commands;
  11. using Prism.Common;
  12. using Prism.Mvvm;
  13. using Prism.Services.Dialogs;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Drawing;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Annotations;
  23. using System.Windows.Media;
  24. namespace PDF_Office.ViewModels.Dialog.BOTA
  25. {
  26. public class AuthorItem : BindableBase
  27. {
  28. private string name;
  29. public string Name
  30. {
  31. get { return name; }
  32. set
  33. {
  34. SetProperty(ref name, value);
  35. }
  36. }
  37. public AuthorItem(string name)
  38. {
  39. Name = name;
  40. }
  41. }
  42. public class Compare<T, C> : IEqualityComparer<T>
  43. {
  44. private Func<T, C> _getField;
  45. public Compare(Func<T, C> getfield)
  46. {
  47. this._getField = getfield;
  48. }
  49. public bool Equals(T x, T y)
  50. {
  51. return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
  52. }
  53. public int GetHashCode(T obj)
  54. {
  55. return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
  56. }
  57. }
  58. public static class CommonHelper
  59. {
  60. /// <summary>
  61. /// 自定义Distinct扩展方法
  62. /// </summary>
  63. /// <typeparam name="T">要去重的对象类</typeparam>
  64. /// <typeparam name="C">自定义去重的字段类型</typeparam>
  65. /// <param name="source">要去重的对象</param>
  66. /// <param name="getfield">获取自定义去重字段的委托</param>
  67. /// <returns></returns>
  68. public static IEnumerable<T> MyDistinct<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
  69. {
  70. return source.Distinct(new Compare<T, C>(getfield));
  71. }
  72. }
  73. internal class ScreenAnnotationDialogViewModel : BindableBase, IDialogAware
  74. {
  75. public string Title => "";
  76. public event Action<IDialogResult> RequestClose;
  77. #region 类型的显示隐藏
  78. /// <summary>
  79. /// 高亮字体
  80. /// </summary>
  81. private Visibility highlightVisibility = Visibility.Collapsed;
  82. public Visibility HighlightVisibility
  83. {
  84. get { return highlightVisibility; }
  85. set
  86. {
  87. SetProperty(ref highlightVisibility, value);
  88. }
  89. }
  90. /// <summary>
  91. /// 手绘
  92. /// </summary>
  93. private Visibility freeHandVisibility = Visibility.Collapsed;
  94. public Visibility FreeHandVisibility
  95. {
  96. get { return freeHandVisibility; }
  97. set
  98. {
  99. SetProperty(ref freeHandVisibility, value);
  100. }
  101. }
  102. /// <summary>
  103. /// 便签
  104. /// </summary>
  105. private Visibility annotStickyVisibility = Visibility.Collapsed;
  106. public Visibility AnnotStickyVisibility
  107. {
  108. get { return annotStickyVisibility; }
  109. set
  110. {
  111. SetProperty(ref annotStickyVisibility, value);
  112. }
  113. }
  114. /// <summary>
  115. /// 图章
  116. /// </summary>
  117. private Visibility annotStampVisibility = Visibility.Collapsed;
  118. public Visibility AnnotStampVisibility
  119. {
  120. get { return annotStampVisibility; }
  121. set
  122. {
  123. SetProperty(ref annotStampVisibility, value);
  124. }
  125. }
  126. /// <summary>
  127. /// 线
  128. /// </summary>
  129. private Visibility sharpLineVisibility = Visibility.Collapsed;
  130. public Visibility SharpLineVisibility
  131. {
  132. get { return sharpLineVisibility; }
  133. set
  134. {
  135. SetProperty(ref sharpLineVisibility, value);
  136. }
  137. }
  138. /// <summary>
  139. /// 箭头
  140. /// </summary>
  141. private Visibility sharpArrowVisibility = Visibility.Collapsed;
  142. public Visibility SharpArrowVisibility
  143. {
  144. get { return sharpArrowVisibility; }
  145. set
  146. {
  147. SetProperty(ref sharpArrowVisibility, value);
  148. }
  149. }
  150. /// <summary>
  151. /// 圆
  152. /// </summary>
  153. private Visibility annotCircleVisibility = Visibility.Collapsed;
  154. public Visibility AnnotCircleVisibility
  155. {
  156. get { return annotCircleVisibility; }
  157. set
  158. {
  159. SetProperty(ref annotCircleVisibility, value);
  160. }
  161. }
  162. /// <summary>
  163. /// 矩形
  164. /// </summary>
  165. private Visibility annotSquareVisibility = Visibility.Collapsed;
  166. public Visibility AnnotSquareVisibility
  167. {
  168. get { return annotSquareVisibility; }
  169. set
  170. {
  171. SetProperty(ref annotSquareVisibility, value);
  172. }
  173. }
  174. /// <summary>
  175. /// 文本注释
  176. /// </summary>
  177. private Visibility annotFreeTextVisibility = Visibility.Collapsed;
  178. public Visibility AnnotFreeTextVisibility
  179. {
  180. get { return annotFreeTextVisibility; }
  181. set
  182. {
  183. SetProperty(ref annotFreeTextVisibility, value);
  184. }
  185. }
  186. /// <summary>
  187. /// 删除线
  188. /// </summary>
  189. private Visibility annotStrikeoutVisibility = Visibility.Collapsed;
  190. public Visibility AnnotStrikeoutVisibility
  191. {
  192. get { return annotStrikeoutVisibility; }
  193. set
  194. {
  195. SetProperty(ref annotStrikeoutVisibility, value);
  196. }
  197. }
  198. /// <summary>
  199. /// 下划线
  200. /// </summary>
  201. private Visibility underLineVisibility = Visibility.Collapsed;
  202. public Visibility UnderLineVisibility
  203. {
  204. get { return underLineVisibility; }
  205. set
  206. {
  207. SetProperty(ref underLineVisibility, value);
  208. }
  209. }
  210. #endregion 类型的显示隐藏
  211. public DelegateCommand CancelCommand { get; set; }
  212. public DelegateCommand CreateCommnad { get; set; }
  213. public ObservableCollection<AnnotationHandlerEventArgs> AnnotationListItems { get; set; }
  214. private ObservableCollection<ColorItem> annotationColors = new ObservableCollection<ColorItem>();
  215. public ObservableCollection<ColorItem> AnnotationColors
  216. {
  217. get { return annotationColors; }
  218. set
  219. {
  220. SetProperty(ref annotationColors, value);
  221. }
  222. }
  223. private ObservableCollection<AuthorItem> annotationAuthor = new ObservableCollection<AuthorItem>();
  224. public ObservableCollection<AuthorItem> AnnotationAuthor
  225. {
  226. get { return annotationAuthor; }
  227. set
  228. {
  229. SetProperty(ref annotationAuthor, value);
  230. }
  231. }
  232. //private List<System.Windows.Media.Color> annotationColors = new List<System.Windows.Media.Color>();
  233. //private List<string> annotationAuthor = new List<string>();
  234. public ScreenAnnotationDialogViewModel()
  235. {
  236. CancelCommand = new DelegateCommand(CancelEvent);
  237. CreateCommnad = new DelegateCommand(CreateEvent);
  238. }
  239. private void CreateEvent()
  240. {
  241. DialogParameters valuePairs = new DialogParameters();
  242. //valuePairs.Add(ParameterNames.Bookmark, Bookmark);
  243. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  244. }
  245. private void CancelEvent()
  246. {
  247. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  248. }
  249. public bool CanCloseDialog()
  250. {
  251. return true;
  252. }
  253. public void OnDialogClosed()
  254. {
  255. }
  256. private void SetColor(System.Windows.Media.Color color)
  257. {
  258. if (AnnotationColors.Count > 0)
  259. {
  260. for (int i = 0; i < AnnotationColors.Count; i++)
  261. {
  262. System.Windows.Media.Color color1 = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(AnnotationColors[i].Color.ToString());
  263. if (color1.R == color.R && color1.G == color.G && color1.B == color.B
  264. && color1.A == color.A)
  265. {
  266. AnnotationColors.Remove(AnnotationColors[i]);
  267. }
  268. }
  269. }
  270. AnnotationColors.Add(new ColorItem(color));
  271. //var bFind = AnnotationColors.All<ColorItem>(p => p.Color == new SolidColorBrush(color));
  272. //if (bFind)
  273. //{
  274. // AnnotationColors.Add(new ColorItem(color));
  275. //}
  276. }
  277. public void OnDialogOpened(IDialogParameters parameters)
  278. {
  279. ObservableCollection<AnnotationHandlerEventArgs> list;
  280. parameters.TryGetValue<ObservableCollection<AnnotationHandlerEventArgs>>(ParameterNames.AnnotationList, out list);
  281. AnnotationListItems = list;
  282. foreach (var item in AnnotationListItems)
  283. {
  284. AnnotationAuthor.Add(new AuthorItem(item.Author));
  285. AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs;
  286. switch (item.EventType)
  287. {
  288. case AnnotArgsType.AnnotFreeText://文本
  289. if (data is FreeTextAnnotArgs textAnnotArgs)
  290. {
  291. SetColor(textAnnotArgs.FontColor);
  292. }
  293. AnnotFreeTextVisibility = Visibility.Visible;
  294. break;
  295. case AnnotArgsType.AnnotHighlight://高亮
  296. if (data is TextHighlightAnnotArgs highlightAnnotArgs)
  297. {
  298. SetColor(highlightAnnotArgs.Color);
  299. }
  300. HighlightVisibility = Visibility.Visible;
  301. break;
  302. case AnnotArgsType.AnnotFreehand://手绘
  303. if (data is FreehandAnnotArgs freehandAnnotArgs)
  304. {
  305. SetColor(freehandAnnotArgs.InkColor);
  306. }
  307. FreeHandVisibility = Visibility.Visible;
  308. break;
  309. case AnnotArgsType.AnnotSquiggly://波浪线
  310. break;
  311. case AnnotArgsType.AnnotStamp://图章
  312. AnnotStampVisibility = Visibility.Visible;
  313. break;
  314. case AnnotArgsType.AnnotStrikeout://删除线
  315. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  316. {
  317. SetColor(textStrikeoutAnnotArgs.Color);
  318. }
  319. AnnotStickyVisibility = Visibility.Visible;
  320. break;
  321. case AnnotArgsType.AnnotSticky://便签
  322. if (data is StickyAnnotArgs stickyAnnotArgs)
  323. {
  324. SetColor(stickyAnnotArgs.Color);
  325. }
  326. AnnotStickyVisibility = Visibility.Visible;
  327. break;
  328. case AnnotArgsType.AnnotUnderline://下划线
  329. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  330. {
  331. SetColor(textUnderlineAnnotArgs.Color);
  332. }
  333. UnderLineVisibility = Visibility.Visible;
  334. break;
  335. case AnnotArgsType.AnnotLine:
  336. if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
  337. {
  338. //箭头
  339. SharpArrowVisibility = Visibility.Visible;
  340. }
  341. else
  342. {
  343. //线
  344. SharpLineVisibility = Visibility.Visible;
  345. }
  346. SetColor((item.AnnotHandlerEventArgs as LineAnnotArgs).LineColor);
  347. break;
  348. case AnnotArgsType.AnnotSquare://矩形
  349. if (data is SquareAnnotArgs squareAnnotArgs)
  350. {
  351. SetColor(squareAnnotArgs.BgColor);
  352. SetColor(squareAnnotArgs.LineColor);
  353. }
  354. AnnotSquareVisibility = Visibility.Visible;
  355. break;
  356. case AnnotArgsType.AnnotCircle://圆
  357. if (data is CircleAnnotArgs circleAnnotArgs)
  358. {
  359. SetColor(circleAnnotArgs.BgColor);
  360. SetColor(circleAnnotArgs.LineColor);
  361. }
  362. AnnotCircleVisibility = Visibility.Visible;
  363. break;
  364. }
  365. }
  366. //AnnotationAuthor = AnnotationAuthor.MyDistinct(s => s.Name) as ObservableCollection<AuthorItem>;
  367. //AnnotationColors = AnnotationColors.MyDistinct(s => s.Color) as ObservableCollection<ColorItem>;
  368. AnnotationAuthor = new ObservableCollection<AuthorItem>(AnnotationAuthor.MyDistinct(s => s.Name));
  369. //AnnotationColors = new ObservableCollection<ColorItem>(AnnotationColors.MyDistinct(s => s.Color));
  370. }
  371. }
  372. }