AnnotationContentViewModel.cs 61 KB

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