1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using DryIoc;
- using ImTools;
- using Microsoft.Office.Interop.PowerPoint;
- 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.BOTA;
- using PDF_Office.Views.PropertyPanel.AnnotPanel;
- using PDF_Office.Views.Scan;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Configuration;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Annotations;
- using System.Windows.Annotations.Storage;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Markup;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Media.Media3D;
- using System.Windows.Shapes;
- using static Dropbox.Api.Files.SearchMatchType;
- using static Dropbox.Api.TeamLog.SpaceCapsType;
- using static System.Net.Mime.MediaTypeNames;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
- 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
- {
- #region 文案
- private string T_expand;
- public string T_Expand
- {
- get { return T_expand; }
- set
- {
- SetProperty(ref T_expand, value);
- }
- }
- private string T_collapse;
- public string T_Collapse
- {
- get { return T_collapse; }
- set
- {
- SetProperty(ref T_collapse, value);
- }
- }
- private string T_import;
- public string T_Import
- {
- get { return T_import; }
- set
- {
- SetProperty(ref T_import, value);
- }
- }
- private string T_export;
- public string T_Export
- {
- get { return T_export; }
- set
- {
- SetProperty(ref T_export, value);
- }
- }
- private string T_deleteAll;
- public string T_DeleteAll
- {
- get { return T_deleteAll; }
- set
- {
- SetProperty(ref T_deleteAll, value);
- }
- }
- private string T_emptyTitle;
- public string T_EmptyTitle
- {
- get { return T_emptyTitle; }
- set
- {
- SetProperty(ref T_emptyTitle, value);
- }
- }
- private string T_emptyContext;
- public string T_EmptyContext
- {
- get { return T_emptyContext; }
- set
- {
- SetProperty(ref T_emptyContext, value);
- }
- }
- private void InitString()
- {
- T_Expand = App.MainPageLoader.GetString("Annotation_MenuExpand");
- T_Collapse = App.MainPageLoader.GetString("Annotation_MenuCollapse");
- T_Import = App.MainPageLoader.GetString("Annotation_MenuImport");
- T_Export = App.MainPageLoader.GetString("Annotation_MenuExport");
- T_DeleteAll = App.MainPageLoader.GetString("Annotation_MenuDeleteAll");
- T_EmptyTitle = App.MainPageLoader.GetString("Annotation_EmptyTitle");
- T_EmptyContext = App.MainPageLoader.GetString("Annotation_EmptyContext");
- }
- #endregion 文案
- 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> currentAnnotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
- public ObservableCollection<AnnotationHandlerEventArgs> CurrentAnnotationLists { get => currentAnnotationArgs; set => currentAnnotationArgs = value; }
- private AnnotationSortOrder annotationSortOrder = AnnotationSortOrder.PageIndexAscending;
- public AnnotationSortOrder AnnotationSortOrder
- {
- get { return annotationSortOrder; }
- set { annotationSortOrder = value; }
- }
- private ObservableCollection<AnnotationHandlerEventArgs> annotationListItems;
- public ObservableCollection<AnnotationHandlerEventArgs> AnnotationListItems
- {
- get { return annotationListItems; }
- set
- {
- SetProperty(ref annotationListItems, value);
- }
- }
- private bool CanIsEnabled()
- {
- if (AnnotationListItems.Count > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private List<string> colors = new List<string>();
- private List<string> authors = new List<string>();
- private List<AnnotArgsType> annotArgsTypes = new List<AnnotArgsType>();
- 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 ExportCommentsCommand { get; set; }
- public DelegateCommand ImportCommentsCommand { get; set; }
- public DelegateCommand<object> DeleteCommand { get; set; }
- public DelegateCommand DeleteAllCommand { get; set; }
- public DelegateCommand PageSortCommand { get; set; }
- public DelegateCommand TimeAscendingCommand { get; set; }
- public DelegateCommand TimeDescendingCommand { 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(ExportCommentsEvent, CanIsEnabled).ObservesProperty(() => AnnotationListItems);
- ImportCommentsCommand = new DelegateCommand(ImportCommentsEvent);
- DeleteCommand = new DelegateCommand<object>(DelegateEvent);
- DeleteAllCommand = new DelegateCommand(DeleteAllEvent, CanIsEnabled).ObservesProperty(() => AnnotationListItems);
- PageSortCommand = new DelegateCommand(PageSortEvent);
- TimeAscendingCommand = new DelegateCommand(TimeAscendingEvent);
- TimeDescendingCommand = new DelegateCommand(TimeDescendingEvent);
- InitString();
- }
- public void HiddenAnnot()
- {
- AnnotationListItems.Clear();
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- public void DisplayAnnot()
- {
- AnnotationListItems = GetDocumentAnnotionList();
- CurrentAnnotationLists = GetDocumentAnnotionList();
- RefreshAnnotationListItems();
- }
- /// <summary>
- /// 导入注释
- /// </summary>
- /// <param name="obj"></param>
- private async void ImportCommentsEvent()
- {
- Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
- dlg.Filter = "PDF|*.xfdf";
- dlg.DefaultExt = ".xfdf";
- if (dlg.ShowDialog() == true)
- {
- try
- {
- string fileName = dlg.FileName;
- await Task.Delay(10);
- var result = PdfViewer.ImportAnnotationFromXFDFPath(fileName);
- if (result == false)
- {
- //MessageBoxEx.Show("导入失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.Show("提示", "导入失败!", "OK");
- return;
- }
- PdfViewer.ReloadVisibleAnnots();
- //提取出来的注释文件 时间为空 则显示未系统当前时间
- CurrentAnnotationLists = GetDocumentAnnotionList();
- //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
- //CurrentAnnotationLists = new ObservableCollection<AnnotationHandlerEventArgs>(CurrentAnnotationLists.OrderBy(item => item.ClientRect.X).ThenBy(item => item.ClientRect.Y));
- AnnotationListItems.Clear();
- AnnotationListItems.AddRange(CurrentAnnotationLists);
- await Task.Delay(5);
- RefreshAnnotationListItems(null);
- PdfViewer.UndoManager.CanSave = true;
- }
- catch (Exception ex)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.Show("提示", "导入失败!" + ex.Message, "OK");
- }
- }
- }
- /// <summary>
- /// 时间倒序
- /// </summary>
- private void TimeDescendingEvent()
- {
- ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
- CreateTimeToDate createTimeToDate = new CreateTimeToDate();
- v.GroupDescriptions.Clear();
- v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
- v.SortDescriptions.Clear();
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
- AnnotationSortOrder = AnnotationSortOrder.TimeDescending;
- }
- /// <summary>
- /// 时间正序
- /// </summary>
- private void TimeAscendingEvent()
- {
- ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
- CreateTimeToDate createTimeToDate = new CreateTimeToDate();
- v.GroupDescriptions.Clear();
- v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
- v.SortDescriptions.Clear();
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
- AnnotationSortOrder = AnnotationSortOrder.TimeAscending;
- }
- /// <summary>
- /// 页面排序
- /// </summary>
- private void PageSortEvent()
- {
- ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
- v.GroupDescriptions.Clear();
- v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
- v.SortDescriptions.Clear();
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
- v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
- AnnotationSortOrder = AnnotationSortOrder.PageIndexAscending;
- }
- /// <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);
- }
- }
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog(" ", "This action will permanently delete all comments,\r\nWhether to continue??", "Cancel", "OK");
- if (alertsMessage.result == ContentResult.Ok)
- {
- PdfViewer.RemovePageAnnot(deleteLists);
- AnnotationListItems.Clear();
- CurrentAnnotationLists.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)
- {
- RemoveItem(annotation);
- }
- }
- }
- }
- public void RemoveItem(AnnotationHandlerEventArgs annotation)
- {
- AnnotationListItems.Remove(annotation);
- var annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == annotation.PageIndex && x.AnnotIndex == annotation.AnnotIndex);
- CurrentAnnotationLists.Remove(annotation1);
- //记录是删除了哪些页面的注释,然后更新对应页面的注释即可
- UpdateAnnotListAfterDelete(annotation.PageIndex, annotation.AnnotIndex);
- PdfViewer.UndoManager.CanSave = true;
- }
- /// <summary>
- /// 删除之后,更新
- /// </summary>
- /// <param name="pageIndex"></param>
- /// <param name="annoteIndex"></param>
- 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;
- if (items[j].EventType == AnnotArgsType.AnnotFreehand)
- {
- BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, items[j].PageIndex, items[j].AnnotIndex);
- AnnotationListItems[k].WriteableBitmap = bitmap;
- }
- 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;
- }
- }
- }
- }
- }
- /// <summary>
- /// 导出注释
- /// </summary>
- /// <param name="obj"></param>
- private void ExportCommentsEvent()
- {
- 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);
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.Show("提示", "导出成功!", "OK");
- }
- else
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.Show("提示", "导出失败!", "OK");
- //MessageBoxEx.Show("导出失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
- }
- }
- }
- /// <summary>
- /// 筛选注释
- /// </summary>
- /// <param name="obj"></param>
- private void ScreenEvent(object obj)
- {
- if (obj is System.Windows.Controls.Button button)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.AnnotationList, CurrentAnnotationLists);
- value.Add(ParameterNames.AnnotArgsTypes, annotArgsTypes);
- value.Add(ParameterNames.AnnotationColors, colors);
- value.Add(ParameterNames.AnnotationAuthor, authors);
- dialogs.ShowDialog(DialogNames.ScreenAnnotationDialog, value, e =>
- {
- if (e.Result == ButtonResult.OK && e.Parameters != null)
- {
- if (e.Parameters.ContainsKey(ParameterNames.AnnotArgsTypes))
- {
- annotArgsTypes = e.Parameters.GetValue<List<AnnotArgsType>>(ParameterNames.AnnotArgsTypes);
- }
- if (e.Parameters.ContainsKey(ParameterNames.AnnotationColors))
- {
- colors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationColors);
- }
- if (e.Parameters.ContainsKey(ParameterNames.AnnotationAuthor))
- {
- authors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationAuthor);
- }
- if (annotArgsTypes.Count > 0 || colors.Count > 0 || authors.Count > 0)
- {
- ObservableCollection<AnnotationHandlerEventArgs> screenAnnotationArgs = SelectAnnotationListItems(annotArgsTypes, colors, authors);
- AnnotationListItems.Clear();
- AnnotationListItems.AddRange(screenAnnotationArgs);
- //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
- //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
- RefreshAnnotationListItems(button);
- //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- }
- else
- {
- CleanAnnotation(button);
- }
- }
- else if (e.Result == ButtonResult.Cancel && e.Parameters != null)
- {
- //CleanAnnotation(button);
- }
- });
- }
- }
- private void CleanAnnotation(System.Windows.Controls.Button button)
- {
- colors.Clear();
- authors.Clear();
- annotArgsTypes.Clear();
- AnnotationListItems.Clear();
- AnnotationListItems.AddRange(CurrentAnnotationLists);
- //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
- //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
- RefreshAnnotationListItems(button);
- //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- }
- /// <summary>
- /// 刷新列表数据
- /// </summary>
- /// <param name="button"></param>
- public async void RefreshAnnotationListItems(System.Windows.Controls.Button button = null)
- {
- switch (AnnotationSortOrder)
- {
- case AnnotationSortOrder.TimeAscending:
- TimeAscendingEvent();
- break;
- case AnnotationSortOrder.TimeDescending:
- TimeDescendingEvent();
- break;
- case AnnotationSortOrder.PageIndexAscending:
- PageSortEvent();
- break;
- default:
- break;
- }
- if (AnnotationListItems.Count > 0)
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- await Task.Delay(2);
- //展开数据
- ExpandGroupHeader(AnnotationListItems, listBox);
- }
- else
- {
- if (button != null)
- {
- button.IsEnabled = true;
- }
- IsEmptyPanelVisibility = Visibility.Visible;
- //AnnotationListItems.AddRange(currentAnnotationArgs);
- //RefreshAnnotationListItems();
- }
- }
- /// <summary>
- /// 根据条件筛选列表数据
- /// </summary>
- /// <param name="annotArgsTypes"></param>
- /// <param name="colors"></param>
- /// <param name="authors"></param>
- /// <returns></returns>
- private ObservableCollection<AnnotationHandlerEventArgs> SelectAnnotationListItems(List<AnnotArgsType> annotArgsTypes, List<string> colors, List<string> authors)
- {
- ObservableCollection<AnnotationHandlerEventArgs> annotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
- #region 类型 颜色 作者
- if (annotArgsTypes.Count > 0 && colors.Count > 0 && authors.Count > 0)
- {
- foreach (var item in CurrentAnnotationLists)
- {
- foreach (var type in annotArgsTypes)
- {
- foreach (var color in colors)
- {
- foreach (var author in authors)
- {
- if (item.EventType == type && item.Author == author)
- {
- SelectAnnotationColors(item, color, annotationArgs);
- }
- }
- }
- }
- }
- }
- #endregion 类型 颜色 作者
- #region 颜色 作者
- else if (colors.Count > 0 && authors.Count > 0)
- {
- foreach (var item in CurrentAnnotationLists)
- {
- foreach (var color in colors)
- {
- foreach (var author in authors)
- {
- if (item.Author == author)
- {
- SelectAnnotationColors(item, color, annotationArgs);
- }
- }
- }
- }
- }
- #endregion 颜色 作者
- #region 类型 作者
- else if (annotArgsTypes.Count > 0 && authors.Count > 0)
- {
- foreach (var item in CurrentAnnotationLists)
- {
- foreach (var type in annotArgsTypes)
- {
- foreach (var author in authors)
- {
- if (item.EventType == type && item.Author == author)
- {
- annotationArgs.Add(item);
- }
- }
- }
- }
- }
- #endregion 类型 作者
- #region 类型 颜色
- else if (annotArgsTypes.Count > 0 && colors.Count > 0)
- {
- foreach (var item in CurrentAnnotationLists)
- {
- foreach (var type in annotArgsTypes)
- {
- foreach (var color in colors)
- {
- if (item.EventType == type)
- {
- SelectAnnotationColors(item, color, annotationArgs);
- }
- }
- }
- }
- }
- #endregion 类型 颜色
- #region 类型
- else if (annotArgsTypes.Count > 0)
- {
- foreach (var item in CurrentAnnotationLists)
- {
- foreach (var type in annotArgsTypes)
- {
- if (item.EventType == type)
- {
- annotationArgs.Add(item);
- }
- }
- }
- }
- #endregion 类型
- #region 颜色
- else if (colors.Count > 0)
- {
- foreach (var item in CurrentAnnotationLists)
- {
- foreach (var color in colors)
- {
- SelectAnnotationColors(item, color, annotationArgs);
- }
- }
- }
- #endregion 颜色
- #region 作者
- else if (authors.Count > 0)
- {
- foreach (var item in CurrentAnnotationLists)
- {
- foreach (var author in authors)
- {
- if (item.Author == author)
- {
- annotationArgs.Add(item);
- }
- }
- }
- }
- #endregion 作者
- return annotationArgs;
- }
- /// <summary>
- /// 根据颜色筛选注释
- /// </summary>
- /// <param name="item"></param>
- /// <param name="color"></param>
- /// <param name="annotationArgs"></param>
- private void SelectAnnotationColors(AnnotationHandlerEventArgs item, string color, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
- {
- AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs;
- switch (item.EventType)
- {
- case AnnotArgsType.AnnotFreeText://文本
- if (data is FreeTextAnnotArgs textAnnotArgs)
- {
- AddScreenAnnotationArgs(textAnnotArgs.FontColor, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotHighlight://高亮
- if (data is TextHighlightAnnotArgs highlightAnnotArgs)
- {
- AddScreenAnnotationArgs(highlightAnnotArgs.Color, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotFreehand://手绘
- if (data is FreehandAnnotArgs freehandAnnotArgs)
- {
- AddScreenAnnotationArgs(freehandAnnotArgs.InkColor, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotSquiggly://波浪线
- break;
- case AnnotArgsType.AnnotStamp://图章
- break;
- case AnnotArgsType.AnnotStrikeout://删除线
- if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
- {
- AddScreenAnnotationArgs(textStrikeoutAnnotArgs.Color, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotSticky://便签
- if (data is StickyAnnotArgs stickyAnnotArgs)
- {
- AddScreenAnnotationArgs(stickyAnnotArgs.Color, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotUnderline://下划线
- if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
- {
- AddScreenAnnotationArgs(textUnderlineAnnotArgs.Color, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotLine:
- //if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
- //{
- // //箭头
- //}
- //else
- //{
- // //线
- //}
- if (data is LineAnnotArgs lineAnnotArgs)
- {
- AddScreenAnnotationArgs(lineAnnotArgs.LineColor, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotSquare://矩形
- if (data is SquareAnnotArgs squareAnnotArgs)
- {
- AddScreenAnnotationArgs(squareAnnotArgs.LineColor, color, item, annotationArgs);
- AddScreenAnnotationArgs(squareAnnotArgs.BgColor, color, item, annotationArgs);
- }
- break;
- case AnnotArgsType.AnnotCircle://圆
- if (data is CircleAnnotArgs circleAnnotArgs)
- {
- AddScreenAnnotationArgs(circleAnnotArgs.LineColor, color, item, annotationArgs);
- AddScreenAnnotationArgs(circleAnnotArgs.BgColor, color, item, annotationArgs);
- }
- break;
- }
- }
- /// <summary>
- /// 对比颜色
- /// </summary>
- /// <param name="color1"></param>
- /// <param name="color"></param>
- /// <param name="item"></param>
- /// <param name="annotationArgs"></param>
- private void AddScreenAnnotationArgs(System.Windows.Media.Color color1, string color, AnnotationHandlerEventArgs item, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
- {
- if (color1.ToString() == color)
- {
- annotationArgs.Add(item);
- }
- }
- /// <summary>
- /// 添加附注的注释
- /// </summary>
- /// <param name="obj"></param>
- private void AddNotesEvent(object obj)
- {
- if (obj is ListBoxItem listBoxItem)
- {
- if (listBoxItem.DataContext is AnnotationHandlerEventArgs args)
- {
- if (args.EventType != AnnotArgsType.AnnotSticky && args.EventType != AnnotArgsType.AnnotFreeText && args.EventType != AnnotArgsType.AnnotLink)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.Annotation, args);
- 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);
- RefreshOneListItem(annotation.AnnotHandlerEventArgs);
- }
- }
- });
- }
- //else if (args.EventType == AnnotArgsType.AnnotFreeText)
- //{
- // StackPanel stackPanel = CommonHelper.FindVisualChild<StackPanel>(listBoxItem);
- // TextBox textBox = CommonHelper.FindVisualChild<TextBox>(stackPanel);
- // TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(stackPanel);
- // textBlock.Visibility = Visibility.Collapsed;
- // textBox.Visibility = Visibility.Visible;
- // textBox.Dispatcher.BeginInvoke(new Action(() =>
- // {
- // textBox.Focus();
- // textBox.SelectAll();
- // }));
- //}
- }
- }
- }
- private async void RefreshOneListItem(AnnotHandlerEventArgs annotation)
- {
- for (int i = 0; i < AnnotationListItems.Count; i++)
- {
- if (AnnotationListItems[i].PageIndex == annotation.PageIndex && AnnotationListItems[i].AnnotIndex == annotation.AnnotIndex)
- {
- AnnotationListItems[i] = GetAddAnnotEventArgs(annotation);
- for (int j = 0; j < CurrentAnnotationLists.Count; j++)
- {
- if (CurrentAnnotationLists[j].PageIndex == annotation.PageIndex && CurrentAnnotationLists[j].AnnotIndex == annotation.AnnotIndex)
- {
- CurrentAnnotationLists[j] = GetAddAnnotEventArgs(annotation);
- break;
- }
- }
- if (!listBox.SelectedItems.Contains(AnnotationListItems[i]))
- {
- listBox.SelectedItem = AnnotationListItems[i];
- }
- await Task.Delay(1);//不加延时 每页的第一个注释不会展开
- ExpandGroupHeader(AnnotationListItems[i], listBox);
- await Task.Delay(1);//不加延时 会有不滚动的现象
- listBox.ScrollIntoView(AnnotationListItems[i]);
- break;
- }
- }
- }
- public async void ScrollToAnnot(int pageindex, int annotindex, ListBox AnnotationList)
- {
- //var list = PdfViewer.GetAnnotCommentList(pageindex, PdfViewer.Document);
- for (int i = 0; i < AnnotationList.Items.Count; i++)
- {
- if ((AnnotationList.Items[i] as AnnotationHandlerEventArgs).AnnotIndex == annotindex && (AnnotationList.Items[i] as AnnotationHandlerEventArgs).PageIndex == pageindex)
- {
- var item = AnnotationList.Items[i] as AnnotationHandlerEventArgs;
- //需要手动搜寻在哪一个分组 展开分组头
- ExpandGroupHeader(item, AnnotationList);
- AnnotationList.SelectedItem = item;
- await Task.Delay(1);
- AnnotationList.ScrollIntoView(item);
- }
- }
- }
- /// <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);
- }
- }
- if (obj is AnnotationHandlerEventArgs annotation)
- {
- PdfViewer.SelectAnnotation(annotation.PageIndex, annotation.AnnotIndex);
- }
- if (obj is Dictionary<int, List<int>> eventArgs)
- {
- PdfViewer.SelectAnnotation(eventArgs);
- }
- }
- public async void UpdateAddedAnnot(int pageIndex, int annotIndex)
- {
- var annots = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
- for (int i = 0; i < annots.Count; i++)
- {
- AnnotHandlerEventArgs item = annots[i];
- if (item.AnnotIndex == annotIndex && item.EventType != AnnotArgsType.AnnotRedaction)
- {
- AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
- if (args.AnnotHandlerEventArgs != null)
- {
- AnnotationListItems.Add(args);
- CurrentAnnotationLists.Add(args);
- await Task.Delay(5);//不加延时 每页的第一个注释不会展开
- ExpandGroupHeader(args, listBox);
- listBox.SelectedItem = item;
- await Task.Delay(5);//不加延时 会有不滚动的现象
- listBox.ScrollIntoView(item);
- if (!listBox.SelectedItems.Contains(args))
- {
- listBox.SelectedItem = args;
- //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
- }
- }
- }
- }
- listBox.UpdateLayout();
- //SetGroupHeader(listBox);
- if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- }
- }
- public async void UpdateModifiedAnnot(int pageIndex, int annotIndex, bool isDelete)
- {
- try
- {
- var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
- if (isDelete && items.Count == 0)//当前页的最后一次删除
- {
- for (int k = 0; k < AnnotationListItems.Count; k++)
- {
- if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
- {
- AnnotationListItems.RemoveAt(k);
- AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
- CurrentAnnotationLists.Remove(annotation1);
- UpdateAnnotListAfterDelete(pageIndex, annotIndex);
- return;
- }
- }
- }
- if (isDelete)//删除
- {
- for (int k = 0; k < AnnotationListItems.Count; k++)
- {
- //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
- if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
- {
- AnnotationListItems.RemoveAt(k);
- AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
- CurrentAnnotationLists.Remove(annotation1);
- UpdateAnnotListAfterDelete(pageIndex, annotIndex);
- return;
- }
- }
- }
- for (int j = 0; j < items.Count; j++)
- {
- for (int k = 0; k < AnnotationListItems.Count; k++)
- {
- if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex && items[j].AnnotIndex == annotIndex)
- {
- AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
- AnnotationListItems[k] = GetAddAnnotEventArgs(items[j]);
- for (int i = 0; i < CurrentAnnotationLists.Count; i++)
- {
- if (CurrentAnnotationLists[i].PageIndex == pageIndex && CurrentAnnotationLists[i].AnnotIndex == annotIndex)
- {
- CurrentAnnotationLists[i] = GetAddAnnotEventArgs(items[j]);
- break;
- }
- }
- if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
- {
- listBox.SelectedItem = AnnotationListItems[k];
- //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
- }
- await Task.Delay(5);//不加延时 每页的第一个注释不会展开
- ExpandGroupHeader(AnnotationListItems[k], listBox);
- await Task.Delay(5);//不加延时 会有不滚动的现象
- listBox.ScrollIntoView(AnnotationListItems[k]);
- }
- }
- }
- listBox.UpdateLayout();
- //SetGroupHeader(listBox);
- }
- catch { }
- }
- /// <summary>
- /// 展开item所在分组
- /// </summary>
- private void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
- {
- try
- {
- var groups = listBox.Items.Groups;
- for (int i = 0; i < groups.Count; i++)
- {
- var l = groups[i] as CollectionViewGroup;
- if (l.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 { }
- }
- /// <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)
- {
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- }
- 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;
- }
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- KeyEventsHelper.KeyDown += ShortCut_KeyDown;
- }
- private void ShortCut_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
- {
- if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
- {
- if (listBox != null)
- {
- if (listBox.SelectedItems == null)
- {
- return;
- }
- listBox.SelectedItems.Clear();
- }
- }
- if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Delete))
- {
- if (listBox != null)
- {
- if (listBox.SelectedItems == null)
- {
- return;
- }
- List<int> pagelist = new List<int>();
- for (int i = 0; i < listBox.SelectedItems.Count; i++)
- {
- AnnotationHandlerEventArgs annotation = listBox.SelectedItems[i] as AnnotationHandlerEventArgs;
- pagelist.Add(listBox.Items.IndexOf(annotation));
- }
- pagelist.Sort();
- for (int i = 0; i < pagelist.Count; i++)
- {
- AnnotationHandlerEventArgs annotation = listBox.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
- if (annotation == null)
- {
- continue;
- }
- DeleteCommand.Execute(annotation);
- }
- }
- }
- }
- /// <summary>
- /// 默认按照PageIndex,分组 排序
- /// </summary>
- /// <param name="listBox"></param>
- private async void SetGroupHeader(ListBox listBox)
- {
- #region BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
- //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
- //CurrentAnnotationLists.AddRange(AnnotationListItems);
- //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- //ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
- //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));
- //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.Y), ListSortDirection.Ascending));
- //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.X), ListSortDirection.Ascending));
- #endregion BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
- CurrentAnnotationLists = GetDocumentAnnotionList();
- AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
- AnnotationListItems = GetDocumentAnnotionList();
- AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- //按照PageIndex,分组 排序
- ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
- 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;
- }
- /// <summary>
- /// 获取listbox绑定数据
- /// </summary>
- /// <returns></returns>
- public 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)
- {
- if (item.EventType == AnnotArgsType.AnnotLink)
- {
- if (item is LinkAnnotArgs link)
- {
- if (link.LinkType == LINK_TYPE.URI)
- {
- continue;
- }
- }
- }
- AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
- if (args.AnnotHandlerEventArgs != null)
- {
- list.Add(args);
- }
- }
- }
- }
- return list;
- }
- private AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
- {
- AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
- if (item.EventType == AnnotArgsType.AnnotFreehand)
- {
- BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
- args.WriteableBitmap = bitmap;
- }
- args.ClientRect = item.ClientRect;
- 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;
- return args;
- }
- /// <summary>
- /// 获取手绘图案
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="pageIndex"></param>
- /// <param name="annotIndex"></param>
- /// <returns></returns>
- public BitmapImage 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)
- {
- CPDFInkAnnotation cPDFInk = (CPDFInkAnnotation)docAnnots[annotIndex];
- //CRect rawRect = annot.GetRect();
- CRect rawRect = cPDFInk.Rect;
- 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;
- switch (docPage.Rotation)
- {
- case 0:
- case 2:
- drawWidth = (int)paintRect.Width;
- drawHeight = (int)paintRect.Height;
- break;
- case 1:
- case 3:
- drawWidth = (int)paintRect.Height;
- drawHeight = (int)paintRect.Width;
- break;
- }
- byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
- annot.UpdateAp();
- 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);
- BitmapImage bitmapImage = ConvertWriteableBitmapToBitmapImage(wirteBitmap, docPage.Rotation);
- cPDFInk.SetRect(rawRect);
- // 测试
- //using (FileStream stream = new FileStream(@"C:\Users\oyxh\Desktop\images\1.png", FileMode.Create))
- //{
- // PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
- // bitmapEncoder.Frames.Add(BitmapFrame.Create(wirteBitmap));
- // bitmapEncoder.Save(stream);
- //}
- return bitmapImage;
- }
- }
- return null;
- }
- public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm, int rotation)
- {
- BitmapImage bmImage = new BitmapImage();
- using (MemoryStream stream = new MemoryStream())
- {
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(wbm));
- encoder.Save(stream);
- bmImage.BeginInit();
- bmImage.CacheOption = BitmapCacheOption.OnLoad;
- bmImage.StreamSource = stream;
- switch (rotation)
- {
- case 0:
- bmImage.Rotation = Rotation.Rotate0;
- break;
- case 1:
- bmImage.Rotation = Rotation.Rotate90;
- break;
- case 2:
- bmImage.Rotation = Rotation.Rotate180;
- break;
- case 3:
- bmImage.Rotation = Rotation.Rotate270;
- break;
- }
- bmImage.EndInit();
- bmImage.Freeze();
- }
- return bmImage;
- }
- /// <summary>
- /// listbox的itemsource发生变化时
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
- {
- //currentAnnotationArgs.Clear();
- //currentAnnotationArgs.AddRange(obsSender);
- if (obsSender.Count < 1)
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- //currentAnnotationArgs.Clear();
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- }
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- }
- }
- }
|