using ComPDFKit.Import; using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using DryIoc; using Microsoft.Office.Interop.Word; using PDF_Office.DataConvert; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Model.BOTA; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Forms; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using static Dropbox.Api.Files.SearchMatchType; using static System.Net.Mime.MediaTypeNames; using Border = System.Windows.Controls.Border; using ListBox = System.Windows.Controls.ListBox; using Task = System.Threading.Tasks.Task; namespace PDF_Office.ViewModels.BOTA { public class AnnotationContentViewModel : BindableBase, INavigationAware { private Visibility isEmptyPanelVisibility = Visibility.Visible; public Visibility IsEmptyPanelVisibility { get { return isEmptyPanelVisibility; } set { SetProperty(ref isEmptyPanelVisibility, value); } } private ObservableCollection annotationListItems; public CPDFViewer PdfViewer { get; set; } public ObservableCollection AnnotationListItems { get { return annotationListItems; } set { SetProperty(ref annotationListItems, value); } } public DelegateCommand LoadedCommand { get; set; } public DelegateCommand ListBoxItemPreviewMouseLeftButtonDown { get; set; } public AnnotationContentViewModel() { LoadedCommand = new DelegateCommand(Loaded); ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand(ListBoxItem_PreviewMouseLeftButtonDown); } /// /// listboxitem鼠标左键点击,显示分组的数据 /// /// private void ListBoxItem_PreviewMouseLeftButtonDown(object obj) { if (obj is ListBoxItem item) { var data = item.DataContext as AnnotationHandlerEventArgs; if (data != null) { PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex); } } } private void Loaded(object obj) { if (obj is CompositeCommandParameter composite) { if (composite.Parameter is ListBox listBox) { SetGroupHeader(listBox); } } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } public void OnNavigatedTo(NavigationContext navigationContext) { var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer; if (pdfview != null) { PdfViewer = pdfview; AnnotationListItems = new ObservableCollection(); AnnotationListItems = GetDocumentAnnotionList(); AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged; } } private async void SetGroupHeader(ListBox listBox) { //按照PageIndex,分组 排序 ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems); CreateTimeToDate createTimeToDate = new CreateTimeToDate(); iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex))); iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending)); iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending)); if (AnnotationListItems.Count > 0) { IsEmptyPanelVisibility = Visibility.Collapsed; await Task.Delay(5); //展开数据 ExpandGroupHeader(AnnotationListItems, listBox); } else { IsEmptyPanelVisibility = Visibility.Visible; } } /// /// 展开列表项 /// /// /// private void ExpandGroupHeader(ObservableCollection annotationListItems, ListBox listBox) { try { foreach (var item in annotationListItems) { var groups = listBox.Items.Groups; for (int i = 0; i < groups.Count; i++) { var group = groups[i] as CollectionViewGroup; if (group.Items.Contains(item)) { var scroller = GetScrollHost(listBox); var stackpanel = CommonHelper.FindVisualChild(scroller); int count = VisualTreeHelper.GetChildrenCount(stackpanel); var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem; var g = CommonHelper.FindVisualChild(groupItem); if (g != null) { g.IsExpanded = true; } } } } } catch { } } private ScrollViewer GetScrollHost(ListBox listBox) { if (VisualTreeHelper.GetChildrenCount(listBox) > 0) { int s = VisualTreeHelper.GetChildrenCount(listBox); Border border = VisualTreeHelper.GetChild(listBox, 0) as Border; if (border != null) { return VisualTreeHelper.GetChild(border, 0) as ScrollViewer; } } return null; } private ObservableCollection GetDocumentAnnotionList() { ObservableCollection list = new ObservableCollection(); for (int i = 0; i < PdfViewer.Document.PageCount; i++) { var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document); foreach (var item in items) { //原型图上,目前对波浪线的类型,在注释列表不显示 if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly) { AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs(); if (item.EventType == AnnotArgsType.AnnotFreehand) { WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex); args.WriteableBitmap = bitmap; } args.AnnotHandlerEventArgs = item; args.PageIndex = item.PageIndex; args.AnnotIndex = item.AnnotIndex; args.EventType = item.EventType; args.CreateTime = item.CreateTime; args.UpdateTime = item.UpdateTime; args.Content = item.Content; args.MarkupContent = item.MarkupContent; args.Author = item.Author; args.Locked = item.Locked; args.ReadOnly = item.ReadOnly; args.FormField = item.FormField; args.Document = PdfViewer.Document; list.Add(args); } } } return list; } public WriteableBitmap GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex) { if (doc == null) { return null; } CPDFPage docPage = doc.PageAtIndex(pageIndex, false); if (docPage == null) { return null; } List docAnnots = docPage.GetAnnotations(); foreach (CPDFAnnotation annot in docAnnots) { if (docAnnots.IndexOf(annot) == annotIndex) { CRect rawRect = annot.GetRect(); double scaleDpi = 96.0 / 72.0; Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi); int drawWidth = (int)paintRect.Width; int drawHeight = (int)paintRect.Height; byte[] bitmapArray = new byte[drawWidth * drawHeight * 4]; annot.RenderAnnot(drawWidth, drawHeight, bitmapArray); WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null); wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0); return wirteBitmap; } } return null; } private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { ObservableCollection obsSender = sender as ObservableCollection; if (obsSender != null) { if (obsSender.Count < 1) { IsEmptyPanelVisibility = Visibility.Visible; } else { IsEmptyPanelVisibility = Visibility.Collapsed; } } } } }