AnnotationContentViewModel.cs 56 KB

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