123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using DryIoc;
- using Microsoft.Office.Interop.Excel;
- using Microsoft.Office.Interop.Word;
- using PDF_Office.CustomControl;
- using PDF_Office.DataConvert;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.BOTA;
- using PDF_Office.Views.PropertyPanel.AnnotPanel;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- 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.Markup;
- 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 static System.Windows.Forms.VisualStyles.VisualStyleElement;
- using Border = System.Windows.Controls.Border;
- using ListBox = System.Windows.Controls.ListBox;
- using Task = System.Threading.Tasks.Task;
- using TextBox = System.Windows.Controls.TextBox;
- using Winform = System.Windows.Forms;
- namespace PDF_Office.ViewModels.BOTA
- {
- public class AnnotationContentViewModel : BindableBase, INavigationAware
- {
- private ListBox listBox;
- private IRegionManager region;
- private IDialogService dialogs;
- private Visibility isEmptyPanelVisibility = Visibility.Visible;
- public ViewContentViewModel ViewContentViewModel { get; set; }
- public CPDFViewer PdfViewer { get; set; }
- public Visibility IsEmptyPanelVisibility
- {
- get { return isEmptyPanelVisibility; }
- set
- {
- SetProperty(ref isEmptyPanelVisibility, value);
- }
- }
- private ObservableCollection<AnnotationHandlerEventArgs> annotationListItems;
- public ObservableCollection<AnnotationHandlerEventArgs> AnnotationListItems
- {
- get { return annotationListItems; }
- set
- {
- SetProperty(ref annotationListItems, value);
- }
- }
- public DelegateCommand<object> LoadedCommand { get; set; }
- public DelegateCommand<object> ListBoxItemPreviewMouseLeftButtonDown { get; set; }
- public DelegateCommand<object> AddNotesCommand { get; set; }
- public DelegateCommand<object> ScreenCommand { get; set; }
- public DelegateCommand<object> ExportCommentsCommand { get; set; }
- public DelegateCommand<object> DeleteCommand { get; set; }
- public DelegateCommand DeleteAllCommand { get; set; }
- public AnnotationContentViewModel(IRegionManager regionManager, IDialogService dialogService)
- {
- region = regionManager;
- dialogs = dialogService;
- LoadedCommand = new DelegateCommand<object>(Loaded);
- ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand<object>(ListBoxItem_PreviewMouseLeftButtonDown);
- AddNotesCommand = new DelegateCommand<object>(AddNotesEvent);
- ScreenCommand = new DelegateCommand<object>(ScreenEvent);
- ExportCommentsCommand = new DelegateCommand<object>(ExportCommentsEvent);
- DeleteCommand = new DelegateCommand<object>(DelegateEvent);
- DeleteAllCommand = new DelegateCommand(DeleteAllEvent);
- }
- /// <summary>
- /// 删除所有注释
- /// </summary>
- private void DeleteAllEvent()
- {
- //调用集中删除的接口 方便一次性undo
- Dictionary<int, List<int>> deleteLists = new Dictionary<int, List<int>>();
- for (int i = 0; i < annotationListItems.Count; i++)
- {
- AnnotationHandlerEventArgs item = annotationListItems[i] as AnnotationHandlerEventArgs;
- if (!deleteLists.ContainsKey(item.PageIndex))
- {
- deleteLists.Add(item.PageIndex, new List<int>() { item.AnnotIndex });
- }
- else
- {
- var pagelist = deleteLists[item.PageIndex];
- pagelist.Add(item.AnnotIndex);
- }
- }
- PdfViewer.RemovePageAnnot(deleteLists);
- annotationListItems.Clear();
- PdfViewer.UndoManager.CanSave = true;
- }
- /// <summary>
- /// 删除注释,单个/多个
- /// </summary>
- /// <param name="obj"></param>
- private void DelegateEvent(object obj)
- {
- if (obj is AnnotationHandlerEventArgs annotation)
- {
- if (annotation != null)
- {
- var result = PdfViewer.RemovePageAnnot(annotation.PageIndex, annotation.AnnotIndex);
- if (result)
- {
- AnnotationListItems.Remove(annotation);
- //记录是删除了哪些页面的注释,然后更新对应页面的注释即可
- UpdateAnnotListAfterDelete(annotation.PageIndex, annotation.AnnotIndex);
- PdfViewer.UndoManager.CanSave = true;
- }
- }
- }
- }
- private void UpdateAnnotListAfterDelete(int pageIndex, int annoteIndex)
- {
- var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
- for (int j = 0; j < items.Count; j++)//用修改赋值的方式 可以解决删除后表头折叠的问题
- {
- if (items[j].AnnotIndex >= annoteIndex)//只需要更新比删除元素索引大的注释
- {
- for (int k = 0; k < AnnotationListItems.Count; k++)
- {
- //相当于将后面的索引-1
- if (AnnotationListItems[k].PageIndex == pageIndex &&
- AnnotationListItems[k].AnnotIndex == (items[j].AnnotIndex + 1) &&
- string.Equals(AnnotationListItems[k].MarkupContent, items[j].MarkupContent) &&
- string.Equals(AnnotationListItems[k].Content, items[j].Content) &&
- string.Equals(AnnotationListItems[k].CreateTime, items[j].CreateTime) &&
- string.Equals(AnnotationListItems[k].Author, items[j].Author)
- )
- {
- AnnotationListItems[k].AnnotHandlerEventArgs = items[j];
- AnnotationListItems[k].PageIndex = items[j].PageIndex;
- AnnotationListItems[k].AnnotIndex = items[j].AnnotIndex;
- AnnotationListItems[k].EventType = items[j].EventType;
- AnnotationListItems[k].CreateTime = items[j].CreateTime;
- AnnotationListItems[k].UpdateTime = items[j].UpdateTime;
- AnnotationListItems[k].Content = items[j].Content;
- AnnotationListItems[k].MarkupContent = items[j].MarkupContent;
- AnnotationListItems[k].Author = items[j].Author;
- AnnotationListItems[k].Locked = items[j].Locked;
- AnnotationListItems[k].ReadOnly = items[j].ReadOnly;
- AnnotationListItems[k].FormField = items[j].FormField;
- AnnotationListItems[k].Document = PdfViewer.Document;
- }
- }
- }
- }
- }
- private void ExportCommentsEvent(object obj)
- {
- if (ViewContentViewModel.CanSave)
- {
- ViewContentViewModel.SaveFile.Execute();
- }
- Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
- dlg.Filter = "PDF|*.xfdf";
- dlg.DefaultExt = ".xfdf";
- dlg.FileName = PdfViewer.Document.FileName;
- if (dlg.ShowDialog() == true)
- {
- string fileName = dlg.FileName;
- var result = PdfViewer.ExportAnnotationToXFDFPath(fileName);
- if (result)
- {
- MessageBoxEx.Show("导出成功", "", Winform.MessageBoxButtons.OK);
- }
- else
- {
- MessageBoxEx.Show("导出失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
- }
- }
- }
- private void ScreenEvent(object obj)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.AnnotationList, AnnotationListItems);
- dialogs.ShowDialog(DialogNames.ScreenAnnotationDialog, value, e =>
- {
- });
- }
- private void AddNotesEvent(object obj)
- {
- if (obj is AnnotationHandlerEventArgs data)
- {
- if (data != null)
- {
- if (data.EventType != AnnotArgsType.AnnotSticky)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.Annotation, data);
- dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
- {
- if (e.Result == ButtonResult.OK && e.Parameters != null)
- {
- //PdfViewer.UndoManager.CanSave = true;
- // if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
- // {
- // AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
- // //AnnotAttribEvent annotEvent = e.Parameters.GetValue<AnnotAttribEvent>(ParameterNames.AnnotEvent);
- // //annotEvent?.UpdateAttrib(AnnotAttrib.NoteText, annotation.MarkupContent);
- // //annotEvent?.UpdateAnnot();
- // }
- }
- });
- }
- //if(data.EventType == AnnotArgsType.AnnotFreeText)
- //{
- // ListBoxItem myListBoxItem = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(data));
- // TextBlock txbContent = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
- // TextBox txtContent = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
- // txbContent.Visibility = Visibility.Collapsed;
- // txtContent.Visibility = Visibility.Visible;
- //}
- }
- }
- }
- /// <summary>
- /// listboxitem鼠标左键点击,显示分组的数据
- /// </summary>
- /// <param name="obj"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 页面加载时
- /// </summary>
- /// <param name="obj"></param>
- private void Loaded(object obj)
- {
- if (obj is CompositeCommandParameter composite)
- {
- if (composite.Parameter is ListBox listBox)
- {
- this.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;
- var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
- if (pdfview != null && viewContentViewModel != null)
- {
- ViewContentViewModel = viewContentViewModel;
- PdfViewer = pdfview;
- AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
- 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;
- }
- }
- /// <summary>
- /// 展开列表项
- /// </summary>
- /// <param name="annotationListItems"></param>
- /// <param name="listBox"></param>
- private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> 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<StackPanel>(scroller);
- int count = VisualTreeHelper.GetChildrenCount(stackpanel);
- var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
- var g = CommonHelper.FindVisualChild<Expander>(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<AnnotationHandlerEventArgs> GetDocumentAnnotionList()
- {
- ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
- 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;
- }
- /// <summary>
- /// 获取手绘图案
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="pageIndex"></param>
- /// <param name="annotIndex"></param>
- /// <returns></returns>
- 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<CPDFAnnotation> 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<AnnotationHandlerEventArgs> obsSender = sender as ObservableCollection<AnnotationHandlerEventArgs>;
- if (obsSender != null)
- {
- if (obsSender.Count < 1)
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- }
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- }
- }
- }
|