123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887 |
- 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_Master.CustomControl;
- using PDF_Master.DataConvert;
- using PDF_Master.EventAggregators;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using PDF_Master.Model.BOTA;
- using PDF_Master.Properties;
- using PDF_Master.Views.BOTA;
- using PDF_Master.Views.PropertyPanel.AnnotPanel;
- using PDF_Master.Views.Scan;
- using Prism.Commands;
- using Prism.Events;
- 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.Management.Instrumentation;
- 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.Paper.ListPaperDocsSortBy;
- 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_Master.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_title;
- public string T_Title
- {
- get { return T_title; }
- set
- {
- SetProperty(ref T_title, 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_copyText;
- public string T_CopyText
- {
- get { return T_copyText; }
- set
- {
- SetProperty(ref T_copyText, 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 string T_delete;
- public string T_Delete
- {
- get { return T_delete; }
- set
- {
- SetProperty(ref T_delete, value);
- }
- }
- private Visibility screen = Visibility.Collapsed;
- /// <summary>
- /// 是否有筛选条件
- /// </summary>
- public Visibility Screen
- {
- get { return screen; }
- set
- {
- SetProperty(ref screen, 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");
- T_CopyText = App.MainPageLoader.GetString("Annotation_MenuCopyText");
- T_Delete = App.MainPageLoader.GetString("Annotation_MenuDelete");
- T_Title = App.MainPageLoader.GetString("Annotation_Title");
- }
- #endregion 文案
- private ListBox listBox;
- private IRegionManager region;
- private IDialogService dialogs;
- private IEventAggregator events;
- 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 Visibility isHideAnnotPanelVisibility = Visibility.Collapsed;
- public Visibility IsHideAnnotPanelVisibility
- {
- get { return isHideAnnotPanelVisibility; }
- set
- {
- SetProperty(ref isHideAnnotPanelVisibility, 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 == null)
- {
- return false;
- }
- else
- {
- 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 string Unicode { get; private set; }
- private bool IsFirst = true;
- private bool isHiddenAnnot = false;
- /// <summary>
- /// 判断是否为属性修改进入,属性修改,改变后不让再次其选中注释
- /// </summary>
- public bool IsModified = true;
- public AnnotationContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
- {
- region = regionManager;
- dialogs = dialogService;
- events = eventAggregator;
- Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- LoadedCommand = new DelegateCommand<object>(Loaded);
- ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand<object>(ListBoxItem_PreviewMouseLeftButtonDown);
- AddNotesCommand = new DelegateCommand<object>(AddNotesEvent);
- ScreenCommand = new DelegateCommand<object>(FilterCommentsEvent);
- 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();
- events.GetEvent<RefreshAnnotEvent>().Subscribe(RefreshData, e => e.Unicode == Unicode);
- events.GetEvent<PageEditNotifyEvent>().Subscribe(OnNotifyEvent, e => e.Unicode == Unicode && e.Type == NotifyType.PageCountChanged);
- }
- private void OnNotifyEvent(PageEditNotifyEventArgs obj)
- {
- RefreshAnnotationListItems();
- }
- public void HiddenAnnot()
- {
- isHiddenAnnot = true;
- colors.Clear();
- authors.Clear();
- annotArgsTypes.Clear();
- AnnotationListItems.Clear();
- CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
- AnnotationListItems.AddRange(CurrentAnnotationLists);
- Screen = Visibility.Collapsed;
- }
- public void DisplayAnnot()
- {
- if (ViewContentViewModel.mainViewModel.AnnotMouseLeftButtonDown)
- {
- return;
- }
- isHiddenAnnot = false;
- colors.Clear();
- authors.Clear();
- annotArgsTypes.Clear();
- AnnotationListItems.Clear();
- CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
- AnnotationListItems.AddRange(CurrentAnnotationLists);
- RefreshAnnotationListItems();
- Screen = Visibility.Collapsed;
- }
- /// <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.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportFailWarning"), App.ServiceLoader.GetString("Text_ok"));
- return;
- }
- PdfViewer.ReloadVisibleAnnots();
- //提取出来的注释文件 时间为空 则显示未系统当前时间
- CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
- //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.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportFailWarning") + ex.Message, App.ServiceLoader.GetString("Text_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()
- {
- if (AnnotationListItems == null) return;
- if (AnnotationListItems.Count < 0) return;
- 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(" ", App.MainPageLoader.GetString("DeleteAllAnnotation_Warning"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_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);
- }
- }
- }
- //删除多个
- if (obj is Dictionary<int, List<int>> dics)
- {
- foreach (KeyValuePair<int, List<int>> kvp in dics)
- {
- int key = kvp.Key;
- List<int> list = kvp.Value;
- foreach (var item in list)
- {
- var annotation2 = AnnotationListItems.FirstOrDefault(x => x.PageIndex == key && x.AnnotIndex == item);
- if (annotation2 != null)
- {
- AnnotationListItems.Remove(annotation2);
- }
- var annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == key && x.AnnotIndex == item);
- if (annotation1 != null)
- {
- CurrentAnnotationLists.Remove(annotation1);
- }
- if (annotation2 != null && annotation1 != null)
- {
- UpdateAnnotListAfterDelete(key, item);
- }
- }
- }
- PdfViewer.UndoManager.CanSave = true;
- PdfViewer.RemovePageAnnot(dics);
- }
- }
- 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);
- if (bitmap != null)
- 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)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportSuccess"), App.ServiceLoader.GetString("Text_ok"));
- }
- else
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportSuccess"), App.ServiceLoader.GetString("Text_ok"));
- }
- }
- }
- /// <summary>
- /// 筛选注释
- /// </summary>
- /// <param name="obj"></param>
- private void FilterCommentsEvent(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;
- Screen = Visibility.Visible;
- }
- else
- {
- CleanAnnotation(button);
- Screen = Visibility.Collapsed;
- }
- }
- 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 == null) return;
- if (AnnotationListItems.Count > 0)
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- await Task.Delay(5);
- ////展开数据
- //ExpandGroupHeader(AnnotationListItems, listBox);
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = true });
- }
- else
- {
- if (button != null)
- {
- button.IsEnabled = true;
- }
- IsEmptyPanelVisibility = Visibility.Visible;
- IsHideAnnotPanelVisibility = Visibility.Visible;
- }
- }
- /// <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)
- if (args.EventType != AnnotArgsType.AnnotSticky && args.EventType != AnnotArgsType.AnnotFreeText && args.EventType != AnnotArgsType.AnnotLink/*&&
- args.EventType != AnnotArgsType.AnnotHighlight && args.EventType != AnnotArgsType.AnnotUnderline
- && args.EventType != AnnotArgsType.AnnotStrikeout*/)
- {
- 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 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 (AnnotationListItems[i] != null)
- {
- //if (!listBox.SelectedItems.Contains(AnnotationListItems[i]))
- //{
- // listBox.SelectedItem = AnnotationListItems[i];
- //}
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, IsSelect = true, annotHandlerArgs = AnnotationListItems[i] });
- }
- //await Task.Delay(5);//不加延时 每页的第一个注释不会展开
- //await Task.Delay(1);//不加延时 每页的第一个注释不会展开
- //ExpandGroupHeader(AnnotationListItems[i], listBox);
- //await Task.Delay(1);//不加延时 会有不滚动的现象
- //listBox.ScrollIntoView(AnnotationListItems[i]);
- break;
- }
- }
- }
- public 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] is AnnotationHandlerEventArgs item)
- {
- if (item.AnnotIndex == annotindex && item.PageIndex == pageindex)
- {
- //var item = AnnotationList.Items[i] as AnnotationHandlerEventArgs;
- if (item.EventType != AnnotArgsType.AnnotLink)
- {
- #region 失败率比较高 当数据多的时候
- //需要手动搜寻在哪一个分组 展开分组头
- //ExpandGroupHeader(item, AnnotationList);
- //if (AnnotationList.SelectedItems.IndexOf(item) == -1)
- //{
- // AnnotationList.SelectedItem = item;
- // await Task.Delay(5);
- // AnnotationList.ScrollIntoView(item);
- //}
- #endregion 失败率比较高 当数据多的时候
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, IsSelect = true, annotHandlerArgs = item, IsExpand = false });
- }
- }
- }
- }
- }
- /// <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)
- {
- ViewContentViewModel.mainViewModel.AnnotMouseLeftButtonDown = true;
- if (PdfViewer.GetSelectAnnotCount() == 1)
- {
- List<AnnotHandlerEventArgs> annotationHandlers = PdfViewer.GetSelectionAnnots();
- var result = annotationHandlers.FindAll(x => x.PageIndex == annotation.PageIndex && x.AnnotIndex == annotation.AnnotIndex);
- if (result != null && result.Count > 0)
- {
- return;
- }
- }
- PdfViewer.SelectAnnotation(annotation.PageIndex, annotation.AnnotIndex);
- }
- else
- {
- ViewContentViewModel.mainViewModel.AnnotMouseLeftButtonDown = false;
- }
- if (obj is Dictionary<int, List<int>> eventArgs)
- {
- PdfViewer.SelectAnnotation(eventArgs);
- }
- }
- public async void UpdateAddedAnnot(BOTAContent bOTAContent, 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 is LinkAnnotArgs || args == null)
- {
- continue;
- }
- if (args.AnnotHandlerEventArgs != null)
- {
- AnnotationListItems.Add(args);
- CurrentAnnotationLists.Add(args);
- await Task.Delay(5);//不加延时 每页的第一个注释不会展开
- if (ViewContentViewModel.IsMultiSelected == false)
- {
- //添加 时 ,形状、图章、签名,添加后选中
- if (args.EventType == AnnotArgsType.AnnotSquare ||
- args.EventType == AnnotArgsType.AnnotCircle ||
- args.EventType == AnnotArgsType.AnnotLine ||
- args.EventType == AnnotArgsType.AnnotStamp
- /* ||args.EventType == AnnotArgsType.AnnotLink*/)
- {
- listBox.SelectedItem = args;
- }
- }
- //ExpandGroupHeader(args, listBox);
- // 关闭后选中关闭前的位置
- if (ViewContentViewModel.OpenBOTA && bOTAContent.TabItemAnnotation.IsSelected)
- {
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, annotHandlerArgs = args });
- }
- }
- }
- }
- listBox.UpdateLayout();
- //SetGroupHeader(listBox);
- if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- }
- }
- public void DeleteModifiedAnnot(int pageIndex, int annotIndex)
- {
- #region 历史版本
- //var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
- //if (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);
- // //AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
- // //listBox.ScrollIntoView(args);
- // //删除之后要拿到最新的数据源
- // CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
- // AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
- // AnnotationListItems.AddRange(CurrentAnnotationLists);
- // AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- // RefreshAnnotationListItems();
- // return;
- // }
- // }
- //}
- //else
- //{
- // for (int k = 0; k < AnnotationListItems.Count; k++)
- // {
- // //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
- // if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
- // {
- // //AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
- // //CurrentAnnotationLists.Remove(annotation1);
- // //AnnotationListItems.RemoveAt(k);
- // //UpdateAnnotListAfterDelete(pageIndex, annotIndex);
- // //删除之后要拿到最新的数据源
- // RefreshAnnotation(pageIndex);
- // return;
- // }
- // }
- //}
- #endregion 历史版本
- //更改更新注释方法 5.16
- RefreshAnnotationPage(pageIndex);
- }
- /// <summary>
- /// 将BOTA原本的对应页面所有注释删除,将更新后的所对应页面的注释更新到BOTA中,在滚动到对应页面第一个注释
- /// </summary>
- /// <param name="pageIndex">页面索引</param>
- private void RefreshAnnotationPage(int pageIndex)
- {
- List<int> pageindex = new List<int>();
- for (int k = 0; k < AnnotationListItems.Count; k++)
- {
- if (AnnotationListItems[k].PageIndex == pageIndex)
- {
- // AnnotationListItems.RemoveAt(k);
- pageindex.Add(k);
- }
- }
- CommonHelper.Reverseorder(ref pageindex);
- foreach (var a in pageindex)
- {
- AnnotationListItems.RemoveAt(a);
- }
- CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
- for (int k = 0; k < CurrentAnnotationLists.Count; k++)
- {
- if (CurrentAnnotationLists[k].PageIndex == pageIndex)
- {
- AnnotationListItems.Add(CurrentAnnotationLists[k]);
- }
- }
- //PageSortEvent();
- AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
- ////await Task.Delay(5);//不加延时 每页的第一个注释不会展开
- ////ExpandGroupHeader(args, listBox);
- ////await Task.Delay(5);//不加延时 会有不滚动的现象
- ////listBox.ScrollIntoView(args);
- ///
- IsModified = true;
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, IsDel = true, annotHandlerArgs = args });
- }
- private void RefreshAnnotation(int pageIndex)
- {
- CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
- AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
- AnnotationListItems.AddRange(CurrentAnnotationLists);
- AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- //RefreshAnnotationListItems();
- PageSortEvent();
- AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
- //await Task.Delay(5);//不加延时 每页的第一个注释不会展开
- //ExpandGroupHeader(args, listBox);
- //await Task.Delay(5);//不加延时 会有不滚动的现象
- //listBox.ScrollIntoView(args);
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, annotHandlerArgs = args });
- }
- public void UpdateModifiedAnnot(int pageIndex, int annotIndex/*, bool isDelete*/)
- {
- try
- {
- var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
- 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)
- {
- IsModified = false;
- AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
- if (args != null)
- {
- AnnotationListItems[k] = args;
- AnnotationListItems[k].Author = Settings.Default.AppProperties.Description.Author;
- for (int i = 0; i < CurrentAnnotationLists.Count; i++)
- {
- if (CurrentAnnotationLists[i].PageIndex == pageIndex && CurrentAnnotationLists[i].AnnotIndex == annotIndex)
- {
- CurrentAnnotationLists[i] = GetAddAnnotEventArgs(items[j]);
- break;
- }
- }
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, annotHandlerArgs = AnnotationListItems[k], IsSelect = true });
- }
- //await Task.Delay(5);//不加延时 每页的第一个注释不会展开
- //if (ViewContentViewModel.IsMultiSelected == false)
- //{
- // if (listBox.SelectedItems.IndexOf(AnnotationListItems[k]) == -1)
- // //if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
- // {
- // IsModified = false;
- // listBox.SelectedItem = AnnotationListItems[k];
- // //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
- // }
- //}
- //ExpandGroupHeader(AnnotationListItems[k], listBox);
- //await Task.Delay(5);//不加延时 会有不滚动的现象
- //listBox.ScrollIntoView(AnnotationListItems[k]);
- }
- }
- }
- listBox.UpdateLayout();
- //SetGroupHeader(listBox);
- }
- catch { }
- }
- /// <summary>
- /// 展开item所在分组
- /// </summary>
- private async void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
- {
- try
- {
- var groups = listBox.Items.Groups;
- if (groups != null && groups.Count > 0)
- {
- 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);
- if (stackpanel != null)
- {
- int count = VisualTreeHelper.GetChildrenCount(stackpanel);
- var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
- var g = CommonHelper.FindVisualChild<Expander>(groupItem);
- if (g != null)
- {
- await Task.Delay(5);
- 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;
- //RefreshAnnotationListItems();
- SetGroupHeader(listBox);
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- }
- private ObservableCollection<AnnotationHandlerEventArgs> HideAnnots;
- 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;
- }
- if (IsFirst)
- {
- //加密文档未解密进入时,不算进入次数,方便后续方法再次导航进来刷新
- if (!pdfview.Document.IsLocked)
- {
- IsFirst = false;
- }
- CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
- AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
- AnnotationListItems.CollectionChanged -= AnnotationListItems_CollectionChanged;
- AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- AnnotationListItems.AddRange(CurrentAnnotationLists);
- 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));
- //按照PageIndex,分组 排序
- SetGroupHeader(listBox);
- Screen = Visibility.Collapsed;
- colors.Clear();
- authors.Clear();
- annotArgsTypes.Clear();
- }
- if (viewContentViewModel.IsHiddenAnnot)
- {
- HiddenAnnot();
- }
- else
- {
- DisplayAnnot();
- }
- AnnotationListItems.CollectionChanged -= AnnotationListItems_CollectionChanged;
- AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- KeyEventsHelper.KeyDown += ShortCut_KeyDown;
- }
- private void RefreshData(RefreshAnnotArgs obj)
- {
- if (obj.Annotation != null)
- {
- RefreshOneListItem(obj.Annotation.AnnotHandlerEventArgs);
- }
- }
- 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;
- }
- //Windows:BOTA列表,移除Esc取消选中逻辑
- //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 void SetGroupHeader(ListBox listBox)
- {
- if (listBox == null) { return; }
- #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,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
- if (AnnotationListItems == null)
- {
- return;
- }
- if (AnnotationListItems.Count > 0)
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- //await Task.Delay(5);
- //展开数据
- //ExpandGroupHeader(AnnotationListItems, listBox);
- events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = true });
- }
- else
- {
- if (CurrentAnnotationLists.Count <= 0 && HideAnnots.Count > 1)
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- IsHideAnnotPanelVisibility = Visibility.Visible;
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- IsHideAnnotPanelVisibility = Visibility.Visible;
- }
- }
- }
- /// <summary>
- /// 展开列表项
- /// </summary>
- /// <param name="annotationListItems"></param>
- /// <param name="listBox"></param>
- private async 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)
- {
- await Task.Delay(5);
- 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(out ObservableCollection<AnnotationHandlerEventArgs> HideAnnots)
- {
- ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
- HideAnnots = 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
- && item.EventType != AnnotArgsType.AnnotLink)
- {
- if (item.EventType == AnnotArgsType.AnnotLink)
- {
- if (item is LinkAnnotArgs link)
- {
- if (link.LinkType == LINK_TYPE.URI)
- {
- continue;
- }
- }
- }
- AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
- if (args != null)
- {
- bool isVisible = PdfViewer.CheckAnnotVisible(args.PageIndex, args.AnnotIndex);
- if (args.AnnotHandlerEventArgs != null)
- {
- if (isVisible)
- {
- list.Add(args);
- }
- else
- {
- HideAnnots.Add(args);
- }
- }
- }
- }
- }
- }
- return list;
- }
- public AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
- {
- AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
- //原型图上,目前对波浪线的类型,在注释列表不显示
- //注释列表 不显示链接注释
- if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly
- && item.EventType != AnnotArgsType.AnnotLink)
- {
- if (item.EventType == AnnotArgsType.AnnotFreehand)
- {
- BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
- if (bitmap != null)
- 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);
- BitmapImage bitmapImage = null;
- if (drawWidth != 0 && drawHeight != 0)
- {
- WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
- wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
- bitmapImage = ConvertWriteableBitmapToBitmapImage(wirteBitmap, docPage.Rotation);
- }
- else
- {
- bitmapImage = null;
- }
- 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)
- {
- if (CurrentAnnotationLists.Count <= 0)
- {
- IsHideAnnotPanelVisibility = Visibility.Collapsed;
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- else
- {
- if (isHiddenAnnot)
- {
- IsHideAnnotPanelVisibility = Visibility.Collapsed;
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- IsHideAnnotPanelVisibility = Visibility.Visible;
- }
- }
- //IsEmptyPanelVisibility = Visibility.Visible;
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- IsHideAnnotPanelVisibility = Visibility.Collapsed;
- }
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- }
- }
- }
|