AnnotationContentViewModel.cs 68 KB

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