AnnotationContentViewModel.cs 79 KB

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