AnnotationContentViewModel.cs 42 KB

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