AnnotationContentViewModel.cs 67 KB

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