AnnotationContentViewModel.cs 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.AnnotEvent;
  7. using ComPDFKitViewer.PdfViewer;
  8. using DryIoc;
  9. using ImTools;
  10. using Microsoft.Office.Interop.PowerPoint;
  11. using Microsoft.Office.Interop.Word;
  12. using PDF_Master.CustomControl;
  13. using PDF_Master.DataConvert;
  14. using PDF_Master.EventAggregators;
  15. using PDF_Master.Helper;
  16. using PDF_Master.Model;
  17. using PDF_Master.Model.BOTA;
  18. using PDF_Master.Views.BOTA;
  19. using PDF_Master.Views.PropertyPanel.AnnotPanel;
  20. using PDF_Master.Views.Scan;
  21. using Prism.Commands;
  22. using Prism.Events;
  23. using Prism.Mvvm;
  24. using Prism.Regions;
  25. using Prism.Services.Dialogs;
  26. using System;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using System.Collections.ObjectModel;
  30. using System.ComponentModel;
  31. using System.Configuration;
  32. using System.Drawing;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Management.Instrumentation;
  36. using System.Text;
  37. using System.Threading.Tasks;
  38. using System.Windows;
  39. using System.Windows.Annotations;
  40. using System.Windows.Annotations.Storage;
  41. using System.Windows.Controls;
  42. using System.Windows.Data;
  43. using System.Windows.Documents;
  44. using System.Windows.Forms;
  45. using System.Windows.Input;
  46. using System.Windows.Markup;
  47. using System.Windows.Media;
  48. using System.Windows.Media.Imaging;
  49. using System.Windows.Media.Media3D;
  50. using System.Windows.Shapes;
  51. using static Dropbox.Api.Files.SearchMatchType;
  52. using static Dropbox.Api.TeamLog.SpaceCapsType;
  53. using static System.Net.Mime.MediaTypeNames;
  54. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  55. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
  56. using Border = System.Windows.Controls.Border;
  57. using ListBox = System.Windows.Controls.ListBox;
  58. using Task = System.Threading.Tasks.Task;
  59. using TextBox = System.Windows.Controls.TextBox;
  60. using Winform = System.Windows.Forms;
  61. namespace PDF_Master.ViewModels.BOTA
  62. {
  63. public class AnnotationContentViewModel : BindableBase, INavigationAware
  64. {
  65. #region 文案
  66. private string T_expand;
  67. public string T_Expand
  68. {
  69. get { return T_expand; }
  70. set
  71. {
  72. SetProperty(ref T_expand, value);
  73. }
  74. }
  75. private string T_collapse;
  76. public string T_Collapse
  77. {
  78. get { return T_collapse; }
  79. set
  80. {
  81. SetProperty(ref T_collapse, value);
  82. }
  83. }
  84. private string T_import;
  85. public string T_Import
  86. {
  87. get { return T_import; }
  88. set
  89. {
  90. SetProperty(ref T_import, value);
  91. }
  92. }
  93. private string T_copyText;
  94. public string T_CopyText
  95. {
  96. get { return T_copyText; }
  97. set
  98. {
  99. SetProperty(ref T_copyText, value);
  100. }
  101. }
  102. private string T_export;
  103. public string T_Export
  104. {
  105. get { return T_export; }
  106. set
  107. {
  108. SetProperty(ref T_export, value);
  109. }
  110. }
  111. private string T_deleteAll;
  112. public string T_DeleteAll
  113. {
  114. get { return T_deleteAll; }
  115. set
  116. {
  117. SetProperty(ref T_deleteAll, value);
  118. }
  119. }
  120. private string T_emptyTitle;
  121. public string T_EmptyTitle
  122. {
  123. get { return T_emptyTitle; }
  124. set
  125. {
  126. SetProperty(ref T_emptyTitle, value);
  127. }
  128. }
  129. private string T_emptyContext;
  130. public string T_EmptyContext
  131. {
  132. get { return T_emptyContext; }
  133. set
  134. {
  135. SetProperty(ref T_emptyContext, value);
  136. }
  137. }
  138. private string T_delete;
  139. public string T_Delete
  140. {
  141. get { return T_delete; }
  142. set
  143. {
  144. SetProperty(ref T_delete, value);
  145. }
  146. }
  147. private Visibility screen = Visibility.Collapsed;
  148. /// <summary>
  149. /// 是否有筛选条件
  150. /// </summary>
  151. public Visibility Screen
  152. {
  153. get { return screen; }
  154. set
  155. {
  156. SetProperty(ref screen, value);
  157. }
  158. }
  159. private void InitString()
  160. {
  161. T_Expand = App.MainPageLoader.GetString("Annotation_MenuExpand");
  162. T_Collapse = App.MainPageLoader.GetString("Annotation_MenuCollapse");
  163. T_Import = App.MainPageLoader.GetString("Annotation_MenuImport");
  164. T_Export = App.MainPageLoader.GetString("Annotation_MenuExport");
  165. T_DeleteAll = App.MainPageLoader.GetString("Annotation_MenuDeleteAll");
  166. T_EmptyTitle = App.MainPageLoader.GetString("Annotation_EmptyTitle");
  167. T_EmptyContext = App.MainPageLoader.GetString("Annotation_EmptyContext");
  168. T_CopyText = App.MainPageLoader.GetString("Annotation_MenuCopyText");
  169. T_Delete = App.MainPageLoader.GetString("Annotation_MenuDelete");
  170. }
  171. #endregion 文案
  172. private ListBox listBox;
  173. private IRegionManager region;
  174. private IDialogService dialogs;
  175. private IEventAggregator events;
  176. private Visibility isEmptyPanelVisibility = Visibility.Visible;
  177. public ViewContentViewModel ViewContentViewModel { get; set; }
  178. public CPDFViewer PdfViewer { get; set; }
  179. public Visibility IsEmptyPanelVisibility
  180. {
  181. get { return isEmptyPanelVisibility; }
  182. set
  183. {
  184. SetProperty(ref isEmptyPanelVisibility, value);
  185. }
  186. }
  187. private Visibility isHideAnnotPanelVisibility = Visibility.Collapsed;
  188. public Visibility IsHideAnnotPanelVisibility
  189. {
  190. get { return isHideAnnotPanelVisibility; }
  191. set
  192. {
  193. SetProperty(ref isHideAnnotPanelVisibility, value);
  194. }
  195. }
  196. private ObservableCollection<AnnotationHandlerEventArgs> currentAnnotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
  197. public ObservableCollection<AnnotationHandlerEventArgs> CurrentAnnotationLists { get => currentAnnotationArgs; set => currentAnnotationArgs = value; }
  198. private AnnotationSortOrder annotationSortOrder = AnnotationSortOrder.PageIndexAscending;
  199. public AnnotationSortOrder AnnotationSortOrder
  200. {
  201. get { return annotationSortOrder; }
  202. set { annotationSortOrder = value; }
  203. }
  204. private ObservableCollection<AnnotationHandlerEventArgs> annotationListItems;
  205. public ObservableCollection<AnnotationHandlerEventArgs> AnnotationListItems
  206. {
  207. get { return annotationListItems; }
  208. set
  209. {
  210. SetProperty(ref annotationListItems, value);
  211. }
  212. }
  213. private bool CanIsEnabled()
  214. {
  215. if (AnnotationListItems.Count > 0)
  216. {
  217. return true;
  218. }
  219. else
  220. {
  221. return false;
  222. }
  223. }
  224. private List<string> colors = new List<string>();
  225. private List<string> authors = new List<string>();
  226. private List<AnnotArgsType> annotArgsTypes = new List<AnnotArgsType>();
  227. public DelegateCommand<object> LoadedCommand { get; set; }
  228. public DelegateCommand<object> ListBoxItemPreviewMouseLeftButtonDown { get; set; }
  229. public DelegateCommand<object> AddNotesCommand { get; set; }
  230. public DelegateCommand<object> ScreenCommand { get; set; }
  231. public DelegateCommand ExportCommentsCommand { get; set; }
  232. public DelegateCommand ImportCommentsCommand { get; set; }
  233. public DelegateCommand<object> DeleteCommand { get; set; }
  234. public DelegateCommand DeleteAllCommand { get; set; }
  235. public DelegateCommand PageSortCommand { get; set; }
  236. public DelegateCommand TimeAscendingCommand { get; set; }
  237. public DelegateCommand TimeDescendingCommand { get; set; }
  238. public string Unicode { get; private set; }
  239. private bool IsFirst = true;
  240. public AnnotationContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  241. {
  242. region = regionManager;
  243. dialogs = dialogService;
  244. events = eventAggregator;
  245. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  246. LoadedCommand = new DelegateCommand<object>(Loaded);
  247. ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand<object>(ListBoxItem_PreviewMouseLeftButtonDown);
  248. AddNotesCommand = new DelegateCommand<object>(AddNotesEvent);
  249. ScreenCommand = new DelegateCommand<object>(ScreenEvent);
  250. ExportCommentsCommand = new DelegateCommand(ExportCommentsEvent, CanIsEnabled).ObservesProperty(() => AnnotationListItems);
  251. ImportCommentsCommand = new DelegateCommand(ImportCommentsEvent);
  252. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  253. DeleteAllCommand = new DelegateCommand(DeleteAllEvent, CanIsEnabled).ObservesProperty(() => AnnotationListItems);
  254. PageSortCommand = new DelegateCommand(PageSortEvent);
  255. TimeAscendingCommand = new DelegateCommand(TimeAscendingEvent);
  256. TimeDescendingCommand = new DelegateCommand(TimeDescendingEvent);
  257. InitString();
  258. events.GetEvent<RefreshAnnotEvent>().Subscribe(RefreshData, e => e.Unicode == Unicode);
  259. events.GetEvent<PageEditNotifyEvent>().Subscribe(OnNotifyEvent, e => e.Unicode == Unicode && e.Type == NotifyType.PageCountChanged);
  260. }
  261. private void OnNotifyEvent(PageEditNotifyEventArgs obj)
  262. {
  263. RefreshAnnotationListItems();
  264. }
  265. private bool isHiddenAnnot = false;
  266. public void HiddenAnnot()
  267. {
  268. isHiddenAnnot = true;
  269. AnnotationListItems.Clear();
  270. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  271. AnnotationListItems.AddRange(CurrentAnnotationLists);
  272. }
  273. public void DisplayAnnot()
  274. {
  275. isHiddenAnnot = false;
  276. AnnotationListItems.Clear();
  277. //AnnotationListItems = GetDocumentAnnotionList(out ObservableCollection<AnnotationHandlerEventArgs> HideAnnots);
  278. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  279. AnnotationListItems.AddRange(CurrentAnnotationLists);
  280. RefreshAnnotationListItems();
  281. }
  282. /// <summary>
  283. /// 导入注释
  284. /// </summary>
  285. /// <param name="obj"></param>
  286. private async void ImportCommentsEvent()
  287. {
  288. Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  289. dlg.Filter = "PDF|*.xfdf";
  290. dlg.DefaultExt = ".xfdf";
  291. if (dlg.ShowDialog() == true)
  292. {
  293. try
  294. {
  295. string fileName = dlg.FileName;
  296. await Task.Delay(10);
  297. var result = PdfViewer.ImportAnnotationFromXFDFPath(fileName);
  298. if (result == false)
  299. {
  300. //MessageBoxEx.Show("导入失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
  301. AlertsMessage alertsMessage = new AlertsMessage();
  302. alertsMessage.Show("提示", "导入失败!", "OK");
  303. return;
  304. }
  305. PdfViewer.ReloadVisibleAnnots();
  306. //提取出来的注释文件 时间为空 则显示未系统当前时间
  307. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  308. //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  309. //CurrentAnnotationLists = new ObservableCollection<AnnotationHandlerEventArgs>(CurrentAnnotationLists.OrderBy(item => item.ClientRect.X).ThenBy(item => item.ClientRect.Y));
  310. AnnotationListItems.Clear();
  311. AnnotationListItems.AddRange(CurrentAnnotationLists);
  312. await Task.Delay(5);
  313. RefreshAnnotationListItems(null);
  314. PdfViewer.UndoManager.CanSave = true;
  315. }
  316. catch (Exception ex)
  317. {
  318. AlertsMessage alertsMessage = new AlertsMessage();
  319. alertsMessage.Show("提示", "导入失败!" + ex.Message, "OK");
  320. }
  321. }
  322. }
  323. /// <summary>
  324. /// 时间倒序
  325. /// </summary>
  326. private void TimeDescendingEvent()
  327. {
  328. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
  329. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  330. v.GroupDescriptions.Clear();
  331. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  332. v.SortDescriptions.Clear();
  333. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  334. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  335. AnnotationSortOrder = AnnotationSortOrder.TimeDescending;
  336. }
  337. /// <summary>
  338. /// 时间正序
  339. /// </summary>
  340. private void TimeAscendingEvent()
  341. {
  342. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
  343. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  344. v.GroupDescriptions.Clear();
  345. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  346. v.SortDescriptions.Clear();
  347. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  348. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  349. AnnotationSortOrder = AnnotationSortOrder.TimeAscending;
  350. }
  351. /// <summary>
  352. /// 页面排序
  353. /// </summary>
  354. private void PageSortEvent()
  355. {
  356. if (AnnotationListItems == null) return;
  357. if (AnnotationListItems.Count < 0) return;
  358. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
  359. v.GroupDescriptions.Clear();
  360. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  361. v.SortDescriptions.Clear();
  362. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  363. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  364. AnnotationSortOrder = AnnotationSortOrder.PageIndexAscending;
  365. }
  366. /// <summary>
  367. /// 删除所有注释
  368. /// </summary>
  369. private void DeleteAllEvent()
  370. {
  371. //调用集中删除的接口 方便一次性undo
  372. Dictionary<int, List<int>> deleteLists = new Dictionary<int, List<int>>();
  373. for (int i = 0; i < AnnotationListItems.Count; i++)
  374. {
  375. AnnotationHandlerEventArgs item = AnnotationListItems[i] as AnnotationHandlerEventArgs;
  376. if (!deleteLists.ContainsKey(item.PageIndex))
  377. {
  378. deleteLists.Add(item.PageIndex, new List<int>() { item.AnnotIndex });
  379. }
  380. else
  381. {
  382. var pagelist = deleteLists[item.PageIndex];
  383. pagelist.Add(item.AnnotIndex);
  384. }
  385. }
  386. AlertsMessage alertsMessage = new AlertsMessage();
  387. alertsMessage.ShowDialog(" ", "This action will permanently delete all comments,\r\nWhether to continue??", "Cancel", "OK");
  388. if (alertsMessage.result == ContentResult.Ok)
  389. {
  390. PdfViewer.RemovePageAnnot(deleteLists);
  391. AnnotationListItems.Clear();
  392. CurrentAnnotationLists.Clear();
  393. PdfViewer.UndoManager.CanSave = true;
  394. }
  395. }
  396. /// <summary>
  397. /// 删除注释,单个/多个
  398. /// </summary>
  399. /// <param name="obj"></param>
  400. private void DelegateEvent(object obj)
  401. {
  402. if (obj is AnnotationHandlerEventArgs annotation)
  403. {
  404. if (annotation != null)
  405. {
  406. var result = PdfViewer.RemovePageAnnot(annotation.PageIndex, annotation.AnnotIndex);
  407. if (result)
  408. {
  409. RemoveItem(annotation);
  410. }
  411. }
  412. }
  413. }
  414. public void RemoveItem(AnnotationHandlerEventArgs annotation)
  415. {
  416. AnnotationListItems.Remove(annotation);
  417. var annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == annotation.PageIndex && x.AnnotIndex == annotation.AnnotIndex);
  418. CurrentAnnotationLists.Remove(annotation1);
  419. //记录是删除了哪些页面的注释,然后更新对应页面的注释即可
  420. UpdateAnnotListAfterDelete(annotation.PageIndex, annotation.AnnotIndex);
  421. PdfViewer.UndoManager.CanSave = true;
  422. }
  423. /// <summary>
  424. /// 删除之后,更新
  425. /// </summary>
  426. /// <param name="pageIndex"></param>
  427. /// <param name="annoteIndex"></param>
  428. private void UpdateAnnotListAfterDelete(int pageIndex, int annoteIndex)
  429. {
  430. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  431. for (int j = 0; j < items.Count; j++)//用修改赋值的方式 可以解决删除后表头折叠的问题
  432. {
  433. if (items[j].AnnotIndex >= annoteIndex)//只需要更新比删除元素索引大的注释
  434. {
  435. for (int k = 0; k < AnnotationListItems.Count; k++)
  436. {
  437. //相当于将后面的索引-1
  438. if (AnnotationListItems[k].PageIndex == pageIndex &&
  439. AnnotationListItems[k].AnnotIndex == (items[j].AnnotIndex + 1) &&
  440. string.Equals(AnnotationListItems[k].MarkupContent, items[j].MarkupContent) &&
  441. string.Equals(AnnotationListItems[k].Content, items[j].Content) &&
  442. string.Equals(AnnotationListItems[k].CreateTime, items[j].CreateTime) &&
  443. string.Equals(AnnotationListItems[k].Author, items[j].Author)
  444. )
  445. {
  446. AnnotationListItems[k].AnnotHandlerEventArgs = items[j];
  447. AnnotationListItems[k].PageIndex = items[j].PageIndex;
  448. AnnotationListItems[k].AnnotIndex = items[j].AnnotIndex;
  449. AnnotationListItems[k].EventType = items[j].EventType;
  450. AnnotationListItems[k].CreateTime = items[j].CreateTime;
  451. AnnotationListItems[k].UpdateTime = items[j].UpdateTime;
  452. if (items[j].EventType == AnnotArgsType.AnnotFreehand)
  453. {
  454. BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, items[j].PageIndex, items[j].AnnotIndex);
  455. if (bitmap != null)
  456. AnnotationListItems[k].WriteableBitmap = bitmap;
  457. }
  458. AnnotationListItems[k].Content = items[j].Content;
  459. AnnotationListItems[k].MarkupContent = items[j].MarkupContent;
  460. AnnotationListItems[k].Author = items[j].Author;
  461. AnnotationListItems[k].Locked = items[j].Locked;
  462. AnnotationListItems[k].ReadOnly = items[j].ReadOnly;
  463. AnnotationListItems[k].FormField = items[j].FormField;
  464. AnnotationListItems[k].Document = PdfViewer.Document;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. /// <summary>
  471. /// 导出注释
  472. /// </summary>
  473. /// <param name="obj"></param>
  474. private void ExportCommentsEvent()
  475. {
  476. if (ViewContentViewModel.CanSave)
  477. {
  478. ViewContentViewModel.SaveFile.Execute();
  479. }
  480. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  481. dlg.Filter = "PDF|*.xfdf";
  482. dlg.DefaultExt = ".xfdf";
  483. dlg.FileName = PdfViewer.Document.FileName;
  484. if (dlg.ShowDialog() == true)
  485. {
  486. string fileName = dlg.FileName;
  487. var result = PdfViewer.ExportAnnotationToXFDFPath(fileName);
  488. if (result)
  489. {
  490. //MessageBoxEx.Show("导出成功", "", Winform.MessageBoxButtons.OK);
  491. AlertsMessage alertsMessage = new AlertsMessage();
  492. alertsMessage.Show("提示", "导出成功!", "OK");
  493. }
  494. else
  495. {
  496. AlertsMessage alertsMessage = new AlertsMessage();
  497. alertsMessage.Show("提示", "导出失败!", "OK");
  498. //MessageBoxEx.Show("导出失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
  499. }
  500. }
  501. }
  502. /// <summary>
  503. /// 筛选注释
  504. /// </summary>
  505. /// <param name="obj"></param>
  506. private void ScreenEvent(object obj)
  507. {
  508. if (obj is System.Windows.Controls.Button button)
  509. {
  510. DialogParameters value = new DialogParameters();
  511. value.Add(ParameterNames.AnnotationList, CurrentAnnotationLists);
  512. value.Add(ParameterNames.AnnotArgsTypes, annotArgsTypes);
  513. value.Add(ParameterNames.AnnotationColors, colors);
  514. value.Add(ParameterNames.AnnotationAuthor, authors);
  515. dialogs.ShowDialog(DialogNames.ScreenAnnotationDialog, value, e =>
  516. {
  517. if (e.Result == ButtonResult.OK && e.Parameters != null)
  518. {
  519. if (e.Parameters.ContainsKey(ParameterNames.AnnotArgsTypes))
  520. {
  521. annotArgsTypes = e.Parameters.GetValue<List<AnnotArgsType>>(ParameterNames.AnnotArgsTypes);
  522. }
  523. if (e.Parameters.ContainsKey(ParameterNames.AnnotationColors))
  524. {
  525. colors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationColors);
  526. }
  527. if (e.Parameters.ContainsKey(ParameterNames.AnnotationAuthor))
  528. {
  529. authors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationAuthor);
  530. }
  531. if (annotArgsTypes.Count > 0 || colors.Count > 0 || authors.Count > 0)
  532. {
  533. ObservableCollection<AnnotationHandlerEventArgs> screenAnnotationArgs = SelectAnnotationListItems(annotArgsTypes, colors, authors);
  534. AnnotationListItems.Clear();
  535. AnnotationListItems.AddRange(screenAnnotationArgs);
  536. //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  537. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  538. RefreshAnnotationListItems(button);
  539. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  540. Screen = Visibility.Visible;
  541. }
  542. else
  543. {
  544. CleanAnnotation(button);
  545. Screen = Visibility.Collapsed;
  546. }
  547. }
  548. else if (e.Result == ButtonResult.Cancel && e.Parameters != null)
  549. {
  550. //CleanAnnotation(button);
  551. }
  552. });
  553. }
  554. }
  555. private void CleanAnnotation(System.Windows.Controls.Button button)
  556. {
  557. colors.Clear();
  558. authors.Clear();
  559. annotArgsTypes.Clear();
  560. AnnotationListItems.Clear();
  561. AnnotationListItems.AddRange(CurrentAnnotationLists);
  562. //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  563. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  564. RefreshAnnotationListItems(button);
  565. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  566. }
  567. /// <summary>
  568. /// 刷新列表数据
  569. /// </summary>
  570. /// <param name="button"></param>
  571. public async void RefreshAnnotationListItems(System.Windows.Controls.Button button = null)
  572. {
  573. switch (AnnotationSortOrder)
  574. {
  575. case AnnotationSortOrder.TimeAscending:
  576. TimeAscendingEvent();
  577. break;
  578. case AnnotationSortOrder.TimeDescending:
  579. TimeDescendingEvent();
  580. break;
  581. case AnnotationSortOrder.PageIndexAscending:
  582. PageSortEvent();
  583. break;
  584. default:
  585. break;
  586. }
  587. if (AnnotationListItems == null) return;
  588. if (AnnotationListItems.Count > 0)
  589. {
  590. IsEmptyPanelVisibility = Visibility.Collapsed;
  591. await Task.Delay(2);
  592. //展开数据
  593. ExpandGroupHeader(AnnotationListItems, listBox);
  594. }
  595. else
  596. {
  597. if (button != null)
  598. {
  599. button.IsEnabled = true;
  600. }
  601. IsEmptyPanelVisibility = Visibility.Visible;
  602. IsHideAnnotPanelVisibility = Visibility.Visible;
  603. }
  604. }
  605. /// <summary>
  606. /// 根据条件筛选列表数据
  607. /// </summary>
  608. /// <param name="annotArgsTypes"></param>
  609. /// <param name="colors"></param>
  610. /// <param name="authors"></param>
  611. /// <returns></returns>
  612. private ObservableCollection<AnnotationHandlerEventArgs> SelectAnnotationListItems(List<AnnotArgsType> annotArgsTypes, List<string> colors, List<string> authors)
  613. {
  614. ObservableCollection<AnnotationHandlerEventArgs> annotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
  615. #region 类型 颜色 作者
  616. if (annotArgsTypes.Count > 0 && colors.Count > 0 && authors.Count > 0)
  617. {
  618. foreach (var item in CurrentAnnotationLists)
  619. {
  620. foreach (var type in annotArgsTypes)
  621. {
  622. foreach (var color in colors)
  623. {
  624. foreach (var author in authors)
  625. {
  626. if (item.EventType == type && item.Author == author)
  627. {
  628. SelectAnnotationColors(item, color, annotationArgs);
  629. }
  630. }
  631. }
  632. }
  633. }
  634. }
  635. #endregion 类型 颜色 作者
  636. #region 颜色 作者
  637. else if (colors.Count > 0 && authors.Count > 0)
  638. {
  639. foreach (var item in CurrentAnnotationLists)
  640. {
  641. foreach (var color in colors)
  642. {
  643. foreach (var author in authors)
  644. {
  645. if (item.Author == author)
  646. {
  647. SelectAnnotationColors(item, color, annotationArgs);
  648. }
  649. }
  650. }
  651. }
  652. }
  653. #endregion 颜色 作者
  654. #region 类型 作者
  655. else if (annotArgsTypes.Count > 0 && authors.Count > 0)
  656. {
  657. foreach (var item in CurrentAnnotationLists)
  658. {
  659. foreach (var type in annotArgsTypes)
  660. {
  661. foreach (var author in authors)
  662. {
  663. if (item.EventType == type && item.Author == author)
  664. {
  665. annotationArgs.Add(item);
  666. }
  667. }
  668. }
  669. }
  670. }
  671. #endregion 类型 作者
  672. #region 类型 颜色
  673. else if (annotArgsTypes.Count > 0 && colors.Count > 0)
  674. {
  675. foreach (var item in CurrentAnnotationLists)
  676. {
  677. foreach (var type in annotArgsTypes)
  678. {
  679. foreach (var color in colors)
  680. {
  681. if (item.EventType == type)
  682. {
  683. SelectAnnotationColors(item, color, annotationArgs);
  684. }
  685. }
  686. }
  687. }
  688. }
  689. #endregion 类型 颜色
  690. #region 类型
  691. else if (annotArgsTypes.Count > 0)
  692. {
  693. foreach (var item in CurrentAnnotationLists)
  694. {
  695. foreach (var type in annotArgsTypes)
  696. {
  697. if (item.EventType == type)
  698. {
  699. annotationArgs.Add(item);
  700. }
  701. }
  702. }
  703. }
  704. #endregion 类型
  705. #region 颜色
  706. else if (colors.Count > 0)
  707. {
  708. foreach (var item in CurrentAnnotationLists)
  709. {
  710. foreach (var color in colors)
  711. {
  712. SelectAnnotationColors(item, color, annotationArgs);
  713. }
  714. }
  715. }
  716. #endregion 颜色
  717. #region 作者
  718. else if (authors.Count > 0)
  719. {
  720. foreach (var item in CurrentAnnotationLists)
  721. {
  722. foreach (var author in authors)
  723. {
  724. if (item.Author == author)
  725. {
  726. annotationArgs.Add(item);
  727. }
  728. }
  729. }
  730. }
  731. #endregion 作者
  732. return annotationArgs;
  733. }
  734. /// <summary>
  735. /// 根据颜色筛选注释
  736. /// </summary>
  737. /// <param name="item"></param>
  738. /// <param name="color"></param>
  739. /// <param name="annotationArgs"></param>
  740. private void SelectAnnotationColors(AnnotationHandlerEventArgs item, string color, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  741. {
  742. AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs;
  743. switch (item.EventType)
  744. {
  745. case AnnotArgsType.AnnotFreeText://文本
  746. if (data is FreeTextAnnotArgs textAnnotArgs)
  747. {
  748. AddScreenAnnotationArgs(textAnnotArgs.FontColor, color, item, annotationArgs);
  749. }
  750. break;
  751. case AnnotArgsType.AnnotHighlight://高亮
  752. if (data is TextHighlightAnnotArgs highlightAnnotArgs)
  753. {
  754. AddScreenAnnotationArgs(highlightAnnotArgs.Color, color, item, annotationArgs);
  755. }
  756. break;
  757. case AnnotArgsType.AnnotFreehand://手绘
  758. if (data is FreehandAnnotArgs freehandAnnotArgs)
  759. {
  760. AddScreenAnnotationArgs(freehandAnnotArgs.InkColor, color, item, annotationArgs);
  761. }
  762. break;
  763. case AnnotArgsType.AnnotSquiggly://波浪线
  764. break;
  765. case AnnotArgsType.AnnotStamp://图章
  766. break;
  767. case AnnotArgsType.AnnotStrikeout://删除线
  768. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  769. {
  770. AddScreenAnnotationArgs(textStrikeoutAnnotArgs.Color, color, item, annotationArgs);
  771. }
  772. break;
  773. case AnnotArgsType.AnnotSticky://便签
  774. if (data is StickyAnnotArgs stickyAnnotArgs)
  775. {
  776. AddScreenAnnotationArgs(stickyAnnotArgs.Color, color, item, annotationArgs);
  777. }
  778. break;
  779. case AnnotArgsType.AnnotUnderline://下划线
  780. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  781. {
  782. AddScreenAnnotationArgs(textUnderlineAnnotArgs.Color, color, item, annotationArgs);
  783. }
  784. break;
  785. case AnnotArgsType.AnnotLine:
  786. //if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
  787. //{
  788. // //箭头
  789. //}
  790. //else
  791. //{
  792. // //线
  793. //}
  794. if (data is LineAnnotArgs lineAnnotArgs)
  795. {
  796. AddScreenAnnotationArgs(lineAnnotArgs.LineColor, color, item, annotationArgs);
  797. }
  798. break;
  799. case AnnotArgsType.AnnotSquare://矩形
  800. if (data is SquareAnnotArgs squareAnnotArgs)
  801. {
  802. AddScreenAnnotationArgs(squareAnnotArgs.LineColor, color, item, annotationArgs);
  803. AddScreenAnnotationArgs(squareAnnotArgs.BgColor, color, item, annotationArgs);
  804. }
  805. break;
  806. case AnnotArgsType.AnnotCircle://圆
  807. if (data is CircleAnnotArgs circleAnnotArgs)
  808. {
  809. AddScreenAnnotationArgs(circleAnnotArgs.LineColor, color, item, annotationArgs);
  810. AddScreenAnnotationArgs(circleAnnotArgs.BgColor, color, item, annotationArgs);
  811. }
  812. break;
  813. }
  814. }
  815. /// <summary>
  816. /// 对比颜色
  817. /// </summary>
  818. /// <param name="color1"></param>
  819. /// <param name="color"></param>
  820. /// <param name="item"></param>
  821. /// <param name="annotationArgs"></param>
  822. private void AddScreenAnnotationArgs(System.Windows.Media.Color color1, string color, AnnotationHandlerEventArgs item, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  823. {
  824. if (color1.ToString() == color)
  825. {
  826. annotationArgs.Add(item);
  827. }
  828. }
  829. /// <summary>
  830. /// 添加附注的注释
  831. /// </summary>
  832. /// <param name="obj"></param>
  833. private void AddNotesEvent(object obj)
  834. {
  835. if (obj is ListBoxItem listBoxItem)
  836. {
  837. if (listBoxItem.DataContext is AnnotationHandlerEventArgs args)
  838. {
  839. //if (args.EventType != AnnotArgsType.AnnotSticky && args.EventType != AnnotArgsType.AnnotFreeText && args.EventType != AnnotArgsType.AnnotLink)
  840. if (args.EventType != AnnotArgsType.AnnotSticky && args.EventType != AnnotArgsType.AnnotFreeText &&
  841. args.EventType != AnnotArgsType.AnnotHighlight && args.EventType != AnnotArgsType.AnnotUnderline
  842. && args.EventType != AnnotArgsType.AnnotStrikeout && args.EventType != AnnotArgsType.AnnotLink)
  843. {
  844. DialogParameters value = new DialogParameters();
  845. value.Add(ParameterNames.Annotation, args);
  846. dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
  847. {
  848. if (e.Result == ButtonResult.OK && e.Parameters != null)
  849. {
  850. PdfViewer.UndoManager.CanSave = true;
  851. if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
  852. {
  853. AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
  854. RefreshOneListItem(annotation.AnnotHandlerEventArgs);
  855. }
  856. }
  857. });
  858. }
  859. //else if (args.EventType == AnnotArgsType.AnnotFreeText)
  860. //{
  861. // StackPanel stackPanel = CommonHelper.FindVisualChild<StackPanel>(listBoxItem);
  862. // TextBox textBox = CommonHelper.FindVisualChild<TextBox>(stackPanel);
  863. // TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(stackPanel);
  864. // textBlock.Visibility = Visibility.Collapsed;
  865. // textBox.Visibility = Visibility.Visible;
  866. // textBox.Dispatcher.BeginInvoke(new Action(() =>
  867. // {
  868. // textBox.Focus();
  869. // textBox.SelectAll();
  870. // }));
  871. //}
  872. }
  873. }
  874. }
  875. private async void RefreshOneListItem(AnnotHandlerEventArgs annotation)
  876. {
  877. for (int i = 0; i < AnnotationListItems.Count; i++)
  878. {
  879. if (AnnotationListItems[i].PageIndex == annotation.PageIndex && AnnotationListItems[i].AnnotIndex == annotation.AnnotIndex)
  880. {
  881. AnnotationListItems[i] = GetAddAnnotEventArgs(annotation);
  882. for (int j = 0; j < CurrentAnnotationLists.Count; j++)
  883. {
  884. if (CurrentAnnotationLists[j].PageIndex == annotation.PageIndex && CurrentAnnotationLists[j].AnnotIndex == annotation.AnnotIndex)
  885. {
  886. CurrentAnnotationLists[j] = GetAddAnnotEventArgs(annotation);
  887. break;
  888. }
  889. }
  890. if (!listBox.SelectedItems.Contains(AnnotationListItems[i]))
  891. {
  892. listBox.SelectedItem = AnnotationListItems[i];
  893. }
  894. await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  895. ExpandGroupHeader(AnnotationListItems[i], listBox);
  896. await Task.Delay(1);//不加延时 会有不滚动的现象
  897. listBox.ScrollIntoView(AnnotationListItems[i]);
  898. break;
  899. }
  900. }
  901. }
  902. public async void ScrollToAnnot(int pageindex, int annotindex, ListBox AnnotationList)
  903. {
  904. //var list = PdfViewer.GetAnnotCommentList(pageindex, PdfViewer.Document);
  905. for (int i = 0; i < AnnotationList.Items.Count; i++)
  906. {
  907. if ((AnnotationList.Items[i] as AnnotationHandlerEventArgs).AnnotIndex == annotindex && (AnnotationList.Items[i] as AnnotationHandlerEventArgs).PageIndex == pageindex)
  908. {
  909. var item = AnnotationList.Items[i] as AnnotationHandlerEventArgs;
  910. if (item.EventType != AnnotArgsType.AnnotLink)
  911. {
  912. //需要手动搜寻在哪一个分组 展开分组头
  913. ExpandGroupHeader(item, AnnotationList);
  914. AnnotationList.SelectedItem = item;
  915. await Task.Delay(1);
  916. AnnotationList.ScrollIntoView(item);
  917. }
  918. }
  919. }
  920. }
  921. /// <summary>
  922. /// listboxitem鼠标左键点击,显示分组的数据
  923. /// </summary>
  924. /// <param name="obj"></param>
  925. private void ListBoxItem_PreviewMouseLeftButtonDown(object obj)
  926. {
  927. if (obj is ListBoxItem item)
  928. {
  929. var data = item.DataContext as AnnotationHandlerEventArgs;
  930. if (data != null)
  931. {
  932. PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex);
  933. }
  934. }
  935. if (obj is AnnotationHandlerEventArgs annotation)
  936. {
  937. PdfViewer.SelectAnnotation(annotation.PageIndex, annotation.AnnotIndex);
  938. }
  939. if (obj is Dictionary<int, List<int>> eventArgs)
  940. {
  941. PdfViewer.SelectAnnotation(eventArgs);
  942. }
  943. }
  944. public async void UpdateAddedAnnot(int pageIndex, int annotIndex)
  945. {
  946. var annots = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  947. for (int i = 0; i < annots.Count; i++)
  948. {
  949. AnnotHandlerEventArgs item = annots[i];
  950. if (item.AnnotIndex == annotIndex && item.EventType != AnnotArgsType.AnnotRedaction)
  951. {
  952. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  953. // 注释列表 不显示链接
  954. if (args.AnnotHandlerEventArgs is LinkAnnotArgs)
  955. {
  956. continue;
  957. }
  958. if (args.AnnotHandlerEventArgs != null)
  959. {
  960. AnnotationListItems.Add(args);
  961. CurrentAnnotationLists.Add(args);
  962. await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  963. //添加 时 ,形状、图章、签名,添加后选中
  964. if (args.EventType == AnnotArgsType.AnnotSquare ||
  965. args.EventType == AnnotArgsType.AnnotCircle ||
  966. args.EventType == AnnotArgsType.AnnotLine ||
  967. args.EventType == AnnotArgsType.AnnotStamp
  968. /* ||args.EventType == AnnotArgsType.AnnotLink*/)
  969. {
  970. listBox.SelectedItem = args;
  971. }
  972. ExpandGroupHeader(args, listBox);
  973. // 关闭后选中关闭前的位置
  974. if (ViewContentViewModel.OpenBOTA) {
  975. await Task.Delay(5);//不加延时 会有不滚动的现象
  976. //events.GetEvent<ScrollIntoAnnotViewEvent>().Publish(new ScrollIntoAnnotViewArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, Annotation = item });
  977. listBox.ScrollIntoView(args);
  978. }
  979. //if (!listBox.SelectedItems.Contains(args))
  980. //{
  981. // listBox.SelectedItem = args;
  982. // //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  983. //}
  984. }
  985. }
  986. }
  987. listBox.UpdateLayout();
  988. //SetGroupHeader(listBox);
  989. if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
  990. {
  991. IsEmptyPanelVisibility = Visibility.Collapsed;
  992. }
  993. }
  994. public void DeleteModifiedAnnot(int pageIndex, int annotIndex)
  995. {
  996. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  997. //if (items.Count == 0)
  998. //{
  999. // for (int k = 0; k < AnnotationListItems.Count; k++)
  1000. // {
  1001. // if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  1002. // {
  1003. // AnnotationListItems.RemoveAt(k);
  1004. // AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  1005. // CurrentAnnotationLists.Remove(annotation1);
  1006. // UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  1007. // return;
  1008. // }
  1009. // }
  1010. //}
  1011. //else
  1012. //{
  1013. for (int k = 0; k < AnnotationListItems.Count; k++)
  1014. {
  1015. //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
  1016. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  1017. {
  1018. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  1019. CurrentAnnotationLists.Remove(annotation1);
  1020. AnnotationListItems.RemoveAt(k);
  1021. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  1022. return;
  1023. }
  1024. }
  1025. //}
  1026. }
  1027. public async void UpdateModifiedAnnot(int pageIndex, int annotIndex/*, bool isDelete*/)
  1028. {
  1029. try
  1030. {
  1031. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  1032. #region 删除
  1033. //if (isDelete && items.Count == 0)//当前页的最后一次删除
  1034. //{
  1035. // for (int k = 0; k < AnnotationListItems.Count; k++)
  1036. // {
  1037. // if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  1038. // {
  1039. // AnnotationListItems.RemoveAt(k);
  1040. // AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  1041. // CurrentAnnotationLists.Remove(annotation1);
  1042. // UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  1043. // return;
  1044. // }
  1045. // }
  1046. //}
  1047. //if (isDelete)//删除
  1048. //{
  1049. // for (int k = 0; k < AnnotationListItems.Count; k++)
  1050. // {
  1051. // //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
  1052. // if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  1053. // {
  1054. // AnnotationListItems.RemoveAt(k);
  1055. // AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  1056. // CurrentAnnotationLists.Remove(annotation1);
  1057. // UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  1058. // return;
  1059. // }
  1060. // }
  1061. //}
  1062. #endregion 删除
  1063. for (int j = 0; j < items.Count; j++)
  1064. {
  1065. for (int k = 0; k < AnnotationListItems.Count; k++)
  1066. {
  1067. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex && items[j].AnnotIndex == annotIndex)
  1068. {
  1069. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
  1070. AnnotationListItems[k] = GetAddAnnotEventArgs(items[j]);
  1071. for (int i = 0; i < CurrentAnnotationLists.Count; i++)
  1072. {
  1073. if (CurrentAnnotationLists[i].PageIndex == pageIndex && CurrentAnnotationLists[i].AnnotIndex == annotIndex)
  1074. {
  1075. CurrentAnnotationLists[i] = GetAddAnnotEventArgs(items[j]);
  1076. break;
  1077. }
  1078. }
  1079. await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  1080. if (ViewContentViewModel.IsMultiSelected == false)
  1081. {
  1082. if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
  1083. {
  1084. listBox.SelectedItem = AnnotationListItems[k];
  1085. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  1086. }
  1087. }
  1088. ExpandGroupHeader(AnnotationListItems[k], listBox);
  1089. await Task.Delay(5);//不加延时 会有不滚动的现象
  1090. listBox.ScrollIntoView(AnnotationListItems[k]);
  1091. }
  1092. }
  1093. }
  1094. listBox.UpdateLayout();
  1095. //SetGroupHeader(listBox);
  1096. }
  1097. catch { }
  1098. }
  1099. /// <summary>
  1100. /// 展开item所在分组
  1101. /// </summary>
  1102. private void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
  1103. {
  1104. try
  1105. {
  1106. var groups = listBox.Items.Groups;
  1107. if (groups != null && groups.Count > 0)
  1108. {
  1109. for (int i = 0; i < groups.Count; i++)
  1110. {
  1111. var l = groups[i] as CollectionViewGroup;
  1112. if (l.Items.Contains(item))
  1113. {
  1114. var scroller = GetScrollHost(listBox);
  1115. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  1116. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  1117. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  1118. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  1119. if (g != null)
  1120. g.IsExpanded = true;
  1121. }
  1122. }
  1123. }
  1124. }
  1125. catch { }
  1126. }
  1127. /// <summary>
  1128. /// 页面加载时
  1129. /// </summary>
  1130. /// <param name="obj"></param>
  1131. private void Loaded(object obj)
  1132. {
  1133. if (obj is CompositeCommandParameter composite)
  1134. {
  1135. if (composite.Parameter is ListBox listBox)
  1136. {
  1137. this.listBox = listBox;
  1138. //RefreshAnnotationListItems();
  1139. SetGroupHeader(listBox);
  1140. }
  1141. }
  1142. }
  1143. public bool IsNavigationTarget(NavigationContext navigationContext)
  1144. {
  1145. return true;
  1146. }
  1147. public void OnNavigatedFrom(NavigationContext navigationContext)
  1148. {
  1149. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1150. }
  1151. private ObservableCollection<AnnotationHandlerEventArgs> HideAnnots;
  1152. public void OnNavigatedTo(NavigationContext navigationContext)
  1153. {
  1154. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  1155. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  1156. if (pdfview != null && viewContentViewModel != null)
  1157. {
  1158. ViewContentViewModel = viewContentViewModel;
  1159. PdfViewer = pdfview;
  1160. }
  1161. if (IsFirst)
  1162. {
  1163. IsFirst = false;
  1164. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  1165. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  1166. AnnotationListItems.AddRange(CurrentAnnotationLists);
  1167. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  1168. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  1169. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  1170. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  1171. //按照PageIndex,分组 排序
  1172. SetGroupHeader(listBox);
  1173. Screen = Visibility.Collapsed;
  1174. colors.Clear();
  1175. authors.Clear();
  1176. annotArgsTypes.Clear();
  1177. }
  1178. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1179. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1180. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  1181. }
  1182. private void RefreshData(RefreshAnnotArgs obj)
  1183. {
  1184. if (obj.Annotation != null)
  1185. {
  1186. RefreshOneListItem(obj.Annotation.AnnotHandlerEventArgs);
  1187. }
  1188. }
  1189. private void ShortCut_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  1190. {
  1191. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  1192. {
  1193. if (listBox != null)
  1194. {
  1195. if (listBox.SelectedItems == null)
  1196. {
  1197. return;
  1198. }
  1199. listBox.SelectedItems.Clear();
  1200. }
  1201. }
  1202. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Delete))
  1203. {
  1204. if (listBox != null)
  1205. {
  1206. if (listBox.SelectedItems == null)
  1207. {
  1208. return;
  1209. }
  1210. List<int> pagelist = new List<int>();
  1211. for (int i = 0; i < listBox.SelectedItems.Count; i++)
  1212. {
  1213. AnnotationHandlerEventArgs annotation = listBox.SelectedItems[i] as AnnotationHandlerEventArgs;
  1214. pagelist.Add(listBox.Items.IndexOf(annotation));
  1215. }
  1216. pagelist.Sort();
  1217. for (int i = 0; i < pagelist.Count; i++)
  1218. {
  1219. AnnotationHandlerEventArgs annotation = listBox.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  1220. if (annotation == null)
  1221. {
  1222. continue;
  1223. }
  1224. DeleteCommand.Execute(annotation);
  1225. }
  1226. }
  1227. }
  1228. }
  1229. /// <summary>
  1230. /// 默认按照PageIndex,分组 排序
  1231. /// </summary>
  1232. /// <param name="listBox"></param>
  1233. private async void SetGroupHeader(ListBox listBox)
  1234. {
  1235. if (listBox == null) { return; }
  1236. #region BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  1237. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  1238. //CurrentAnnotationLists.AddRange(AnnotationListItems);
  1239. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1240. //ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  1241. //iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  1242. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  1243. ////iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  1244. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.Y), ListSortDirection.Ascending));
  1245. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.X), ListSortDirection.Ascending));
  1246. #endregion BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  1247. if (AnnotationListItems == null)
  1248. {
  1249. return;
  1250. }
  1251. if (AnnotationListItems.Count > 0)
  1252. {
  1253. IsEmptyPanelVisibility = Visibility.Collapsed;
  1254. await Task.Delay(5);
  1255. //展开数据
  1256. ExpandGroupHeader(AnnotationListItems, listBox);
  1257. }
  1258. else
  1259. {
  1260. if (CurrentAnnotationLists.Count <= 0 && HideAnnots.Count > 1)
  1261. {
  1262. IsEmptyPanelVisibility = Visibility.Visible;
  1263. IsHideAnnotPanelVisibility = Visibility.Visible;
  1264. }
  1265. else
  1266. {
  1267. IsEmptyPanelVisibility = Visibility.Visible;
  1268. IsHideAnnotPanelVisibility = Visibility.Visible;
  1269. }
  1270. }
  1271. }
  1272. /// <summary>
  1273. /// 展开列表项
  1274. /// </summary>
  1275. /// <param name="annotationListItems"></param>
  1276. /// <param name="listBox"></param>
  1277. private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  1278. {
  1279. try
  1280. {
  1281. foreach (var item in annotationListItems)
  1282. {
  1283. var groups = listBox.Items.Groups;
  1284. for (int i = 0; i < groups.Count; i++)
  1285. {
  1286. var group = groups[i] as CollectionViewGroup;
  1287. if (group.Items.Contains(item))
  1288. {
  1289. var scroller = GetScrollHost(listBox);
  1290. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  1291. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  1292. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  1293. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  1294. if (g != null)
  1295. {
  1296. g.IsExpanded = true;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. }
  1302. catch { }
  1303. }
  1304. private ScrollViewer GetScrollHost(ListBox listBox)
  1305. {
  1306. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  1307. {
  1308. int s = VisualTreeHelper.GetChildrenCount(listBox);
  1309. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  1310. if (border != null)
  1311. {
  1312. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  1313. }
  1314. }
  1315. return null;
  1316. }
  1317. /// <summary>
  1318. /// 获取listbox绑定数据
  1319. /// </summary>
  1320. /// <returns></returns>
  1321. public ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList(out ObservableCollection<AnnotationHandlerEventArgs> HideAnnots)
  1322. {
  1323. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  1324. HideAnnots = new ObservableCollection<AnnotationHandlerEventArgs>();
  1325. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  1326. {
  1327. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  1328. foreach (var item in items)
  1329. {
  1330. //原型图上,目前对波浪线的类型,在注释列表不显示
  1331. //注释列表 不显示链接注释
  1332. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly
  1333. && item.EventType != AnnotArgsType.AnnotLink)
  1334. {
  1335. if (item.EventType == AnnotArgsType.AnnotLink)
  1336. {
  1337. if (item is LinkAnnotArgs link)
  1338. {
  1339. if (link.LinkType == LINK_TYPE.URI)
  1340. {
  1341. continue;
  1342. }
  1343. }
  1344. }
  1345. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  1346. bool isVisible = PdfViewer.CheckAnnotVisible(args.PageIndex, args.AnnotIndex);
  1347. if (args.AnnotHandlerEventArgs != null)
  1348. {
  1349. if (isVisible)
  1350. {
  1351. list.Add(args);
  1352. }
  1353. else
  1354. {
  1355. HideAnnots.Add(args);
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }
  1361. return list;
  1362. }
  1363. public AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
  1364. {
  1365. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  1366. if (item.EventType == AnnotArgsType.AnnotFreehand)
  1367. {
  1368. BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  1369. if (bitmap != null)
  1370. args.WriteableBitmap = bitmap;
  1371. }
  1372. args.ClientRect = item.ClientRect;
  1373. args.AnnotHandlerEventArgs = item;
  1374. args.PageIndex = item.PageIndex;
  1375. args.AnnotIndex = item.AnnotIndex;
  1376. args.EventType = item.EventType;
  1377. args.CreateTime = item.CreateTime;
  1378. args.UpdateTime = item.UpdateTime;
  1379. args.Content = item.Content;
  1380. args.MarkupContent = item.MarkupContent;
  1381. args.Author = item.Author;
  1382. args.Locked = item.Locked;
  1383. args.ReadOnly = item.ReadOnly;
  1384. args.FormField = item.FormField;
  1385. args.Document = PdfViewer.Document;
  1386. return args;
  1387. }
  1388. /// <summary>
  1389. /// 获取手绘图案
  1390. /// </summary>
  1391. /// <param name="doc"></param>
  1392. /// <param name="pageIndex"></param>
  1393. /// <param name="annotIndex"></param>
  1394. /// <returns></returns>
  1395. public BitmapImage GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  1396. {
  1397. if (doc == null)
  1398. {
  1399. return null;
  1400. }
  1401. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  1402. if (docPage == null)
  1403. {
  1404. return null;
  1405. }
  1406. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  1407. foreach (CPDFAnnotation annot in docAnnots)
  1408. {
  1409. if (docAnnots.IndexOf(annot) == annotIndex)
  1410. {
  1411. CPDFInkAnnotation cPDFInk = (CPDFInkAnnotation)docAnnots[annotIndex];
  1412. //CRect rawRect = annot.GetRect();
  1413. CRect rawRect = cPDFInk.Rect;
  1414. double scaleDpi = 96.0 / 72.0;
  1415. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  1416. int drawWidth = (int)paintRect.Width;
  1417. int drawHeight = (int)paintRect.Height;
  1418. switch (docPage.Rotation)
  1419. {
  1420. case 0:
  1421. case 2:
  1422. drawWidth = (int)paintRect.Width;
  1423. drawHeight = (int)paintRect.Height;
  1424. break;
  1425. case 1:
  1426. case 3:
  1427. drawWidth = (int)paintRect.Height;
  1428. drawHeight = (int)paintRect.Width;
  1429. break;
  1430. }
  1431. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  1432. annot.UpdateAp();
  1433. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  1434. BitmapImage bitmapImage = null;
  1435. if (drawWidth != 0 && drawHeight != 0)
  1436. {
  1437. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  1438. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  1439. bitmapImage = ConvertWriteableBitmapToBitmapImage(wirteBitmap, docPage.Rotation);
  1440. }
  1441. else
  1442. {
  1443. bitmapImage = null;
  1444. }
  1445. cPDFInk.SetRect(rawRect);
  1446. // 测试
  1447. //using (FileStream stream = new FileStream(@"C:\Users\oyxh\Desktop\images\1.png", FileMode.Create))
  1448. //{
  1449. // PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
  1450. // bitmapEncoder.Frames.Add(BitmapFrame.Create(wirteBitmap));
  1451. // bitmapEncoder.Save(stream);
  1452. //}
  1453. return bitmapImage;
  1454. }
  1455. }
  1456. return null;
  1457. }
  1458. public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm, int rotation)
  1459. {
  1460. BitmapImage bmImage = new BitmapImage();
  1461. using (MemoryStream stream = new MemoryStream())
  1462. {
  1463. PngBitmapEncoder encoder = new PngBitmapEncoder();
  1464. encoder.Frames.Add(BitmapFrame.Create(wbm));
  1465. encoder.Save(stream);
  1466. bmImage.BeginInit();
  1467. bmImage.CacheOption = BitmapCacheOption.OnLoad;
  1468. bmImage.StreamSource = stream;
  1469. switch (rotation)
  1470. {
  1471. case 0:
  1472. bmImage.Rotation = Rotation.Rotate0;
  1473. break;
  1474. case 1:
  1475. bmImage.Rotation = Rotation.Rotate90;
  1476. break;
  1477. case 2:
  1478. bmImage.Rotation = Rotation.Rotate180;
  1479. break;
  1480. case 3:
  1481. bmImage.Rotation = Rotation.Rotate270;
  1482. break;
  1483. }
  1484. bmImage.EndInit();
  1485. bmImage.Freeze();
  1486. }
  1487. return bmImage;
  1488. }
  1489. /// <summary>
  1490. /// listbox的itemsource发生变化时
  1491. /// </summary>
  1492. /// <param name="sender"></param>
  1493. /// <param name="e"></param>
  1494. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  1495. {
  1496. if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
  1497. {
  1498. //currentAnnotationArgs.Clear();
  1499. //currentAnnotationArgs.AddRange(obsSender);
  1500. if (obsSender.Count < 1)
  1501. {
  1502. if (CurrentAnnotationLists.Count <= 0)
  1503. {
  1504. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1505. IsEmptyPanelVisibility = Visibility.Visible;
  1506. }
  1507. else
  1508. {
  1509. if (isHiddenAnnot)
  1510. {
  1511. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1512. IsEmptyPanelVisibility = Visibility.Visible;
  1513. }
  1514. else
  1515. {
  1516. IsEmptyPanelVisibility = Visibility.Visible;
  1517. IsHideAnnotPanelVisibility = Visibility.Visible;
  1518. }
  1519. }
  1520. //IsEmptyPanelVisibility = Visibility.Visible;
  1521. }
  1522. else
  1523. {
  1524. IsEmptyPanelVisibility = Visibility.Collapsed;
  1525. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1526. }
  1527. }
  1528. else
  1529. {
  1530. IsEmptyPanelVisibility = Visibility.Visible;
  1531. }
  1532. }
  1533. }
  1534. }