AnnotationContentViewModel.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. bool cleanState = false;
  346. if (e.Parameters.ContainsKey(ParameterNames.AnnotationCleanState))
  347. {
  348. cleanState = e.Parameters.GetValue<bool>(ParameterNames.AnnotationCleanState);
  349. if (cleanState)
  350. {
  351. CleanAnnotation(button);
  352. }
  353. else
  354. {
  355. if (e.Parameters.ContainsKey(ParameterNames.AnnotArgsTypes))
  356. {
  357. annotArgsTypes = e.Parameters.GetValue<List<AnnotArgsType>>(ParameterNames.AnnotArgsTypes);
  358. }
  359. if (e.Parameters.ContainsKey(ParameterNames.AnnotationColors))
  360. {
  361. colors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationColors);
  362. }
  363. if (e.Parameters.ContainsKey(ParameterNames.AnnotationAuthor))
  364. {
  365. authors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationAuthor);
  366. }
  367. ObservableCollection<AnnotationHandlerEventArgs> screenAnnotationArgs = SelectAnnotationListItems(annotArgsTypes, colors, authors);
  368. AnnotationListItems.Clear();
  369. AnnotationListItems.AddRange(screenAnnotationArgs);
  370. //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  371. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  372. RefreshAnnotationListItems(button);
  373. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  374. }
  375. }
  376. }
  377. else if (e.Result == ButtonResult.Cancel && e.Parameters != null)
  378. {
  379. //colors.Clear();
  380. //authors.Clear();
  381. //annotArgsTypes.Clear();
  382. //AnnotationListItems.Clear();
  383. //AnnotationListItems.AddRange(CurrentAnnotationArgs);
  384. //RefreshAnnotationListItems(button);
  385. CleanAnnotation(button);
  386. }
  387. });
  388. }
  389. }
  390. private void CleanAnnotation(System.Windows.Controls.Button button)
  391. {
  392. colors.Clear();
  393. authors.Clear();
  394. annotArgsTypes.Clear();
  395. AnnotationListItems.Clear();
  396. AnnotationListItems.AddRange(CurrentAnnotationLists);
  397. //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  398. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  399. RefreshAnnotationListItems(button);
  400. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  401. }
  402. /// <summary>
  403. /// 刷新列表数据
  404. /// </summary>
  405. /// <param name="button"></param>
  406. private async void RefreshAnnotationListItems(System.Windows.Controls.Button button)
  407. {
  408. switch (AnnotationSortOrder)
  409. {
  410. case AnnotationSortOrder.TimeAscending:
  411. TimeAscendingEvent();
  412. break;
  413. case AnnotationSortOrder.TimeDescending:
  414. TimeDescendingEvent();
  415. break;
  416. case AnnotationSortOrder.PageIndexAscending:
  417. PageSortEvent();
  418. break;
  419. default:
  420. break;
  421. }
  422. if (AnnotationListItems.Count > 0)
  423. {
  424. IsEmptyPanelVisibility = Visibility.Collapsed;
  425. await Task.Delay(2);
  426. //展开数据
  427. ExpandGroupHeader(AnnotationListItems, listBox);
  428. }
  429. else
  430. {
  431. if (button != null)
  432. {
  433. button.IsEnabled = true;
  434. }
  435. IsEmptyPanelVisibility = Visibility.Visible;
  436. //AnnotationListItems.AddRange(currentAnnotationArgs);
  437. //RefreshAnnotationListItems();
  438. }
  439. }
  440. /// <summary>
  441. /// 根据条件筛选列表数据
  442. /// </summary>
  443. /// <param name="annotArgsTypes"></param>
  444. /// <param name="colors"></param>
  445. /// <param name="authors"></param>
  446. /// <returns></returns>
  447. private ObservableCollection<AnnotationHandlerEventArgs> SelectAnnotationListItems(List<AnnotArgsType> annotArgsTypes, List<string> colors, List<string> authors)
  448. {
  449. ObservableCollection<AnnotationHandlerEventArgs> annotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
  450. #region 类型 颜色 作者
  451. if (annotArgsTypes.Count > 0 && colors.Count > 0 && authors.Count > 0)
  452. {
  453. foreach (var item in CurrentAnnotationLists)
  454. {
  455. foreach (var type in annotArgsTypes)
  456. {
  457. foreach (var color in colors)
  458. {
  459. foreach (var author in authors)
  460. {
  461. if (item.EventType == type && item.Author == author)
  462. {
  463. SelectAnnotationColors(item, color, annotationArgs);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. #endregion 类型 颜色 作者
  471. #region 颜色 作者
  472. else if (colors.Count > 0 && authors.Count > 0)
  473. {
  474. foreach (var item in CurrentAnnotationLists)
  475. {
  476. foreach (var color in colors)
  477. {
  478. foreach (var author in authors)
  479. {
  480. if (item.Author == author)
  481. {
  482. SelectAnnotationColors(item, color, annotationArgs);
  483. }
  484. }
  485. }
  486. }
  487. }
  488. #endregion 颜色 作者
  489. #region 类型 作者
  490. else if (annotArgsTypes.Count > 0 && authors.Count > 0)
  491. {
  492. foreach (var item in CurrentAnnotationLists)
  493. {
  494. foreach (var type in annotArgsTypes)
  495. {
  496. foreach (var author in authors)
  497. {
  498. if (item.EventType == type && item.Author == author)
  499. {
  500. annotationArgs.Add(item);
  501. }
  502. }
  503. }
  504. }
  505. }
  506. #endregion 类型 作者
  507. #region 类型 颜色
  508. else if (annotArgsTypes.Count > 0 && colors.Count > 0)
  509. {
  510. foreach (var item in CurrentAnnotationLists)
  511. {
  512. foreach (var type in annotArgsTypes)
  513. {
  514. foreach (var color in colors)
  515. {
  516. if (item.EventType == type)
  517. {
  518. SelectAnnotationColors(item, color, annotationArgs);
  519. }
  520. }
  521. }
  522. }
  523. }
  524. #endregion 类型 颜色
  525. #region 类型
  526. else if (annotArgsTypes.Count > 0)
  527. {
  528. foreach (var item in CurrentAnnotationLists)
  529. {
  530. foreach (var type in annotArgsTypes)
  531. {
  532. if (item.EventType == type)
  533. {
  534. annotationArgs.Add(item);
  535. }
  536. }
  537. }
  538. }
  539. #endregion 类型
  540. #region 颜色
  541. else if (colors.Count > 0)
  542. {
  543. foreach (var item in CurrentAnnotationLists)
  544. {
  545. foreach (var color in colors)
  546. {
  547. SelectAnnotationColors(item, color, annotationArgs);
  548. }
  549. }
  550. }
  551. #endregion 颜色
  552. #region 作者
  553. else if (authors.Count > 0)
  554. {
  555. foreach (var item in CurrentAnnotationLists)
  556. {
  557. foreach (var author in authors)
  558. {
  559. if (item.Author == author)
  560. {
  561. annotationArgs.Add(item);
  562. }
  563. }
  564. }
  565. }
  566. #endregion 作者
  567. return annotationArgs;
  568. }
  569. /// <summary>
  570. /// 根据颜色筛选注释
  571. /// </summary>
  572. /// <param name="item"></param>
  573. /// <param name="color"></param>
  574. /// <param name="annotationArgs"></param>
  575. private void SelectAnnotationColors(AnnotationHandlerEventArgs item, string color, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  576. {
  577. AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs;
  578. switch (item.EventType)
  579. {
  580. case AnnotArgsType.AnnotFreeText://文本
  581. if (data is FreeTextAnnotArgs textAnnotArgs)
  582. {
  583. AddScreenAnnotationArgs(textAnnotArgs.FontColor, color, item, annotationArgs);
  584. }
  585. break;
  586. case AnnotArgsType.AnnotHighlight://高亮
  587. if (data is TextHighlightAnnotArgs highlightAnnotArgs)
  588. {
  589. AddScreenAnnotationArgs(highlightAnnotArgs.Color, color, item, annotationArgs);
  590. }
  591. break;
  592. case AnnotArgsType.AnnotFreehand://手绘
  593. if (data is FreehandAnnotArgs freehandAnnotArgs)
  594. {
  595. AddScreenAnnotationArgs(freehandAnnotArgs.InkColor, color, item, annotationArgs);
  596. }
  597. break;
  598. case AnnotArgsType.AnnotSquiggly://波浪线
  599. break;
  600. case AnnotArgsType.AnnotStamp://图章
  601. break;
  602. case AnnotArgsType.AnnotStrikeout://删除线
  603. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  604. {
  605. AddScreenAnnotationArgs(textStrikeoutAnnotArgs.Color, color, item, annotationArgs);
  606. }
  607. break;
  608. case AnnotArgsType.AnnotSticky://便签
  609. if (data is StickyAnnotArgs stickyAnnotArgs)
  610. {
  611. AddScreenAnnotationArgs(stickyAnnotArgs.Color, color, item, annotationArgs);
  612. }
  613. break;
  614. case AnnotArgsType.AnnotUnderline://下划线
  615. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  616. {
  617. AddScreenAnnotationArgs(textUnderlineAnnotArgs.Color, color, item, annotationArgs);
  618. }
  619. break;
  620. case AnnotArgsType.AnnotLine:
  621. //if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
  622. //{
  623. // //箭头
  624. //}
  625. //else
  626. //{
  627. // //线
  628. //}
  629. if (data is LineAnnotArgs lineAnnotArgs)
  630. {
  631. AddScreenAnnotationArgs(lineAnnotArgs.LineColor, color, item, annotationArgs);
  632. }
  633. break;
  634. case AnnotArgsType.AnnotSquare://矩形
  635. if (data is SquareAnnotArgs squareAnnotArgs)
  636. {
  637. AddScreenAnnotationArgs(squareAnnotArgs.LineColor, color, item, annotationArgs);
  638. AddScreenAnnotationArgs(squareAnnotArgs.BgColor, color, item, annotationArgs);
  639. }
  640. break;
  641. case AnnotArgsType.AnnotCircle://圆
  642. if (data is CircleAnnotArgs circleAnnotArgs)
  643. {
  644. AddScreenAnnotationArgs(circleAnnotArgs.LineColor, color, item, annotationArgs);
  645. AddScreenAnnotationArgs(circleAnnotArgs.BgColor, color, item, annotationArgs);
  646. }
  647. break;
  648. }
  649. }
  650. /// <summary>
  651. /// 对比颜色
  652. /// </summary>
  653. /// <param name="color1"></param>
  654. /// <param name="color"></param>
  655. /// <param name="item"></param>
  656. /// <param name="annotationArgs"></param>
  657. private void AddScreenAnnotationArgs(System.Windows.Media.Color color1, string color, AnnotationHandlerEventArgs item, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  658. {
  659. if (color1.ToString() == color)
  660. {
  661. annotationArgs.Add(item);
  662. }
  663. }
  664. /// <summary>
  665. /// 添加附注的注释
  666. /// </summary>
  667. /// <param name="obj"></param>
  668. private void AddNotesEvent(object obj)
  669. {
  670. if (obj is ListBoxItem listBoxItem)
  671. {
  672. if (listBoxItem.DataContext is AnnotationHandlerEventArgs args)
  673. {
  674. if (args.EventType != AnnotArgsType.AnnotSticky && args.EventType != AnnotArgsType.AnnotFreeText && args.EventType != AnnotArgsType.AnnotLink)
  675. {
  676. DialogParameters value = new DialogParameters();
  677. value.Add(ParameterNames.Annotation, args);
  678. dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
  679. {
  680. if (e.Result == ButtonResult.OK && e.Parameters != null)
  681. {
  682. PdfViewer.UndoManager.CanSave = true;
  683. if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
  684. {
  685. AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
  686. RefreshOneListItem(annotation.AnnotHandlerEventArgs);
  687. }
  688. }
  689. });
  690. }
  691. //else if (args.EventType == AnnotArgsType.AnnotFreeText)
  692. //{
  693. // StackPanel stackPanel = CommonHelper.FindVisualChild<StackPanel>(listBoxItem);
  694. // TextBox textBox = CommonHelper.FindVisualChild<TextBox>(stackPanel);
  695. // TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(stackPanel);
  696. // textBlock.Visibility = Visibility.Collapsed;
  697. // textBox.Visibility = Visibility.Visible;
  698. // textBox.Dispatcher.BeginInvoke(new Action(() =>
  699. // {
  700. // textBox.Focus();
  701. // textBox.SelectAll();
  702. // }));
  703. //}
  704. }
  705. }
  706. }
  707. private async void RefreshOneListItem(AnnotHandlerEventArgs annotation)
  708. {
  709. for (int i = 0; i < AnnotationListItems.Count; i++)
  710. {
  711. if (AnnotationListItems[i].PageIndex == annotation.PageIndex && AnnotationListItems[i].AnnotIndex == annotation.AnnotIndex)
  712. {
  713. AnnotationListItems[i] = GetAddAnnotEventArgs(annotation);
  714. for (int j = 0; j < CurrentAnnotationLists.Count; j++)
  715. {
  716. if (CurrentAnnotationLists[j].PageIndex == annotation.PageIndex && CurrentAnnotationLists[j].AnnotIndex == annotation.AnnotIndex)
  717. {
  718. CurrentAnnotationLists[j] = GetAddAnnotEventArgs(annotation);
  719. break;
  720. }
  721. }
  722. if (!listBox.SelectedItems.Contains(AnnotationListItems[i]))
  723. {
  724. listBox.SelectedItem = AnnotationListItems[i];
  725. }
  726. await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  727. ExpandGroupHeader(AnnotationListItems[i], listBox);
  728. await Task.Delay(1);//不加延时 会有不滚动的现象
  729. listBox.ScrollIntoView(AnnotationListItems[i]);
  730. break;
  731. }
  732. }
  733. }
  734. public async void ScrollToAnnot(int pageindex, int annotindex, ListBox AnnotationList)
  735. {
  736. //var list = PdfViewer.GetAnnotCommentList(pageindex, PdfViewer.Document);
  737. for (int i = 0; i < AnnotationList.Items.Count; i++)
  738. {
  739. if ((AnnotationList.Items[i] as AnnotationHandlerEventArgs).AnnotIndex == annotindex && (AnnotationList.Items[i] as AnnotationHandlerEventArgs).PageIndex == pageindex)
  740. {
  741. var item = AnnotationList.Items[i] as AnnotationHandlerEventArgs;
  742. //需要手动搜寻在哪一个分组 展开分组头
  743. ExpandGroupHeader(item, AnnotationList);
  744. AnnotationList.SelectedItem = item;
  745. await Task.Delay(1);
  746. AnnotationList.ScrollIntoView(item);
  747. }
  748. }
  749. }
  750. /// <summary>
  751. /// listboxitem鼠标左键点击,显示分组的数据
  752. /// </summary>
  753. /// <param name="obj"></param>
  754. private void ListBoxItem_PreviewMouseLeftButtonDown(object obj)
  755. {
  756. if (obj is ListBoxItem item)
  757. {
  758. var data = item.DataContext as AnnotationHandlerEventArgs;
  759. if (data != null)
  760. {
  761. PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex);
  762. }
  763. }
  764. if (obj is AnnotationHandlerEventArgs annotation)
  765. {
  766. PdfViewer.SelectAnnotation(annotation.PageIndex, annotation.AnnotIndex);
  767. }
  768. if (obj is Dictionary<int, List<int>> eventArgs)
  769. {
  770. PdfViewer.SelectAnnotation(eventArgs);
  771. }
  772. }
  773. public async void UpdateAddedAnnot(int pageIndex, int annotIndex)
  774. {
  775. var annots = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  776. for (int i = 0; i < annots.Count; i++)
  777. {
  778. AnnotHandlerEventArgs item = annots[i];
  779. if (item.AnnotIndex == annotIndex && item.EventType != AnnotArgsType.AnnotRedaction)
  780. {
  781. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  782. if (args.AnnotHandlerEventArgs != null)
  783. {
  784. AnnotationListItems.Add(args);
  785. CurrentAnnotationLists.Add(args);
  786. await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  787. ExpandGroupHeader(args, listBox);
  788. listBox.SelectedItem = item;
  789. await Task.Delay(5);//不加延时 会有不滚动的现象
  790. listBox.ScrollIntoView(item);
  791. if (!listBox.SelectedItems.Contains(args))
  792. {
  793. listBox.SelectedItem = args;
  794. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  795. }
  796. }
  797. }
  798. }
  799. listBox.UpdateLayout();
  800. //SetGroupHeader(listBox);
  801. if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
  802. {
  803. IsEmptyPanelVisibility = Visibility.Collapsed;
  804. }
  805. }
  806. public async void UpdateModifiedAnnot(int pageIndex, int annotIndex, bool isDelete)
  807. {
  808. try
  809. {
  810. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  811. if (isDelete && items.Count == 0)//当前页的最后一次删除
  812. {
  813. for (int k = 0; k < AnnotationListItems.Count; k++)
  814. {
  815. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  816. {
  817. AnnotationListItems.RemoveAt(k);
  818. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  819. CurrentAnnotationLists.Remove(annotation1);
  820. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  821. return;
  822. }
  823. }
  824. }
  825. if (isDelete)//删除
  826. {
  827. for (int k = 0; k < AnnotationListItems.Count; k++)
  828. {
  829. //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
  830. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  831. {
  832. AnnotationListItems.RemoveAt(k);
  833. AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  834. CurrentAnnotationLists.Remove(annotation1);
  835. UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  836. return;
  837. }
  838. }
  839. }
  840. for (int j = 0; j < items.Count; j++)
  841. {
  842. for (int k = 0; k < AnnotationListItems.Count; k++)
  843. {
  844. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex && items[j].AnnotIndex == annotIndex)
  845. {
  846. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
  847. AnnotationListItems[k] = GetAddAnnotEventArgs(items[j]);
  848. for (int i = 0; i < CurrentAnnotationLists.Count; i++)
  849. {
  850. if (CurrentAnnotationLists[i].PageIndex == pageIndex && CurrentAnnotationLists[i].AnnotIndex == annotIndex)
  851. {
  852. CurrentAnnotationLists[i] = GetAddAnnotEventArgs(items[j]);
  853. break;
  854. }
  855. }
  856. if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
  857. {
  858. listBox.SelectedItem = AnnotationListItems[k];
  859. //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  860. }
  861. await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  862. ExpandGroupHeader(AnnotationListItems[k], listBox);
  863. await Task.Delay(5);//不加延时 会有不滚动的现象
  864. listBox.ScrollIntoView(AnnotationListItems[k]);
  865. }
  866. }
  867. }
  868. listBox.UpdateLayout();
  869. //SetGroupHeader(listBox);
  870. }
  871. catch { }
  872. }
  873. /// <summary>
  874. /// 展开item所在分组
  875. /// </summary>
  876. private void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
  877. {
  878. try
  879. {
  880. var groups = listBox.Items.Groups;
  881. for (int i = 0; i < groups.Count; i++)
  882. {
  883. var l = groups[i] as CollectionViewGroup;
  884. if (l.Items.Contains(item))
  885. {
  886. var scroller = GetScrollHost(listBox);
  887. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  888. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  889. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  890. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  891. if (g != null)
  892. g.IsExpanded = true;
  893. }
  894. }
  895. }
  896. catch { }
  897. }
  898. /// <summary>
  899. /// 页面加载时
  900. /// </summary>
  901. /// <param name="obj"></param>
  902. private void Loaded(object obj)
  903. {
  904. if (obj is CompositeCommandParameter composite)
  905. {
  906. if (composite.Parameter is ListBox listBox)
  907. {
  908. this.listBox = listBox;
  909. SetGroupHeader(listBox);
  910. }
  911. }
  912. }
  913. public bool IsNavigationTarget(NavigationContext navigationContext)
  914. {
  915. return true;
  916. }
  917. public void OnNavigatedFrom(NavigationContext navigationContext)
  918. {
  919. }
  920. public void OnNavigatedTo(NavigationContext navigationContext)
  921. {
  922. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  923. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  924. if (pdfview != null && viewContentViewModel != null)
  925. {
  926. ViewContentViewModel = viewContentViewModel;
  927. PdfViewer = pdfview;
  928. }
  929. }
  930. /// <summary>
  931. /// 默认按照PageIndex,分组 排序
  932. /// </summary>
  933. /// <param name="listBox"></param>
  934. private async void SetGroupHeader(ListBox listBox)
  935. {
  936. #region BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  937. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  938. //CurrentAnnotationLists.AddRange(AnnotationListItems);
  939. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  940. //ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  941. //iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  942. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  943. ////iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  944. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.Y), ListSortDirection.Ascending));
  945. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.X), ListSortDirection.Ascending));
  946. #endregion BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  947. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  948. AnnotationListItems = GetDocumentAnnotionList();
  949. CurrentAnnotationLists.AddRange(AnnotationListItems);
  950. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  951. //按照PageIndex,分组 排序
  952. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  953. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  954. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  955. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  956. if (AnnotationListItems.Count > 0)
  957. {
  958. IsEmptyPanelVisibility = Visibility.Collapsed;
  959. await Task.Delay(5);
  960. //展开数据
  961. ExpandGroupHeader(AnnotationListItems, listBox);
  962. }
  963. else
  964. {
  965. IsEmptyPanelVisibility = Visibility.Visible;
  966. }
  967. }
  968. /// <summary>
  969. /// 展开列表项
  970. /// </summary>
  971. /// <param name="annotationListItems"></param>
  972. /// <param name="listBox"></param>
  973. private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  974. {
  975. try
  976. {
  977. foreach (var item in annotationListItems)
  978. {
  979. var groups = listBox.Items.Groups;
  980. for (int i = 0; i < groups.Count; i++)
  981. {
  982. var group = groups[i] as CollectionViewGroup;
  983. if (group.Items.Contains(item))
  984. {
  985. var scroller = GetScrollHost(listBox);
  986. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  987. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  988. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  989. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  990. if (g != null)
  991. {
  992. g.IsExpanded = true;
  993. }
  994. }
  995. }
  996. }
  997. }
  998. catch { }
  999. }
  1000. private ScrollViewer GetScrollHost(ListBox listBox)
  1001. {
  1002. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  1003. {
  1004. int s = VisualTreeHelper.GetChildrenCount(listBox);
  1005. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  1006. if (border != null)
  1007. {
  1008. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  1009. }
  1010. }
  1011. return null;
  1012. }
  1013. /// <summary>
  1014. /// 获取listbox绑定数据
  1015. /// </summary>
  1016. /// <returns></returns>
  1017. private ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList()
  1018. {
  1019. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  1020. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  1021. {
  1022. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  1023. foreach (var item in items)
  1024. {
  1025. //原型图上,目前对波浪线的类型,在注释列表不显示
  1026. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly)
  1027. {
  1028. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  1029. if (args.AnnotHandlerEventArgs != null)
  1030. {
  1031. list.Add(args);
  1032. }
  1033. }
  1034. }
  1035. }
  1036. return list;
  1037. }
  1038. private AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
  1039. {
  1040. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  1041. if (item.EventType == AnnotArgsType.AnnotFreehand)
  1042. {
  1043. WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  1044. args.WriteableBitmap = bitmap;
  1045. }
  1046. args.ClientRect = item.ClientRect;
  1047. args.AnnotHandlerEventArgs = item;
  1048. args.PageIndex = item.PageIndex;
  1049. args.AnnotIndex = item.AnnotIndex;
  1050. args.EventType = item.EventType;
  1051. args.CreateTime = item.CreateTime;
  1052. args.UpdateTime = item.UpdateTime;
  1053. args.Content = item.Content;
  1054. args.MarkupContent = item.MarkupContent;
  1055. args.Author = item.Author;
  1056. args.Locked = item.Locked;
  1057. args.ReadOnly = item.ReadOnly;
  1058. args.FormField = item.FormField;
  1059. args.Document = PdfViewer.Document;
  1060. return args;
  1061. }
  1062. /// <summary>
  1063. /// 获取手绘图案
  1064. /// </summary>
  1065. /// <param name="doc"></param>
  1066. /// <param name="pageIndex"></param>
  1067. /// <param name="annotIndex"></param>
  1068. /// <returns></returns>
  1069. public WriteableBitmap GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  1070. {
  1071. if (doc == null)
  1072. {
  1073. return null;
  1074. }
  1075. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  1076. if (docPage == null)
  1077. {
  1078. return null;
  1079. }
  1080. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  1081. foreach (CPDFAnnotation annot in docAnnots)
  1082. {
  1083. if (docAnnots.IndexOf(annot) == annotIndex)
  1084. {
  1085. CPDFInkAnnotation cPDFInk = (CPDFInkAnnotation)docAnnots[annotIndex];
  1086. //CRect rawRect = annot.GetRect();
  1087. CRect rawRect = cPDFInk.Rect;
  1088. double scaleDpi = 96.0 / 72.0;
  1089. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  1090. int drawWidth = (int)paintRect.Width;
  1091. int drawHeight = (int)paintRect.Height;
  1092. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  1093. annot.UpdateAp();
  1094. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  1095. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  1096. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  1097. cPDFInk.SetRect(rawRect);
  1098. return wirteBitmap;
  1099. }
  1100. }
  1101. return null;
  1102. }
  1103. /// <summary>
  1104. /// listbox的itemsource发生变化时
  1105. /// </summary>
  1106. /// <param name="sender"></param>
  1107. /// <param name="e"></param>
  1108. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  1109. {
  1110. if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
  1111. {
  1112. //currentAnnotationArgs.Clear();
  1113. //currentAnnotationArgs.AddRange(obsSender);
  1114. if (obsSender.Count < 1)
  1115. {
  1116. IsEmptyPanelVisibility = Visibility.Visible;
  1117. //currentAnnotationArgs.Clear();
  1118. }
  1119. else
  1120. {
  1121. IsEmptyPanelVisibility = Visibility.Collapsed;
  1122. }
  1123. }
  1124. else
  1125. {
  1126. IsEmptyPanelVisibility = Visibility.Visible;
  1127. }
  1128. }
  1129. }
  1130. }