AnnotationContentViewModel.cs 53 KB

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