AnnotationContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKitViewer.AnnotEvent;
  6. using ComPDFKitViewer.PdfViewer;
  7. using DryIoc;
  8. using Microsoft.Office.Interop.Word;
  9. using PDF_Office.DataConvert;
  10. using PDF_Office.Helper;
  11. using PDF_Office.Model;
  12. using Prism.Commands;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.ComponentModel;
  19. using System.Configuration;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Windows;
  24. using System.Windows.Controls;
  25. using System.Windows.Data;
  26. using System.Windows.Forms;
  27. using System.Windows.Media;
  28. using System.Windows.Media.Imaging;
  29. using System.Windows.Shapes;
  30. using static Dropbox.Api.Files.SearchMatchType;
  31. using static System.Net.Mime.MediaTypeNames;
  32. using Border = System.Windows.Controls.Border;
  33. using ListBox = System.Windows.Controls.ListBox;
  34. using Task = System.Threading.Tasks.Task;
  35. namespace PDF_Office.ViewModels.BOTA
  36. {
  37. public class AnnotationHandlerEventArgs : EventArgs
  38. {
  39. public string Content { get; set; }
  40. public string MarkupContent { get; set; }
  41. public string Author { get; set; }
  42. public string CreateTime { get; set; }
  43. public string UpdateTime { get; set; }
  44. public AnnotArgsType EventType { get; set; }
  45. public bool Locked { get; set; }
  46. public bool ReadOnly { get; set; }
  47. public ComPDFKitViewer.FormField FormField { get; set; }
  48. public int PageIndex { get; set; }
  49. public int AnnotIndex { get; set; }
  50. public AnnotHandlerEventArgs AnnotHandlerEventArgs { get; set; }
  51. public WriteableBitmap WriteableBitmap { get; set; }
  52. public CPDFDocument Document { get; set; }
  53. }
  54. public class AnnotationContentViewModel : BindableBase, INavigationAware
  55. {
  56. private Visibility isEmptyPanelVisibility = Visibility.Visible;
  57. public Visibility IsEmptyPanelVisibility
  58. {
  59. get { return isEmptyPanelVisibility; }
  60. set
  61. {
  62. SetProperty(ref isEmptyPanelVisibility, value);
  63. }
  64. }
  65. private ObservableCollection<AnnotationHandlerEventArgs> annotationListItems;
  66. public CPDFViewer PdfViewer { get; set; }
  67. public ObservableCollection<AnnotationHandlerEventArgs> AnnotationListItems
  68. {
  69. get { return annotationListItems; }
  70. set
  71. {
  72. SetProperty(ref annotationListItems, value);
  73. }
  74. }
  75. public DelegateCommand<object> LoadedCommand { get; set; }
  76. public DelegateCommand<object> ListBoxItemPreviewMouseLeftButtonDown { get; set; }
  77. public AnnotationContentViewModel()
  78. {
  79. LoadedCommand = new DelegateCommand<object>(Loaded);
  80. ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand<object>(ListBoxItem_PreviewMouseLeftButtonDown);
  81. }
  82. /// <summary>
  83. /// listboxitem鼠标左键点击,显示分组的数据
  84. /// </summary>
  85. /// <param name="obj"></param>
  86. private void ListBoxItem_PreviewMouseLeftButtonDown(object obj)
  87. {
  88. if (obj is ListBoxItem item)
  89. {
  90. var data = item.DataContext as AnnotationHandlerEventArgs;
  91. if (data != null)
  92. {
  93. PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex);
  94. }
  95. }
  96. }
  97. private void Loaded(object obj)
  98. {
  99. if (obj is CompositeCommandParameter composite)
  100. {
  101. if (composite.Parameter is ListBox listBox)
  102. {
  103. SetGroupHeader(listBox);
  104. }
  105. }
  106. }
  107. public bool IsNavigationTarget(NavigationContext navigationContext)
  108. {
  109. return true;
  110. }
  111. public void OnNavigatedFrom(NavigationContext navigationContext)
  112. {
  113. }
  114. public void OnNavigatedTo(NavigationContext navigationContext)
  115. {
  116. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  117. if (pdfview != null)
  118. {
  119. PdfViewer = pdfview;
  120. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  121. AnnotationListItems = GetDocumentAnnotionList();
  122. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  123. }
  124. }
  125. private async void SetGroupHeader(ListBox listBox)
  126. {
  127. //按照PageIndex,分组 排序
  128. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  129. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  130. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  131. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  132. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  133. if (AnnotationListItems.Count > 0)
  134. {
  135. IsEmptyPanelVisibility = Visibility.Collapsed;
  136. await Task.Delay(5);
  137. //展开数据
  138. ExpandGroupHeader(AnnotationListItems, listBox);
  139. }
  140. else
  141. {
  142. IsEmptyPanelVisibility = Visibility.Visible;
  143. }
  144. }
  145. /// <summary>
  146. /// 展开列表项
  147. /// </summary>
  148. /// <param name="annotationListItems"></param>
  149. /// <param name="listBox"></param>
  150. private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  151. {
  152. try
  153. {
  154. foreach (var item in annotationListItems)
  155. {
  156. var groups = listBox.Items.Groups;
  157. for (int i = 0; i < groups.Count; i++)
  158. {
  159. var group = groups[i] as CollectionViewGroup;
  160. if (group.Items.Contains(item))
  161. {
  162. var scroller = GetScrollHost(listBox);
  163. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  164. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  165. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  166. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  167. if (g != null)
  168. {
  169. g.IsExpanded = true;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. catch { }
  176. }
  177. private ScrollViewer GetScrollHost(ListBox listBox)
  178. {
  179. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  180. {
  181. int s = VisualTreeHelper.GetChildrenCount(listBox);
  182. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  183. if (border != null)
  184. {
  185. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  186. }
  187. }
  188. return null;
  189. }
  190. private ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList()
  191. {
  192. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  193. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  194. {
  195. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  196. foreach (var item in items)
  197. {
  198. //原型图上,目前对波浪线的类型,在注释列表不显示
  199. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly)
  200. {
  201. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  202. if (item.EventType == AnnotArgsType.AnnotFreehand)
  203. {
  204. WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  205. args.WriteableBitmap = bitmap;
  206. }
  207. args.AnnotHandlerEventArgs = item;
  208. args.PageIndex = item.PageIndex;
  209. args.AnnotIndex = item.AnnotIndex;
  210. args.EventType = item.EventType;
  211. args.CreateTime = item.CreateTime;
  212. args.UpdateTime = item.UpdateTime;
  213. args.Content = item.Content;
  214. args.MarkupContent = item.MarkupContent;
  215. args.Author = item.Author;
  216. args.Locked = item.Locked;
  217. args.ReadOnly = item.ReadOnly;
  218. args.FormField = item.FormField;
  219. args.Document = PdfViewer.Document;
  220. list.Add(args);
  221. }
  222. }
  223. }
  224. return list;
  225. }
  226. public WriteableBitmap GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  227. {
  228. if (doc == null)
  229. {
  230. return null;
  231. }
  232. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  233. if (docPage == null)
  234. {
  235. return null;
  236. }
  237. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  238. foreach (CPDFAnnotation annot in docAnnots)
  239. {
  240. if (docAnnots.IndexOf(annot) == annotIndex)
  241. {
  242. CRect rawRect = annot.GetRect();
  243. double scaleDpi = 96.0 / 72.0;
  244. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  245. int drawWidth = (int)paintRect.Width;
  246. int drawHeight = (int)paintRect.Height;
  247. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  248. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  249. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  250. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  251. return wirteBitmap;
  252. }
  253. }
  254. return null;
  255. }
  256. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  257. {
  258. ObservableCollection<AnnotHandlerEventArgs> obsSender = sender as ObservableCollection<AnnotHandlerEventArgs>;
  259. if (obsSender != null)
  260. {
  261. if (obsSender.Count < 1)
  262. {
  263. IsEmptyPanelVisibility = Visibility.Visible;
  264. }
  265. else
  266. {
  267. IsEmptyPanelVisibility = Visibility.Collapsed;
  268. }
  269. }
  270. }
  271. }
  272. }