AnnotationContentViewModel.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. AnnotationListItems[k].Content = items[j].Content;
  269. AnnotationListItems[k].MarkupContent = items[j].MarkupContent;
  270. AnnotationListItems[k].Author = items[j].Author;
  271. AnnotationListItems[k].Locked = items[j].Locked;
  272. AnnotationListItems[k].ReadOnly = items[j].ReadOnly;
  273. AnnotationListItems[k].FormField = items[j].FormField;
  274. AnnotationListItems[k].Document = PdfViewer.Document;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. /// <summary>
  281. /// 导出注释
  282. /// </summary>
  283. /// <param name="obj"></param>
  284. private void ExportCommentsEvent(object obj)
  285. {
  286. if (ViewContentViewModel.CanSave)
  287. {
  288. ViewContentViewModel.SaveFile.Execute();
  289. }
  290. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  291. dlg.Filter = "PDF|*.xfdf";
  292. dlg.DefaultExt = ".xfdf";
  293. dlg.FileName = PdfViewer.Document.FileName;
  294. if (dlg.ShowDialog() == true)
  295. {
  296. string fileName = dlg.FileName;
  297. var result = PdfViewer.ExportAnnotationToXFDFPath(fileName);
  298. if (result)
  299. {
  300. //MessageBoxEx.Show("导出成功", "", Winform.MessageBoxButtons.OK);
  301. AlertsMessage alertsMessage = new AlertsMessage();
  302. alertsMessage.Show("提示", "导出成功!", "OK");
  303. }
  304. else
  305. {
  306. AlertsMessage alertsMessage = new AlertsMessage();
  307. alertsMessage.Show("提示", "导出失败!", "OK");
  308. //MessageBoxEx.Show("导出失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
  309. }
  310. }
  311. }
  312. /// <summary>
  313. /// 筛选注释
  314. /// </summary>
  315. /// <param name="obj"></param>
  316. private void ScreenEvent(object obj)
  317. {
  318. if (obj is System.Windows.Controls.Button button)
  319. {
  320. DialogParameters value = new DialogParameters();
  321. value.Add(ParameterNames.AnnotationList, CurrentAnnotationArgs);
  322. value.Add(ParameterNames.AnnotArgsTypes, annotArgsTypes);
  323. value.Add(ParameterNames.AnnotationColors, colors);
  324. value.Add(ParameterNames.AnnotationAuthor, authors);
  325. dialogs.ShowDialog(DialogNames.ScreenAnnotationDialog, value, e =>
  326. {
  327. if (e.Result == ButtonResult.OK && e.Parameters != null)
  328. {
  329. bool cleanState = false;
  330. if (e.Parameters.ContainsKey(ParameterNames.AnnotationCleanState))
  331. {
  332. cleanState = e.Parameters.GetValue<bool>(ParameterNames.AnnotationCleanState);
  333. if (cleanState)
  334. {
  335. colors.Clear();
  336. authors.Clear();
  337. annotArgsTypes.Clear();
  338. AnnotationListItems.Clear();
  339. AnnotationListItems.AddRange(CurrentAnnotationArgs);
  340. RefreshAnnotationListItems(button);
  341. }
  342. else
  343. {
  344. if (e.Parameters.ContainsKey(ParameterNames.AnnotArgsTypes))
  345. {
  346. annotArgsTypes = e.Parameters.GetValue<List<AnnotArgsType>>(ParameterNames.AnnotArgsTypes);
  347. }
  348. if (e.Parameters.ContainsKey(ParameterNames.AnnotationColors))
  349. {
  350. colors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationColors);
  351. }
  352. if (e.Parameters.ContainsKey(ParameterNames.AnnotationAuthor))
  353. {
  354. authors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationAuthor);
  355. }
  356. ObservableCollection<AnnotationHandlerEventArgs> screenAnnotationArgs = SelectAnnotationListItems(annotArgsTypes, colors, authors);
  357. AnnotationListItems.Clear();
  358. AnnotationListItems.AddRange(screenAnnotationArgs);
  359. RefreshAnnotationListItems(button);
  360. }
  361. }
  362. }
  363. else if (e.Result == ButtonResult.Cancel && e.Parameters != null)
  364. {
  365. colors.Clear();
  366. authors.Clear();
  367. annotArgsTypes.Clear();
  368. AnnotationListItems.Clear();
  369. AnnotationListItems.AddRange(CurrentAnnotationArgs);
  370. RefreshAnnotationListItems(button);
  371. }
  372. });
  373. }
  374. }
  375. /// <summary>
  376. /// 刷新列表数据
  377. /// </summary>
  378. /// <param name="button"></param>
  379. private async void RefreshAnnotationListItems(System.Windows.Controls.Button button)
  380. {
  381. switch (AnnotationSortOrder)
  382. {
  383. case AnnotationSortOrder.TimeAscending:
  384. TimeAscendingEvent();
  385. break;
  386. case AnnotationSortOrder.TimeDescending:
  387. TimeDescendingEvent();
  388. break;
  389. case AnnotationSortOrder.PageIndexAscending:
  390. PageSortEvent();
  391. break;
  392. default:
  393. break;
  394. }
  395. if (AnnotationListItems.Count > 0)
  396. {
  397. IsEmptyPanelVisibility = Visibility.Collapsed;
  398. await Task.Delay(2);
  399. //展开数据
  400. ExpandGroupHeader(AnnotationListItems, listBox);
  401. }
  402. else
  403. {
  404. if (button != null)
  405. {
  406. button.IsEnabled = true;
  407. }
  408. IsEmptyPanelVisibility = Visibility.Visible;
  409. //AnnotationListItems.AddRange(currentAnnotationArgs);
  410. //RefreshAnnotationListItems();
  411. }
  412. }
  413. /// <summary>
  414. /// 根据条件筛选列表数据
  415. /// </summary>
  416. /// <param name="annotArgsTypes"></param>
  417. /// <param name="colors"></param>
  418. /// <param name="authors"></param>
  419. /// <returns></returns>
  420. private ObservableCollection<AnnotationHandlerEventArgs> SelectAnnotationListItems(List<AnnotArgsType> annotArgsTypes, List<string> colors, List<string> authors)
  421. {
  422. ObservableCollection<AnnotationHandlerEventArgs> annotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
  423. #region 类型 颜色 作者
  424. if (annotArgsTypes.Count > 0 && colors.Count > 0 && authors.Count > 0)
  425. {
  426. foreach (var item in CurrentAnnotationArgs)
  427. {
  428. foreach (var type in annotArgsTypes)
  429. {
  430. foreach (var color in colors)
  431. {
  432. foreach (var author in authors)
  433. {
  434. if (item.EventType == type && item.Author == author)
  435. {
  436. SelectAnnotationColors(item, color, annotationArgs);
  437. }
  438. }
  439. }
  440. }
  441. }
  442. }
  443. #endregion 类型 颜色 作者
  444. #region 颜色 作者
  445. else if (colors.Count > 0 && authors.Count > 0)
  446. {
  447. foreach (var item in CurrentAnnotationArgs)
  448. {
  449. foreach (var color in colors)
  450. {
  451. foreach (var author in authors)
  452. {
  453. if (item.Author == author)
  454. {
  455. SelectAnnotationColors(item, color, annotationArgs);
  456. }
  457. }
  458. }
  459. }
  460. }
  461. #endregion 颜色 作者
  462. #region 类型 作者
  463. else if (annotArgsTypes.Count > 0 && authors.Count > 0)
  464. {
  465. foreach (var item in CurrentAnnotationArgs)
  466. {
  467. foreach (var type in annotArgsTypes)
  468. {
  469. foreach (var author in authors)
  470. {
  471. if (item.EventType == type && item.Author == author)
  472. {
  473. annotationArgs.Add(item);
  474. }
  475. }
  476. }
  477. }
  478. }
  479. #endregion 类型 作者
  480. #region 类型 颜色
  481. else if (annotArgsTypes.Count > 0 && colors.Count > 0)
  482. {
  483. foreach (var item in CurrentAnnotationArgs)
  484. {
  485. foreach (var type in annotArgsTypes)
  486. {
  487. foreach (var color in colors)
  488. {
  489. if (item.EventType == type)
  490. {
  491. SelectAnnotationColors(item, color, annotationArgs);
  492. }
  493. }
  494. }
  495. }
  496. }
  497. #endregion 类型 颜色
  498. #region 类型
  499. else if (annotArgsTypes.Count > 0)
  500. {
  501. foreach (var item in CurrentAnnotationArgs)
  502. {
  503. foreach (var type in annotArgsTypes)
  504. {
  505. if (item.EventType == type)
  506. {
  507. annotationArgs.Add(item);
  508. }
  509. }
  510. }
  511. }
  512. #endregion 类型
  513. #region 颜色
  514. else if (colors.Count > 0)
  515. {
  516. foreach (var item in CurrentAnnotationArgs)
  517. {
  518. foreach (var color in colors)
  519. {
  520. SelectAnnotationColors(item, color, annotationArgs);
  521. }
  522. }
  523. }
  524. #endregion 颜色
  525. #region 作者
  526. else if (authors.Count > 0)
  527. {
  528. foreach (var item in CurrentAnnotationArgs)
  529. {
  530. foreach (var author in authors)
  531. {
  532. if (item.Author == author)
  533. {
  534. annotationArgs.Add(item);
  535. }
  536. }
  537. }
  538. }
  539. #endregion 作者
  540. return annotationArgs;
  541. }
  542. /// <summary>
  543. /// 根据颜色筛选注释
  544. /// </summary>
  545. /// <param name="item"></param>
  546. /// <param name="color"></param>
  547. /// <param name="annotationArgs"></param>
  548. private void SelectAnnotationColors(AnnotationHandlerEventArgs item, string color, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  549. {
  550. AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs;
  551. switch (item.EventType)
  552. {
  553. case AnnotArgsType.AnnotFreeText://文本
  554. if (data is FreeTextAnnotArgs textAnnotArgs)
  555. {
  556. AddScreenAnnotationArgs(textAnnotArgs.FontColor, color, item, annotationArgs);
  557. }
  558. break;
  559. case AnnotArgsType.AnnotHighlight://高亮
  560. if (data is TextHighlightAnnotArgs highlightAnnotArgs)
  561. {
  562. AddScreenAnnotationArgs(highlightAnnotArgs.Color, color, item, annotationArgs);
  563. }
  564. break;
  565. case AnnotArgsType.AnnotFreehand://手绘
  566. if (data is FreehandAnnotArgs freehandAnnotArgs)
  567. {
  568. AddScreenAnnotationArgs(freehandAnnotArgs.InkColor, color, item, annotationArgs);
  569. }
  570. break;
  571. case AnnotArgsType.AnnotSquiggly://波浪线
  572. break;
  573. case AnnotArgsType.AnnotStamp://图章
  574. break;
  575. case AnnotArgsType.AnnotStrikeout://删除线
  576. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  577. {
  578. AddScreenAnnotationArgs(textStrikeoutAnnotArgs.Color, color, item, annotationArgs);
  579. }
  580. break;
  581. case AnnotArgsType.AnnotSticky://便签
  582. if (data is StickyAnnotArgs stickyAnnotArgs)
  583. {
  584. AddScreenAnnotationArgs(stickyAnnotArgs.Color, color, item, annotationArgs);
  585. }
  586. break;
  587. case AnnotArgsType.AnnotUnderline://下划线
  588. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  589. {
  590. AddScreenAnnotationArgs(textUnderlineAnnotArgs.Color, color, item, annotationArgs);
  591. }
  592. break;
  593. case AnnotArgsType.AnnotLine:
  594. //if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
  595. //{
  596. // //箭头
  597. //}
  598. //else
  599. //{
  600. // //线
  601. //}
  602. if (data is LineAnnotArgs lineAnnotArgs)
  603. {
  604. AddScreenAnnotationArgs(lineAnnotArgs.LineColor, color, item, annotationArgs);
  605. }
  606. break;
  607. case AnnotArgsType.AnnotSquare://矩形
  608. if (data is SquareAnnotArgs squareAnnotArgs)
  609. {
  610. AddScreenAnnotationArgs(squareAnnotArgs.LineColor, color, item, annotationArgs);
  611. AddScreenAnnotationArgs(squareAnnotArgs.BgColor, color, item, annotationArgs);
  612. }
  613. break;
  614. case AnnotArgsType.AnnotCircle://圆
  615. if (data is CircleAnnotArgs circleAnnotArgs)
  616. {
  617. AddScreenAnnotationArgs(circleAnnotArgs.LineColor, color, item, annotationArgs);
  618. AddScreenAnnotationArgs(circleAnnotArgs.BgColor, color, item, annotationArgs);
  619. }
  620. break;
  621. }
  622. }
  623. /// <summary>
  624. /// 对比颜色
  625. /// </summary>
  626. /// <param name="color1"></param>
  627. /// <param name="color"></param>
  628. /// <param name="item"></param>
  629. /// <param name="annotationArgs"></param>
  630. private void AddScreenAnnotationArgs(System.Windows.Media.Color color1, string color, AnnotationHandlerEventArgs item, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  631. {
  632. if (color1.ToString() == color)
  633. {
  634. annotationArgs.Add(item);
  635. }
  636. }
  637. /// <summary>
  638. /// 添加附注的注释
  639. /// </summary>
  640. /// <param name="obj"></param>
  641. private void AddNotesEvent(object obj)
  642. {
  643. if (obj is AnnotationHandlerEventArgs data)
  644. {
  645. if (data != null)
  646. {
  647. if (data.EventType != AnnotArgsType.AnnotSticky)
  648. {
  649. DialogParameters value = new DialogParameters();
  650. value.Add(ParameterNames.Annotation, data);
  651. dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
  652. {
  653. if (e.Result == ButtonResult.OK && e.Parameters != null)
  654. {
  655. //PdfViewer.UndoManager.CanSave = true;
  656. // if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
  657. // {
  658. // AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
  659. // //AnnotAttribEvent annotEvent = e.Parameters.GetValue<AnnotAttribEvent>(ParameterNames.AnnotEvent);
  660. // //annotEvent?.UpdateAttrib(AnnotAttrib.NoteText, annotation.MarkupContent);
  661. // //annotEvent?.UpdateAnnot();
  662. // }
  663. }
  664. });
  665. }
  666. //if(data.EventType == AnnotArgsType.AnnotFreeText)
  667. //{
  668. // ListBoxItem myListBoxItem = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(data));
  669. // TextBlock txbContent = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  670. // TextBox txtContent = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  671. // txbContent.Visibility = Visibility.Collapsed;
  672. // txtContent.Visibility = Visibility.Visible;
  673. //}
  674. }
  675. }
  676. }
  677. /// <summary>
  678. /// listboxitem鼠标左键点击,显示分组的数据
  679. /// </summary>
  680. /// <param name="obj"></param>
  681. private void ListBoxItem_PreviewMouseLeftButtonDown(object obj)
  682. {
  683. if (obj is ListBoxItem item)
  684. {
  685. var data = item.DataContext as AnnotationHandlerEventArgs;
  686. if (data != null)
  687. {
  688. PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex);
  689. }
  690. }
  691. }
  692. public async void UpdateAddedAnnot(int pageIndex, int annotIndex)
  693. {
  694. var annots = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  695. foreach (var item in annots)
  696. {
  697. if (item.AnnotIndex == annotIndex && item.EventType != AnnotArgsType.AnnotRedaction)
  698. {
  699. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  700. if (args.AnnotHandlerEventArgs != null)
  701. {
  702. AnnotationListItems.Add(args);
  703. CurrentAnnotationArgs.Add(args);
  704. await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  705. ExpandGroupHeader(args, listBox);
  706. listBox.SelectedItem = item;
  707. await Task.Delay(1);//不加延时 会有不滚动的现象
  708. listBox.ScrollIntoView(item);
  709. if (!listBox.SelectedItems.Contains(args))
  710. {
  711. listBox.SelectedItem = args;
  712. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  713. }
  714. }
  715. }
  716. }
  717. if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
  718. {
  719. IsEmptyPanelVisibility = Visibility.Collapsed;
  720. }
  721. }
  722. public async void UpdateModifiedAnnot(int pageIndex, int annotIndex, bool isDelete)
  723. {
  724. try
  725. {
  726. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  727. if (isDelete && items.Count == 0)//当前页的最后一次删除
  728. {
  729. for (int k = 0; k < AnnotationListItems.Count; k++)
  730. {
  731. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  732. {
  733. AnnotationListItems.RemoveAt(k);
  734. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationArgs.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  735. CurrentAnnotationArgs.Remove(annotation1);
  736. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  737. return;
  738. }
  739. }
  740. }
  741. if (isDelete)//删除
  742. {
  743. for (int k = 0; k < AnnotationListItems.Count; k++)
  744. {
  745. //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
  746. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  747. {
  748. AnnotationListItems.RemoveAt(k);
  749. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationArgs.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  750. CurrentAnnotationArgs.Remove(annotation1);
  751. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  752. return;
  753. }
  754. }
  755. }
  756. for (int j = 0; j < items.Count; j++)
  757. {
  758. for (int k = 0; k < AnnotationListItems.Count; k++)
  759. {
  760. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex && items[j].AnnotIndex == annotIndex)
  761. {
  762. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
  763. AnnotationListItems[k] = args;
  764. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationArgs.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  765. for (int i = 0; i < CurrentAnnotationArgs.Count; i++)
  766. {
  767. if (annotation1 == CurrentAnnotationArgs[i])
  768. {
  769. CurrentAnnotationArgs[i] = args;
  770. }
  771. }
  772. if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
  773. {
  774. listBox.SelectedItem = AnnotationListItems[k];
  775. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  776. }
  777. await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  778. ExpandGroupHeader(AnnotationListItems[k], listBox);
  779. await Task.Delay(1);//不加延时 会有不滚动的现象
  780. listBox.ScrollIntoView(AnnotationListItems[k]);
  781. }
  782. }
  783. }
  784. }
  785. catch { }
  786. }
  787. /// <summary>
  788. /// 展开item所在分组
  789. /// </summary>
  790. private void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
  791. {
  792. try
  793. {
  794. var groups = listBox.Items.Groups;
  795. for (int i = 0; i < groups.Count; i++)
  796. {
  797. var l = groups[i] as CollectionViewGroup;
  798. if (l.Items.Contains(item))
  799. {
  800. var scroller = GetScrollHost(listBox);
  801. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  802. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  803. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  804. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  805. if (g != null)
  806. g.IsExpanded = true;
  807. }
  808. }
  809. }
  810. catch { }
  811. }
  812. /// <summary>
  813. /// 页面加载时
  814. /// </summary>
  815. /// <param name="obj"></param>
  816. private void Loaded(object obj)
  817. {
  818. if (obj is CompositeCommandParameter composite)
  819. {
  820. if (composite.Parameter is ListBox listBox)
  821. {
  822. this.listBox = listBox;
  823. SetGroupHeader(listBox);
  824. }
  825. }
  826. }
  827. public bool IsNavigationTarget(NavigationContext navigationContext)
  828. {
  829. return true;
  830. }
  831. public void OnNavigatedFrom(NavigationContext navigationContext)
  832. {
  833. }
  834. public void OnNavigatedTo(NavigationContext navigationContext)
  835. {
  836. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  837. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  838. if (pdfview != null && viewContentViewModel != null)
  839. {
  840. ViewContentViewModel = viewContentViewModel;
  841. PdfViewer = pdfview;
  842. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  843. AnnotationListItems = GetDocumentAnnotionList();
  844. CurrentAnnotationArgs.AddRange(AnnotationListItems);
  845. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  846. }
  847. }
  848. /// <summary>
  849. /// 默认按照PageIndex,分组 排序
  850. /// </summary>
  851. /// <param name="listBox"></param>
  852. private async void SetGroupHeader(ListBox listBox)
  853. {
  854. //按照PageIndex,分组 排序
  855. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  856. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  857. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  858. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  859. if (AnnotationListItems.Count > 0)
  860. {
  861. IsEmptyPanelVisibility = Visibility.Collapsed;
  862. await Task.Delay(5);
  863. //展开数据
  864. ExpandGroupHeader(AnnotationListItems, listBox);
  865. }
  866. else
  867. {
  868. IsEmptyPanelVisibility = Visibility.Visible;
  869. }
  870. }
  871. /// <summary>
  872. /// 展开列表项
  873. /// </summary>
  874. /// <param name="annotationListItems"></param>
  875. /// <param name="listBox"></param>
  876. private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  877. {
  878. try
  879. {
  880. foreach (var item in annotationListItems)
  881. {
  882. var groups = listBox.Items.Groups;
  883. for (int i = 0; i < groups.Count; i++)
  884. {
  885. var group = groups[i] as CollectionViewGroup;
  886. if (group.Items.Contains(item))
  887. {
  888. var scroller = GetScrollHost(listBox);
  889. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  890. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  891. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  892. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  893. if (g != null)
  894. {
  895. g.IsExpanded = true;
  896. }
  897. }
  898. }
  899. }
  900. }
  901. catch { }
  902. }
  903. private ScrollViewer GetScrollHost(ListBox listBox)
  904. {
  905. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  906. {
  907. int s = VisualTreeHelper.GetChildrenCount(listBox);
  908. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  909. if (border != null)
  910. {
  911. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  912. }
  913. }
  914. return null;
  915. }
  916. /// <summary>
  917. /// 获取listbox绑定数据
  918. /// </summary>
  919. /// <returns></returns>
  920. private ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList()
  921. {
  922. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  923. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  924. {
  925. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  926. foreach (var item in items)
  927. {
  928. //原型图上,目前对波浪线的类型,在注释列表不显示
  929. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly)
  930. {
  931. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  932. if (args.AnnotHandlerEventArgs != null)
  933. {
  934. list.Add(args);
  935. }
  936. }
  937. }
  938. }
  939. return list;
  940. }
  941. private AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
  942. {
  943. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  944. if (item.EventType == AnnotArgsType.AnnotFreehand)
  945. {
  946. WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  947. args.WriteableBitmap = bitmap;
  948. }
  949. args.AnnotHandlerEventArgs = item;
  950. args.PageIndex = item.PageIndex;
  951. args.AnnotIndex = item.AnnotIndex;
  952. args.EventType = item.EventType;
  953. args.CreateTime = item.CreateTime;
  954. args.UpdateTime = item.UpdateTime;
  955. args.Content = item.Content;
  956. args.MarkupContent = item.MarkupContent;
  957. args.Author = item.Author;
  958. args.Locked = item.Locked;
  959. args.ReadOnly = item.ReadOnly;
  960. args.FormField = item.FormField;
  961. args.Document = PdfViewer.Document;
  962. return args;
  963. }
  964. /// <summary>
  965. /// 获取手绘图案
  966. /// </summary>
  967. /// <param name="doc"></param>
  968. /// <param name="pageIndex"></param>
  969. /// <param name="annotIndex"></param>
  970. /// <returns></returns>
  971. public WriteableBitmap GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  972. {
  973. if (doc == null)
  974. {
  975. return null;
  976. }
  977. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  978. if (docPage == null)
  979. {
  980. return null;
  981. }
  982. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  983. foreach (CPDFAnnotation annot in docAnnots)
  984. {
  985. if (docAnnots.IndexOf(annot) == annotIndex)
  986. {
  987. CRect rawRect = annot.GetRect();
  988. double scaleDpi = 96.0 / 72.0;
  989. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  990. int drawWidth = (int)paintRect.Width;
  991. int drawHeight = (int)paintRect.Height;
  992. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  993. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  994. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  995. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  996. return wirteBitmap;
  997. }
  998. }
  999. return null;
  1000. }
  1001. /// <summary>
  1002. /// listbox的itemsource发生变化时
  1003. /// </summary>
  1004. /// <param name="sender"></param>
  1005. /// <param name="e"></param>
  1006. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  1007. {
  1008. if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
  1009. {
  1010. //currentAnnotationArgs.Clear();
  1011. //currentAnnotationArgs.AddRange(obsSender);
  1012. if (obsSender.Count < 1)
  1013. {
  1014. IsEmptyPanelVisibility = Visibility.Visible;
  1015. //currentAnnotationArgs.Clear();
  1016. }
  1017. else
  1018. {
  1019. IsEmptyPanelVisibility = Visibility.Collapsed;
  1020. }
  1021. }
  1022. else
  1023. {
  1024. IsEmptyPanelVisibility = Visibility.Visible;
  1025. }
  1026. }
  1027. }
  1028. }