AnnotationContentViewModel.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. if (!listBox.SelectedItems.Contains(args))
  706. {
  707. listBox.SelectedItem = args;
  708. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  709. }
  710. }
  711. }
  712. }
  713. if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
  714. {
  715. IsEmptyPanelVisibility = Visibility.Collapsed;
  716. }
  717. }
  718. public async void UpdateModifiedAnnot(int pageIndex, int annotIndex, bool isDelete)
  719. {
  720. try
  721. {
  722. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  723. if (isDelete && items.Count == 0)//当前页的最后一次删除
  724. {
  725. for (int k = 0; k < AnnotationListItems.Count; k++)
  726. {
  727. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  728. {
  729. AnnotationListItems.RemoveAt(k);
  730. currentAnnotationArgs.RemoveAt(k);
  731. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  732. return;
  733. }
  734. }
  735. }
  736. if (isDelete)//删除
  737. {
  738. for (int k = 0; k < AnnotationListItems.Count; k++)
  739. {
  740. //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
  741. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  742. {
  743. AnnotationListItems.RemoveAt(k);
  744. currentAnnotationArgs.RemoveAt(k);
  745. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  746. return;
  747. }
  748. }
  749. }
  750. for (int j = 0; j < items.Count; j++)
  751. {
  752. for (int k = 0; k < AnnotationListItems.Count; k++)
  753. {
  754. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex && items[j].AnnotIndex == annotIndex)
  755. {
  756. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
  757. AnnotationListItems[k] = args;
  758. if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
  759. {
  760. listBox.SelectedItem = AnnotationListItems[k];
  761. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  762. }
  763. await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  764. ExpandGroupHeader(AnnotationListItems[k], listBox);
  765. await Task.Delay(1);//不加延时 会有不滚动的现象
  766. listBox.ScrollIntoView(AnnotationListItems[k]);
  767. }
  768. }
  769. }
  770. }
  771. catch { }
  772. }
  773. /// <summary>
  774. /// 展开item所在分组
  775. /// </summary>
  776. private void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
  777. {
  778. try
  779. {
  780. var groups = listBox.Items.Groups;
  781. for (int i = 0; i < groups.Count; i++)
  782. {
  783. var l = groups[i] as CollectionViewGroup;
  784. if (l.Items.Contains(item))
  785. {
  786. var scroller = GetScrollHost(listBox);
  787. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  788. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  789. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  790. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  791. if (g != null)
  792. g.IsExpanded = true;
  793. }
  794. }
  795. }
  796. catch { }
  797. }
  798. /// <summary>
  799. /// 页面加载时
  800. /// </summary>
  801. /// <param name="obj"></param>
  802. private void Loaded(object obj)
  803. {
  804. if (obj is CompositeCommandParameter composite)
  805. {
  806. if (composite.Parameter is ListBox listBox)
  807. {
  808. this.listBox = listBox;
  809. SetGroupHeader(listBox);
  810. }
  811. }
  812. }
  813. public bool IsNavigationTarget(NavigationContext navigationContext)
  814. {
  815. return true;
  816. }
  817. public void OnNavigatedFrom(NavigationContext navigationContext)
  818. {
  819. }
  820. public void OnNavigatedTo(NavigationContext navigationContext)
  821. {
  822. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  823. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  824. if (pdfview != null && viewContentViewModel != null)
  825. {
  826. ViewContentViewModel = viewContentViewModel;
  827. PdfViewer = pdfview;
  828. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  829. AnnotationListItems = GetDocumentAnnotionList();
  830. currentAnnotationArgs.AddRange(AnnotationListItems);
  831. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  832. }
  833. }
  834. /// <summary>
  835. /// 默认按照PageIndex,分组 排序
  836. /// </summary>
  837. /// <param name="listBox"></param>
  838. private async void SetGroupHeader(ListBox listBox)
  839. {
  840. //按照PageIndex,分组 排序
  841. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  842. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  843. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  844. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  845. if (AnnotationListItems.Count > 0)
  846. {
  847. IsEmptyPanelVisibility = Visibility.Collapsed;
  848. await Task.Delay(5);
  849. //展开数据
  850. ExpandGroupHeader(AnnotationListItems, listBox);
  851. }
  852. else
  853. {
  854. IsEmptyPanelVisibility = Visibility.Visible;
  855. }
  856. }
  857. /// <summary>
  858. /// 展开列表项
  859. /// </summary>
  860. /// <param name="annotationListItems"></param>
  861. /// <param name="listBox"></param>
  862. private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  863. {
  864. try
  865. {
  866. foreach (var item in annotationListItems)
  867. {
  868. var groups = listBox.Items.Groups;
  869. for (int i = 0; i < groups.Count; i++)
  870. {
  871. var group = groups[i] as CollectionViewGroup;
  872. if (group.Items.Contains(item))
  873. {
  874. var scroller = GetScrollHost(listBox);
  875. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  876. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  877. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  878. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  879. if (g != null)
  880. {
  881. g.IsExpanded = true;
  882. }
  883. }
  884. }
  885. }
  886. }
  887. catch { }
  888. }
  889. private ScrollViewer GetScrollHost(ListBox listBox)
  890. {
  891. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  892. {
  893. int s = VisualTreeHelper.GetChildrenCount(listBox);
  894. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  895. if (border != null)
  896. {
  897. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  898. }
  899. }
  900. return null;
  901. }
  902. /// <summary>
  903. /// 获取listbox绑定数据
  904. /// </summary>
  905. /// <returns></returns>
  906. private ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList()
  907. {
  908. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  909. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  910. {
  911. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  912. foreach (var item in items)
  913. {
  914. //原型图上,目前对波浪线的类型,在注释列表不显示
  915. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly)
  916. {
  917. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  918. if (args.AnnotHandlerEventArgs != null)
  919. {
  920. list.Add(args);
  921. }
  922. }
  923. }
  924. }
  925. return list;
  926. }
  927. private AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
  928. {
  929. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  930. if (item.EventType == AnnotArgsType.AnnotFreehand)
  931. {
  932. WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  933. args.WriteableBitmap = bitmap;
  934. }
  935. args.AnnotHandlerEventArgs = item;
  936. args.PageIndex = item.PageIndex;
  937. args.AnnotIndex = item.AnnotIndex;
  938. args.EventType = item.EventType;
  939. args.CreateTime = item.CreateTime;
  940. args.UpdateTime = item.UpdateTime;
  941. args.Content = item.Content;
  942. args.MarkupContent = item.MarkupContent;
  943. args.Author = item.Author;
  944. args.Locked = item.Locked;
  945. args.ReadOnly = item.ReadOnly;
  946. args.FormField = item.FormField;
  947. args.Document = PdfViewer.Document;
  948. return args;
  949. }
  950. /// <summary>
  951. /// 获取手绘图案
  952. /// </summary>
  953. /// <param name="doc"></param>
  954. /// <param name="pageIndex"></param>
  955. /// <param name="annotIndex"></param>
  956. /// <returns></returns>
  957. public WriteableBitmap GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  958. {
  959. if (doc == null)
  960. {
  961. return null;
  962. }
  963. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  964. if (docPage == null)
  965. {
  966. return null;
  967. }
  968. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  969. foreach (CPDFAnnotation annot in docAnnots)
  970. {
  971. if (docAnnots.IndexOf(annot) == annotIndex)
  972. {
  973. CRect rawRect = annot.GetRect();
  974. double scaleDpi = 96.0 / 72.0;
  975. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  976. int drawWidth = (int)paintRect.Width;
  977. int drawHeight = (int)paintRect.Height;
  978. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  979. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  980. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  981. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  982. return wirteBitmap;
  983. }
  984. }
  985. return null;
  986. }
  987. /// <summary>
  988. /// listbox的itemsource发生变化时
  989. /// </summary>
  990. /// <param name="sender"></param>
  991. /// <param name="e"></param>
  992. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  993. {
  994. if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
  995. {
  996. //currentAnnotationArgs.Clear();
  997. //currentAnnotationArgs.AddRange(obsSender);
  998. if (obsSender.Count < 1)
  999. {
  1000. IsEmptyPanelVisibility = Visibility.Visible;
  1001. }
  1002. else
  1003. {
  1004. IsEmptyPanelVisibility = Visibility.Collapsed;
  1005. }
  1006. }
  1007. else
  1008. {
  1009. IsEmptyPanelVisibility = Visibility.Visible;
  1010. }
  1011. }
  1012. }
  1013. }