AnnotationContentViewModel.cs 47 KB

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