AnnotationContentViewModel.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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.Excel;
  11. using Microsoft.Office.Interop.PowerPoint;
  12. using Microsoft.Office.Interop.Word;
  13. using PDF_Office.CustomControl;
  14. using PDF_Office.DataConvert;
  15. using PDF_Office.Helper;
  16. using PDF_Office.Model;
  17. using PDF_Office.Model.BOTA;
  18. using PDF_Office.Views.BOTA;
  19. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  20. using Prism.Commands;
  21. using Prism.Mvvm;
  22. using Prism.Regions;
  23. using Prism.Services.Dialogs;
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Collections.ObjectModel;
  27. using System.ComponentModel;
  28. using System.Configuration;
  29. using System.Drawing;
  30. using System.Linq;
  31. using System.Text;
  32. using System.Threading.Tasks;
  33. using System.Windows;
  34. using System.Windows.Annotations;
  35. using System.Windows.Annotations.Storage;
  36. using System.Windows.Controls;
  37. using System.Windows.Data;
  38. using System.Windows.Documents;
  39. using System.Windows.Forms;
  40. using System.Windows.Markup;
  41. using System.Windows.Media;
  42. using System.Windows.Media.Imaging;
  43. using System.Windows.Shapes;
  44. using static Dropbox.Api.Files.SearchMatchType;
  45. using static Dropbox.Api.TeamLog.SpaceCapsType;
  46. using static System.Net.Mime.MediaTypeNames;
  47. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  48. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
  49. using Border = System.Windows.Controls.Border;
  50. using ListBox = System.Windows.Controls.ListBox;
  51. using Task = System.Threading.Tasks.Task;
  52. using TextBox = System.Windows.Controls.TextBox;
  53. using Winform = System.Windows.Forms;
  54. namespace PDF_Office.ViewModels.BOTA
  55. {
  56. public class AnnotationContentViewModel : BindableBase, INavigationAware
  57. {
  58. private ListBox listBox;
  59. private IRegionManager region;
  60. private IDialogService dialogs;
  61. private Visibility isEmptyPanelVisibility = Visibility.Visible;
  62. public ViewContentViewModel ViewContentViewModel { get; set; }
  63. public CPDFViewer PdfViewer { get; set; }
  64. public Visibility IsEmptyPanelVisibility
  65. {
  66. get { return isEmptyPanelVisibility; }
  67. set
  68. {
  69. SetProperty(ref isEmptyPanelVisibility, value);
  70. }
  71. }
  72. private ObservableCollection<AnnotationHandlerEventArgs> currentAnnotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
  73. public ObservableCollection<AnnotationHandlerEventArgs> CurrentAnnotationArgs { get => currentAnnotationArgs; set => currentAnnotationArgs = value; }
  74. private AnnotationSortOrder annotationSortOrder = AnnotationSortOrder.PageIndexAscending;
  75. public AnnotationSortOrder AnnotationSortOrder
  76. {
  77. get { return annotationSortOrder; }
  78. set { annotationSortOrder = value; }
  79. }
  80. private ObservableCollection<AnnotationHandlerEventArgs> annotationListItems;
  81. public ObservableCollection<AnnotationHandlerEventArgs> AnnotationListItems
  82. {
  83. get { return annotationListItems; }
  84. set
  85. {
  86. SetProperty(ref annotationListItems, value);
  87. }
  88. }
  89. private List<string> colors = new List<string>();
  90. private List<string> authors = new List<string>();
  91. private List<AnnotArgsType> annotArgsTypes = new List<AnnotArgsType>();
  92. public DelegateCommand<object> LoadedCommand { get; set; }
  93. public DelegateCommand<object> ListBoxItemPreviewMouseLeftButtonDown { get; set; }
  94. public DelegateCommand<object> AddNotesCommand { get; set; }
  95. public DelegateCommand<object> ScreenCommand { get; set; }
  96. public DelegateCommand<object> ExportCommentsCommand { get; set; }
  97. public DelegateCommand<object> ImportCommentsCommand { get; set; }
  98. public DelegateCommand<object> DeleteCommand { get; set; }
  99. public DelegateCommand DeleteAllCommand { get; set; }
  100. public DelegateCommand PageSortCommand { get; set; }
  101. public DelegateCommand TimeAscendingCommand { get; set; }
  102. public DelegateCommand TimeDescendingCommand { get; set; }
  103. public AnnotationContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  104. {
  105. region = regionManager;
  106. dialogs = dialogService;
  107. LoadedCommand = new DelegateCommand<object>(Loaded);
  108. ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand<object>(ListBoxItem_PreviewMouseLeftButtonDown);
  109. AddNotesCommand = new DelegateCommand<object>(AddNotesEvent);
  110. ScreenCommand = new DelegateCommand<object>(ScreenEvent);
  111. ExportCommentsCommand = new DelegateCommand<object>(ExportCommentsEvent);
  112. ImportCommentsCommand = new DelegateCommand<object>(ImportCommentsEvent);
  113. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  114. DeleteAllCommand = new DelegateCommand(DeleteAllEvent);
  115. PageSortCommand = new DelegateCommand(PageSortEvent);
  116. TimeAscendingCommand = new DelegateCommand(TimeAscendingEvent);
  117. TimeDescendingCommand = new DelegateCommand(TimeDescendingEvent);
  118. }
  119. /// <summary>
  120. /// 导入注释
  121. /// </summary>
  122. /// <param name="obj"></param>
  123. private async void ImportCommentsEvent(object obj)
  124. {
  125. Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  126. dlg.Filter = "PDF|*.xfdf";
  127. dlg.DefaultExt = ".xfdf";
  128. if (dlg.ShowDialog() == true)
  129. {
  130. string fileName = dlg.FileName;
  131. await Task.Delay(5);
  132. var result = PdfViewer.ImportAnnotationFromXFDFPath(fileName);
  133. if (result == false)
  134. {
  135. //MessageBoxEx.Show("导入失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
  136. AlertsMessage alertsMessage = new AlertsMessage();
  137. alertsMessage.Show("提示", "导入失败!", "OK");
  138. return;
  139. }
  140. PdfViewer.ReloadVisibleAnnots();
  141. //提取出来的注释文件 时间为空 则显示未系统当前时间
  142. CurrentAnnotationArgs = GetDocumentAnnotionList();
  143. AnnotationListItems.Clear();
  144. AnnotationListItems.AddRange(CurrentAnnotationArgs);
  145. await Task.Delay(5);
  146. RefreshAnnotationListItems(null);
  147. PdfViewer.UndoManager.CanSave = true;
  148. }
  149. }
  150. /// <summary>
  151. /// 时间倒序
  152. /// </summary>
  153. private void TimeDescendingEvent()
  154. {
  155. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
  156. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  157. v.GroupDescriptions.Clear();
  158. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  159. v.SortDescriptions.Clear();
  160. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Descending));
  161. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Descending));
  162. AnnotationSortOrder = AnnotationSortOrder.TimeDescending;
  163. }
  164. /// <summary>
  165. /// 时间正序
  166. /// </summary>
  167. private void TimeAscendingEvent()
  168. {
  169. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
  170. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  171. v.GroupDescriptions.Clear();
  172. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.CreateTime), createTimeToDate));
  173. v.SortDescriptions.Clear();
  174. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.CreateTime), ListSortDirection.Ascending));
  175. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  176. AnnotationSortOrder = AnnotationSortOrder.TimeAscending;
  177. }
  178. /// <summary>
  179. /// 页面排序
  180. /// </summary>
  181. private void PageSortEvent()
  182. {
  183. ICollectionView v = CollectionViewSource.GetDefaultView(AnnotationListItems);
  184. v.GroupDescriptions.Clear();
  185. v.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotHandlerEventArgs.PageIndex)));
  186. v.SortDescriptions.Clear();
  187. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  188. v.SortDescriptions.Add(new SortDescription(nameof(AnnotHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  189. AnnotationSortOrder = AnnotationSortOrder.PageIndexAscending;
  190. }
  191. /// <summary>
  192. /// 删除所有注释
  193. /// </summary>
  194. private void DeleteAllEvent()
  195. {
  196. //调用集中删除的接口 方便一次性undo
  197. Dictionary<int, List<int>> deleteLists = new Dictionary<int, List<int>>();
  198. for (int i = 0; i < AnnotationListItems.Count; i++)
  199. {
  200. AnnotationHandlerEventArgs item = AnnotationListItems[i] as AnnotationHandlerEventArgs;
  201. if (!deleteLists.ContainsKey(item.PageIndex))
  202. {
  203. deleteLists.Add(item.PageIndex, new List<int>() { item.AnnotIndex });
  204. }
  205. else
  206. {
  207. var pagelist = deleteLists[item.PageIndex];
  208. pagelist.Add(item.AnnotIndex);
  209. }
  210. }
  211. PdfViewer.RemovePageAnnot(deleteLists);
  212. AnnotationListItems.Clear();
  213. CurrentAnnotationArgs.Clear();
  214. PdfViewer.UndoManager.CanSave = true;
  215. }
  216. /// <summary>
  217. /// 删除注释,单个/多个
  218. /// </summary>
  219. /// <param name="obj"></param>
  220. private void DelegateEvent(object obj)
  221. {
  222. if (obj is AnnotationHandlerEventArgs annotation)
  223. {
  224. if (annotation != null)
  225. {
  226. var result = PdfViewer.RemovePageAnnot(annotation.PageIndex, annotation.AnnotIndex);
  227. if (result)
  228. {
  229. AnnotationListItems.Remove(annotation);
  230. var annotation1 = CurrentAnnotationArgs.FirstOrDefault(x => x.PageIndex == annotation.PageIndex && x.AnnotIndex == annotation.AnnotIndex);
  231. CurrentAnnotationArgs.Remove(annotation1);
  232. //记录是删除了哪些页面的注释,然后更新对应页面的注释即可
  233. UpdateAnnotListAfterDelete(annotation.PageIndex, annotation.AnnotIndex);
  234. PdfViewer.UndoManager.CanSave = true;
  235. }
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// 删除之后,更新
  241. /// </summary>
  242. /// <param name="pageIndex"></param>
  243. /// <param name="annoteIndex"></param>
  244. private void UpdateAnnotListAfterDelete(int pageIndex, int annoteIndex)
  245. {
  246. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  247. for (int j = 0; j < items.Count; j++)//用修改赋值的方式 可以解决删除后表头折叠的问题
  248. {
  249. if (items[j].AnnotIndex >= annoteIndex)//只需要更新比删除元素索引大的注释
  250. {
  251. for (int k = 0; k < AnnotationListItems.Count; k++)
  252. {
  253. //相当于将后面的索引-1
  254. if (AnnotationListItems[k].PageIndex == pageIndex &&
  255. AnnotationListItems[k].AnnotIndex == (items[j].AnnotIndex + 1) &&
  256. string.Equals(AnnotationListItems[k].MarkupContent, items[j].MarkupContent) &&
  257. string.Equals(AnnotationListItems[k].Content, items[j].Content) &&
  258. string.Equals(AnnotationListItems[k].CreateTime, items[j].CreateTime) &&
  259. string.Equals(AnnotationListItems[k].Author, items[j].Author)
  260. )
  261. {
  262. AnnotationListItems[k].AnnotHandlerEventArgs = items[j];
  263. AnnotationListItems[k].PageIndex = items[j].PageIndex;
  264. AnnotationListItems[k].AnnotIndex = items[j].AnnotIndex;
  265. AnnotationListItems[k].EventType = items[j].EventType;
  266. AnnotationListItems[k].CreateTime = items[j].CreateTime;
  267. AnnotationListItems[k].UpdateTime = items[j].UpdateTime;
  268. if (items[j].EventType == AnnotArgsType.AnnotFreehand)
  269. {
  270. WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, items[j].PageIndex, items[j].AnnotIndex);
  271. AnnotationListItems[k].WriteableBitmap = bitmap;
  272. }
  273. AnnotationListItems[k].Content = items[j].Content;
  274. AnnotationListItems[k].MarkupContent = items[j].MarkupContent;
  275. AnnotationListItems[k].Author = items[j].Author;
  276. AnnotationListItems[k].Locked = items[j].Locked;
  277. AnnotationListItems[k].ReadOnly = items[j].ReadOnly;
  278. AnnotationListItems[k].FormField = items[j].FormField;
  279. AnnotationListItems[k].Document = PdfViewer.Document;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. /// <summary>
  286. /// 导出注释
  287. /// </summary>
  288. /// <param name="obj"></param>
  289. private void ExportCommentsEvent(object obj)
  290. {
  291. if (ViewContentViewModel.CanSave)
  292. {
  293. ViewContentViewModel.SaveFile.Execute();
  294. }
  295. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  296. dlg.Filter = "PDF|*.xfdf";
  297. dlg.DefaultExt = ".xfdf";
  298. dlg.FileName = PdfViewer.Document.FileName;
  299. if (dlg.ShowDialog() == true)
  300. {
  301. string fileName = dlg.FileName;
  302. var result = PdfViewer.ExportAnnotationToXFDFPath(fileName);
  303. if (result)
  304. {
  305. //MessageBoxEx.Show("导出成功", "", Winform.MessageBoxButtons.OK);
  306. AlertsMessage alertsMessage = new AlertsMessage();
  307. alertsMessage.Show("提示", "导出成功!", "OK");
  308. }
  309. else
  310. {
  311. AlertsMessage alertsMessage = new AlertsMessage();
  312. alertsMessage.Show("提示", "导出失败!", "OK");
  313. //MessageBoxEx.Show("导出失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
  314. }
  315. }
  316. }
  317. /// <summary>
  318. /// 筛选注释
  319. /// </summary>
  320. /// <param name="obj"></param>
  321. private void ScreenEvent(object obj)
  322. {
  323. if (obj is System.Windows.Controls.Button button)
  324. {
  325. DialogParameters value = new DialogParameters();
  326. value.Add(ParameterNames.AnnotationList, CurrentAnnotationArgs);
  327. value.Add(ParameterNames.AnnotArgsTypes, annotArgsTypes);
  328. value.Add(ParameterNames.AnnotationColors, colors);
  329. value.Add(ParameterNames.AnnotationAuthor, authors);
  330. dialogs.ShowDialog(DialogNames.ScreenAnnotationDialog, value, e =>
  331. {
  332. if (e.Result == ButtonResult.OK && e.Parameters != null)
  333. {
  334. bool cleanState = false;
  335. if (e.Parameters.ContainsKey(ParameterNames.AnnotationCleanState))
  336. {
  337. cleanState = e.Parameters.GetValue<bool>(ParameterNames.AnnotationCleanState);
  338. if (cleanState)
  339. {
  340. colors.Clear();
  341. authors.Clear();
  342. annotArgsTypes.Clear();
  343. AnnotationListItems.Clear();
  344. AnnotationListItems.AddRange(CurrentAnnotationArgs);
  345. RefreshAnnotationListItems(button);
  346. }
  347. else
  348. {
  349. if (e.Parameters.ContainsKey(ParameterNames.AnnotArgsTypes))
  350. {
  351. annotArgsTypes = e.Parameters.GetValue<List<AnnotArgsType>>(ParameterNames.AnnotArgsTypes);
  352. }
  353. if (e.Parameters.ContainsKey(ParameterNames.AnnotationColors))
  354. {
  355. colors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationColors);
  356. }
  357. if (e.Parameters.ContainsKey(ParameterNames.AnnotationAuthor))
  358. {
  359. authors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationAuthor);
  360. }
  361. ObservableCollection<AnnotationHandlerEventArgs> screenAnnotationArgs = SelectAnnotationListItems(annotArgsTypes, colors, authors);
  362. AnnotationListItems.Clear();
  363. AnnotationListItems.AddRange(screenAnnotationArgs);
  364. RefreshAnnotationListItems(button);
  365. }
  366. }
  367. }
  368. else if (e.Result == ButtonResult.Cancel && e.Parameters != null)
  369. {
  370. colors.Clear();
  371. authors.Clear();
  372. annotArgsTypes.Clear();
  373. AnnotationListItems.Clear();
  374. AnnotationListItems.AddRange(CurrentAnnotationArgs);
  375. RefreshAnnotationListItems(button);
  376. }
  377. });
  378. }
  379. }
  380. /// <summary>
  381. /// 刷新列表数据
  382. /// </summary>
  383. /// <param name="button"></param>
  384. private async void RefreshAnnotationListItems(System.Windows.Controls.Button button)
  385. {
  386. switch (AnnotationSortOrder)
  387. {
  388. case AnnotationSortOrder.TimeAscending:
  389. TimeAscendingEvent();
  390. break;
  391. case AnnotationSortOrder.TimeDescending:
  392. TimeDescendingEvent();
  393. break;
  394. case AnnotationSortOrder.PageIndexAscending:
  395. PageSortEvent();
  396. break;
  397. default:
  398. break;
  399. }
  400. if (AnnotationListItems.Count > 0)
  401. {
  402. IsEmptyPanelVisibility = Visibility.Collapsed;
  403. await Task.Delay(2);
  404. //展开数据
  405. ExpandGroupHeader(AnnotationListItems, listBox);
  406. }
  407. else
  408. {
  409. if (button != null)
  410. {
  411. button.IsEnabled = true;
  412. }
  413. IsEmptyPanelVisibility = Visibility.Visible;
  414. //AnnotationListItems.AddRange(currentAnnotationArgs);
  415. //RefreshAnnotationListItems();
  416. }
  417. }
  418. /// <summary>
  419. /// 根据条件筛选列表数据
  420. /// </summary>
  421. /// <param name="annotArgsTypes"></param>
  422. /// <param name="colors"></param>
  423. /// <param name="authors"></param>
  424. /// <returns></returns>
  425. private ObservableCollection<AnnotationHandlerEventArgs> SelectAnnotationListItems(List<AnnotArgsType> annotArgsTypes, List<string> colors, List<string> authors)
  426. {
  427. ObservableCollection<AnnotationHandlerEventArgs> annotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
  428. #region 类型 颜色 作者
  429. if (annotArgsTypes.Count > 0 && colors.Count > 0 && authors.Count > 0)
  430. {
  431. foreach (var item in CurrentAnnotationArgs)
  432. {
  433. foreach (var type in annotArgsTypes)
  434. {
  435. foreach (var color in colors)
  436. {
  437. foreach (var author in authors)
  438. {
  439. if (item.EventType == type && item.Author == author)
  440. {
  441. SelectAnnotationColors(item, color, annotationArgs);
  442. }
  443. }
  444. }
  445. }
  446. }
  447. }
  448. #endregion 类型 颜色 作者
  449. #region 颜色 作者
  450. else if (colors.Count > 0 && authors.Count > 0)
  451. {
  452. foreach (var item in CurrentAnnotationArgs)
  453. {
  454. foreach (var color in colors)
  455. {
  456. foreach (var author in authors)
  457. {
  458. if (item.Author == author)
  459. {
  460. SelectAnnotationColors(item, color, annotationArgs);
  461. }
  462. }
  463. }
  464. }
  465. }
  466. #endregion 颜色 作者
  467. #region 类型 作者
  468. else if (annotArgsTypes.Count > 0 && authors.Count > 0)
  469. {
  470. foreach (var item in CurrentAnnotationArgs)
  471. {
  472. foreach (var type in annotArgsTypes)
  473. {
  474. foreach (var author in authors)
  475. {
  476. if (item.EventType == type && item.Author == author)
  477. {
  478. annotationArgs.Add(item);
  479. }
  480. }
  481. }
  482. }
  483. }
  484. #endregion 类型 作者
  485. #region 类型 颜色
  486. else if (annotArgsTypes.Count > 0 && colors.Count > 0)
  487. {
  488. foreach (var item in CurrentAnnotationArgs)
  489. {
  490. foreach (var type in annotArgsTypes)
  491. {
  492. foreach (var color in colors)
  493. {
  494. if (item.EventType == type)
  495. {
  496. SelectAnnotationColors(item, color, annotationArgs);
  497. }
  498. }
  499. }
  500. }
  501. }
  502. #endregion 类型 颜色
  503. #region 类型
  504. else if (annotArgsTypes.Count > 0)
  505. {
  506. foreach (var item in CurrentAnnotationArgs)
  507. {
  508. foreach (var type in annotArgsTypes)
  509. {
  510. if (item.EventType == type)
  511. {
  512. annotationArgs.Add(item);
  513. }
  514. }
  515. }
  516. }
  517. #endregion 类型
  518. #region 颜色
  519. else if (colors.Count > 0)
  520. {
  521. foreach (var item in CurrentAnnotationArgs)
  522. {
  523. foreach (var color in colors)
  524. {
  525. SelectAnnotationColors(item, color, annotationArgs);
  526. }
  527. }
  528. }
  529. #endregion 颜色
  530. #region 作者
  531. else if (authors.Count > 0)
  532. {
  533. foreach (var item in CurrentAnnotationArgs)
  534. {
  535. foreach (var author in authors)
  536. {
  537. if (item.Author == author)
  538. {
  539. annotationArgs.Add(item);
  540. }
  541. }
  542. }
  543. }
  544. #endregion 作者
  545. return annotationArgs;
  546. }
  547. /// <summary>
  548. /// 根据颜色筛选注释
  549. /// </summary>
  550. /// <param name="item"></param>
  551. /// <param name="color"></param>
  552. /// <param name="annotationArgs"></param>
  553. private void SelectAnnotationColors(AnnotationHandlerEventArgs item, string color, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  554. {
  555. AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs;
  556. switch (item.EventType)
  557. {
  558. case AnnotArgsType.AnnotFreeText://文本
  559. if (data is FreeTextAnnotArgs textAnnotArgs)
  560. {
  561. AddScreenAnnotationArgs(textAnnotArgs.FontColor, color, item, annotationArgs);
  562. }
  563. break;
  564. case AnnotArgsType.AnnotHighlight://高亮
  565. if (data is TextHighlightAnnotArgs highlightAnnotArgs)
  566. {
  567. AddScreenAnnotationArgs(highlightAnnotArgs.Color, color, item, annotationArgs);
  568. }
  569. break;
  570. case AnnotArgsType.AnnotFreehand://手绘
  571. if (data is FreehandAnnotArgs freehandAnnotArgs)
  572. {
  573. AddScreenAnnotationArgs(freehandAnnotArgs.InkColor, color, item, annotationArgs);
  574. }
  575. break;
  576. case AnnotArgsType.AnnotSquiggly://波浪线
  577. break;
  578. case AnnotArgsType.AnnotStamp://图章
  579. break;
  580. case AnnotArgsType.AnnotStrikeout://删除线
  581. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  582. {
  583. AddScreenAnnotationArgs(textStrikeoutAnnotArgs.Color, color, item, annotationArgs);
  584. }
  585. break;
  586. case AnnotArgsType.AnnotSticky://便签
  587. if (data is StickyAnnotArgs stickyAnnotArgs)
  588. {
  589. AddScreenAnnotationArgs(stickyAnnotArgs.Color, color, item, annotationArgs);
  590. }
  591. break;
  592. case AnnotArgsType.AnnotUnderline://下划线
  593. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  594. {
  595. AddScreenAnnotationArgs(textUnderlineAnnotArgs.Color, color, item, annotationArgs);
  596. }
  597. break;
  598. case AnnotArgsType.AnnotLine:
  599. //if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
  600. //{
  601. // //箭头
  602. //}
  603. //else
  604. //{
  605. // //线
  606. //}
  607. if (data is LineAnnotArgs lineAnnotArgs)
  608. {
  609. AddScreenAnnotationArgs(lineAnnotArgs.LineColor, color, item, annotationArgs);
  610. }
  611. break;
  612. case AnnotArgsType.AnnotSquare://矩形
  613. if (data is SquareAnnotArgs squareAnnotArgs)
  614. {
  615. AddScreenAnnotationArgs(squareAnnotArgs.LineColor, color, item, annotationArgs);
  616. AddScreenAnnotationArgs(squareAnnotArgs.BgColor, color, item, annotationArgs);
  617. }
  618. break;
  619. case AnnotArgsType.AnnotCircle://圆
  620. if (data is CircleAnnotArgs circleAnnotArgs)
  621. {
  622. AddScreenAnnotationArgs(circleAnnotArgs.LineColor, color, item, annotationArgs);
  623. AddScreenAnnotationArgs(circleAnnotArgs.BgColor, color, item, annotationArgs);
  624. }
  625. break;
  626. }
  627. }
  628. /// <summary>
  629. /// 对比颜色
  630. /// </summary>
  631. /// <param name="color1"></param>
  632. /// <param name="color"></param>
  633. /// <param name="item"></param>
  634. /// <param name="annotationArgs"></param>
  635. private void AddScreenAnnotationArgs(System.Windows.Media.Color color1, string color, AnnotationHandlerEventArgs item, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  636. {
  637. if (color1.ToString() == color)
  638. {
  639. annotationArgs.Add(item);
  640. }
  641. }
  642. /// <summary>
  643. /// 添加附注的注释
  644. /// </summary>
  645. /// <param name="obj"></param>
  646. private void AddNotesEvent(object obj)
  647. {
  648. if (obj is AnnotationHandlerEventArgs data)
  649. {
  650. if (data != null)
  651. {
  652. if (data.EventType != AnnotArgsType.AnnotSticky)
  653. {
  654. DialogParameters value = new DialogParameters();
  655. value.Add(ParameterNames.Annotation, data);
  656. dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
  657. {
  658. if (e.Result == ButtonResult.OK && e.Parameters != null)
  659. {
  660. //PdfViewer.UndoManager.CanSave = true;
  661. // if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
  662. // {
  663. // AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
  664. // //AnnotAttribEvent annotEvent = e.Parameters.GetValue<AnnotAttribEvent>(ParameterNames.AnnotEvent);
  665. // //annotEvent?.UpdateAttrib(AnnotAttrib.NoteText, annotation.MarkupContent);
  666. // //annotEvent?.UpdateAnnot();
  667. // }
  668. }
  669. });
  670. }
  671. //if(data.EventType == AnnotArgsType.AnnotFreeText)
  672. //{
  673. // ListBoxItem myListBoxItem = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(data));
  674. // TextBlock txbContent = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  675. // TextBox txtContent = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  676. // txbContent.Visibility = Visibility.Collapsed;
  677. // txtContent.Visibility = Visibility.Visible;
  678. //}
  679. }
  680. }
  681. }
  682. /// <summary>
  683. /// listboxitem鼠标左键点击,显示分组的数据
  684. /// </summary>
  685. /// <param name="obj"></param>
  686. private void ListBoxItem_PreviewMouseLeftButtonDown(object obj)
  687. {
  688. if (obj is ListBoxItem item)
  689. {
  690. var data = item.DataContext as AnnotationHandlerEventArgs;
  691. if (data != null)
  692. {
  693. PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex);
  694. }
  695. }
  696. }
  697. public async void UpdateAddedAnnot(int pageIndex, int annotIndex)
  698. {
  699. var annots = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  700. foreach (var item in annots)
  701. {
  702. if (item.AnnotIndex == annotIndex && item.EventType != AnnotArgsType.AnnotRedaction)
  703. {
  704. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  705. if (args.AnnotHandlerEventArgs != null)
  706. {
  707. AnnotationListItems.Add(args);
  708. CurrentAnnotationArgs.Add(args);
  709. await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  710. ExpandGroupHeader(args, listBox);
  711. listBox.SelectedItem = item;
  712. await Task.Delay(1);//不加延时 会有不滚动的现象
  713. listBox.ScrollIntoView(item);
  714. if (!listBox.SelectedItems.Contains(args))
  715. {
  716. listBox.SelectedItem = args;
  717. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  718. }
  719. }
  720. }
  721. }
  722. if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
  723. {
  724. IsEmptyPanelVisibility = Visibility.Collapsed;
  725. }
  726. }
  727. public async void UpdateModifiedAnnot(int pageIndex, int annotIndex, bool isDelete)
  728. {
  729. try
  730. {
  731. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  732. if (isDelete && items.Count == 0)//当前页的最后一次删除
  733. {
  734. for (int k = 0; k < AnnotationListItems.Count; k++)
  735. {
  736. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  737. {
  738. AnnotationListItems.RemoveAt(k);
  739. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationArgs.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  740. CurrentAnnotationArgs.Remove(annotation1);
  741. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  742. return;
  743. }
  744. }
  745. }
  746. if (isDelete)//删除
  747. {
  748. for (int k = 0; k < AnnotationListItems.Count; k++)
  749. {
  750. //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
  751. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  752. {
  753. AnnotationListItems.RemoveAt(k);
  754. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationArgs.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  755. CurrentAnnotationArgs.Remove(annotation1);
  756. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  757. return;
  758. }
  759. }
  760. }
  761. for (int j = 0; j < items.Count; j++)
  762. {
  763. for (int k = 0; k < AnnotationListItems.Count; k++)
  764. {
  765. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex && items[j].AnnotIndex == annotIndex)
  766. {
  767. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
  768. AnnotationListItems[k] = args;
  769. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationArgs.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  770. for (int i = 0; i < CurrentAnnotationArgs.Count; i++)
  771. {
  772. if (annotation1 == CurrentAnnotationArgs[i])
  773. {
  774. CurrentAnnotationArgs[i] = args;
  775. break;
  776. }
  777. }
  778. if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
  779. {
  780. listBox.SelectedItem = AnnotationListItems[k];
  781. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  782. }
  783. await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  784. ExpandGroupHeader(AnnotationListItems[k], listBox);
  785. await Task.Delay(1);//不加延时 会有不滚动的现象
  786. listBox.ScrollIntoView(AnnotationListItems[k]);
  787. }
  788. }
  789. }
  790. }
  791. catch { }
  792. }
  793. /// <summary>
  794. /// 展开item所在分组
  795. /// </summary>
  796. private void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
  797. {
  798. try
  799. {
  800. var groups = listBox.Items.Groups;
  801. for (int i = 0; i < groups.Count; i++)
  802. {
  803. var l = groups[i] as CollectionViewGroup;
  804. if (l.Items.Contains(item))
  805. {
  806. var scroller = GetScrollHost(listBox);
  807. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  808. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  809. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  810. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  811. if (g != null)
  812. g.IsExpanded = true;
  813. }
  814. }
  815. }
  816. catch { }
  817. }
  818. /// <summary>
  819. /// 页面加载时
  820. /// </summary>
  821. /// <param name="obj"></param>
  822. private void Loaded(object obj)
  823. {
  824. if (obj is CompositeCommandParameter composite)
  825. {
  826. if (composite.Parameter is ListBox listBox)
  827. {
  828. this.listBox = listBox;
  829. SetGroupHeader(listBox);
  830. }
  831. }
  832. }
  833. public bool IsNavigationTarget(NavigationContext navigationContext)
  834. {
  835. return true;
  836. }
  837. public void OnNavigatedFrom(NavigationContext navigationContext)
  838. {
  839. }
  840. public void OnNavigatedTo(NavigationContext navigationContext)
  841. {
  842. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  843. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  844. if (pdfview != null && viewContentViewModel != null)
  845. {
  846. ViewContentViewModel = viewContentViewModel;
  847. PdfViewer = pdfview;
  848. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  849. AnnotationListItems = GetDocumentAnnotionList();
  850. CurrentAnnotationArgs.AddRange(AnnotationListItems);
  851. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  852. }
  853. }
  854. /// <summary>
  855. /// 默认按照PageIndex,分组 排序
  856. /// </summary>
  857. /// <param name="listBox"></param>
  858. private async void SetGroupHeader(ListBox listBox)
  859. {
  860. //按照PageIndex,分组 排序
  861. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  862. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  863. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  864. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  865. if (AnnotationListItems.Count > 0)
  866. {
  867. IsEmptyPanelVisibility = Visibility.Collapsed;
  868. await Task.Delay(5);
  869. //展开数据
  870. ExpandGroupHeader(AnnotationListItems, listBox);
  871. }
  872. else
  873. {
  874. IsEmptyPanelVisibility = Visibility.Visible;
  875. }
  876. }
  877. /// <summary>
  878. /// 展开列表项
  879. /// </summary>
  880. /// <param name="annotationListItems"></param>
  881. /// <param name="listBox"></param>
  882. private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  883. {
  884. try
  885. {
  886. foreach (var item in annotationListItems)
  887. {
  888. var groups = listBox.Items.Groups;
  889. for (int i = 0; i < groups.Count; i++)
  890. {
  891. var group = groups[i] as CollectionViewGroup;
  892. if (group.Items.Contains(item))
  893. {
  894. var scroller = GetScrollHost(listBox);
  895. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  896. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  897. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  898. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  899. if (g != null)
  900. {
  901. g.IsExpanded = true;
  902. }
  903. }
  904. }
  905. }
  906. }
  907. catch { }
  908. }
  909. private ScrollViewer GetScrollHost(ListBox listBox)
  910. {
  911. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  912. {
  913. int s = VisualTreeHelper.GetChildrenCount(listBox);
  914. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  915. if (border != null)
  916. {
  917. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  918. }
  919. }
  920. return null;
  921. }
  922. /// <summary>
  923. /// 获取listbox绑定数据
  924. /// </summary>
  925. /// <returns></returns>
  926. private ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList()
  927. {
  928. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  929. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  930. {
  931. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  932. foreach (var item in items)
  933. {
  934. //原型图上,目前对波浪线的类型,在注释列表不显示
  935. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly)
  936. {
  937. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  938. if (args.AnnotHandlerEventArgs != null)
  939. {
  940. list.Add(args);
  941. }
  942. }
  943. }
  944. }
  945. return list;
  946. }
  947. private AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
  948. {
  949. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  950. if (item.EventType == AnnotArgsType.AnnotFreehand)
  951. {
  952. WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  953. args.WriteableBitmap = bitmap;
  954. }
  955. args.AnnotHandlerEventArgs = item;
  956. args.PageIndex = item.PageIndex;
  957. args.AnnotIndex = item.AnnotIndex;
  958. args.EventType = item.EventType;
  959. args.CreateTime = item.CreateTime;
  960. args.UpdateTime = item.UpdateTime;
  961. args.Content = item.Content;
  962. args.MarkupContent = item.MarkupContent;
  963. args.Author = item.Author;
  964. args.Locked = item.Locked;
  965. args.ReadOnly = item.ReadOnly;
  966. args.FormField = item.FormField;
  967. args.Document = PdfViewer.Document;
  968. return args;
  969. }
  970. /// <summary>
  971. /// 获取手绘图案
  972. /// </summary>
  973. /// <param name="doc"></param>
  974. /// <param name="pageIndex"></param>
  975. /// <param name="annotIndex"></param>
  976. /// <returns></returns>
  977. public WriteableBitmap GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  978. {
  979. if (doc == null)
  980. {
  981. return null;
  982. }
  983. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  984. if (docPage == null)
  985. {
  986. return null;
  987. }
  988. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  989. foreach (CPDFAnnotation annot in docAnnots)
  990. {
  991. if (docAnnots.IndexOf(annot) == annotIndex)
  992. {
  993. CRect rawRect = annot.GetRect();
  994. double scaleDpi = 96.0 / 72.0;
  995. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  996. int drawWidth = (int)paintRect.Width;
  997. int drawHeight = (int)paintRect.Height;
  998. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  999. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  1000. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  1001. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  1002. return wirteBitmap;
  1003. }
  1004. }
  1005. return null;
  1006. }
  1007. /// <summary>
  1008. /// listbox的itemsource发生变化时
  1009. /// </summary>
  1010. /// <param name="sender"></param>
  1011. /// <param name="e"></param>
  1012. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  1013. {
  1014. if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
  1015. {
  1016. //currentAnnotationArgs.Clear();
  1017. //currentAnnotationArgs.AddRange(obsSender);
  1018. if (obsSender.Count < 1)
  1019. {
  1020. IsEmptyPanelVisibility = Visibility.Visible;
  1021. //currentAnnotationArgs.Clear();
  1022. }
  1023. else
  1024. {
  1025. IsEmptyPanelVisibility = Visibility.Collapsed;
  1026. }
  1027. }
  1028. else
  1029. {
  1030. IsEmptyPanelVisibility = Visibility.Visible;
  1031. }
  1032. }
  1033. }
  1034. }