AnnotationContentViewModel.cs 70 KB

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