AnnotationContentViewModel.cs 54 KB

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