AnnotationContentViewModel.cs 11 KB

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