AnnotationContentViewModel.cs 40 KB

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