AnnotationContentViewModel.cs 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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. //按照PageIndex,分组 排序
  1315. SetGroupHeader(listBox);
  1316. Screen = Visibility.Collapsed;
  1317. colors.Clear();
  1318. authors.Clear();
  1319. annotArgsTypes.Clear();
  1320. }
  1321. if (viewContentViewModel.IsHiddenAnnot)
  1322. {
  1323. HiddenAnnot();
  1324. }
  1325. else
  1326. {
  1327. DisplayAnnot();
  1328. }
  1329. AnnotationListItems.CollectionChanged -= AnnotationListItems_CollectionChanged;
  1330. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1331. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1332. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  1333. }
  1334. private void RefreshData(RefreshAnnotArgs obj)
  1335. {
  1336. if (obj.Annotation != null)
  1337. {
  1338. RefreshOneListItem(obj.Annotation.AnnotHandlerEventArgs);
  1339. }
  1340. }
  1341. private void ShortCut_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  1342. {
  1343. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  1344. {
  1345. if (listBox != null)
  1346. {
  1347. if (listBox.SelectedItems == null)
  1348. {
  1349. return;
  1350. }
  1351. //Windows:BOTA列表,移除Esc取消选中逻辑
  1352. //listBox.SelectedItems.Clear();
  1353. }
  1354. }
  1355. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Delete))
  1356. {
  1357. if (listBox != null)
  1358. {
  1359. if (listBox.SelectedItems == null)
  1360. {
  1361. return;
  1362. }
  1363. List<int> pagelist = new List<int>();
  1364. for (int i = 0; i < listBox.SelectedItems.Count; i++)
  1365. {
  1366. AnnotationHandlerEventArgs annotation = listBox.SelectedItems[i] as AnnotationHandlerEventArgs;
  1367. pagelist.Add(listBox.Items.IndexOf(annotation));
  1368. }
  1369. pagelist.Sort();
  1370. for (int i = 0; i < pagelist.Count; i++)
  1371. {
  1372. AnnotationHandlerEventArgs annotation = listBox.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  1373. if (annotation == null)
  1374. {
  1375. continue;
  1376. }
  1377. DeleteCommand.Execute(annotation);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. /// <summary>
  1383. /// 默认按照PageIndex,分组 排序
  1384. /// </summary>
  1385. /// <param name="listBox"></param>
  1386. private void SetGroupHeader(ListBox listBox)
  1387. {
  1388. if (listBox == null) { return; }
  1389. #region BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  1390. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  1391. //CurrentAnnotationLists.AddRange(AnnotationListItems);
  1392. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1393. //ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  1394. //iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  1395. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  1396. ////iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  1397. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.Y), ListSortDirection.Ascending));
  1398. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.X), ListSortDirection.Ascending));
  1399. #endregion BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  1400. if (AnnotationListItems == null)
  1401. {
  1402. return;
  1403. }
  1404. if (AnnotationListItems.Count > 0)
  1405. {
  1406. IsEmptyPanelVisibility = Visibility.Collapsed;
  1407. //await Task.Delay(5);
  1408. //展开数据
  1409. //ExpandGroupHeader(AnnotationListItems, listBox);
  1410. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = true });
  1411. }
  1412. else
  1413. {
  1414. if (CurrentAnnotationLists.Count <= 0 && HideAnnots.Count > 1)
  1415. {
  1416. IsEmptyPanelVisibility = Visibility.Visible;
  1417. IsHideAnnotPanelVisibility = Visibility.Visible;
  1418. }
  1419. else
  1420. {
  1421. IsEmptyPanelVisibility = Visibility.Visible;
  1422. IsHideAnnotPanelVisibility = Visibility.Visible;
  1423. }
  1424. }
  1425. }
  1426. /// <summary>
  1427. /// 展开列表项
  1428. /// </summary>
  1429. /// <param name="annotationListItems"></param>
  1430. /// <param name="listBox"></param>
  1431. private async void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  1432. {
  1433. try
  1434. {
  1435. foreach (var item in annotationListItems)
  1436. {
  1437. var groups = listBox.Items.Groups;
  1438. for (int i = 0; i < groups.Count; i++)
  1439. {
  1440. var group = groups[i] as CollectionViewGroup;
  1441. if (group.Items.Contains(item))
  1442. {
  1443. var scroller = GetScrollHost(listBox);
  1444. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  1445. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  1446. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  1447. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  1448. if (g != null)
  1449. {
  1450. await Task.Delay(5);
  1451. g.IsExpanded = true;
  1452. }
  1453. }
  1454. }
  1455. }
  1456. }
  1457. catch { }
  1458. }
  1459. private ScrollViewer GetScrollHost(ListBox listBox)
  1460. {
  1461. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  1462. {
  1463. int s = VisualTreeHelper.GetChildrenCount(listBox);
  1464. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  1465. if (border != null)
  1466. {
  1467. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  1468. }
  1469. }
  1470. return null;
  1471. }
  1472. /// <summary>
  1473. /// 获取listbox绑定数据
  1474. /// </summary>
  1475. /// <returns></returns>
  1476. public ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList(out ObservableCollection<AnnotationHandlerEventArgs> HideAnnots)
  1477. {
  1478. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  1479. HideAnnots = new ObservableCollection<AnnotationHandlerEventArgs>();
  1480. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  1481. {
  1482. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  1483. foreach (var item in items)
  1484. {
  1485. //原型图上,目前对波浪线的类型,在注释列表不显示
  1486. //注释列表 不显示链接注释
  1487. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly
  1488. && item.EventType != AnnotArgsType.AnnotLink)
  1489. {
  1490. if (item.EventType == AnnotArgsType.AnnotLink)
  1491. {
  1492. if (item is LinkAnnotArgs link)
  1493. {
  1494. if (link.LinkType == LINK_TYPE.URI)
  1495. {
  1496. continue;
  1497. }
  1498. }
  1499. }
  1500. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  1501. if (args != null)
  1502. {
  1503. bool isVisible = PdfViewer.CheckAnnotVisible(args.PageIndex, args.AnnotIndex);
  1504. if (args.AnnotHandlerEventArgs != null)
  1505. {
  1506. if (isVisible)
  1507. {
  1508. list.Add(args);
  1509. }
  1510. else
  1511. {
  1512. HideAnnots.Add(args);
  1513. }
  1514. }
  1515. }
  1516. }
  1517. }
  1518. }
  1519. return list;
  1520. }
  1521. public AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
  1522. {
  1523. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  1524. //原型图上,目前对波浪线的类型,在注释列表不显示
  1525. //注释列表 不显示链接注释
  1526. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly
  1527. && item.EventType != AnnotArgsType.AnnotLink)
  1528. {
  1529. if (item.EventType == AnnotArgsType.AnnotFreehand)
  1530. {
  1531. BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  1532. if (bitmap != null)
  1533. args.WriteableBitmap = bitmap;
  1534. }
  1535. args.ClientRect = item.ClientRect;
  1536. args.AnnotHandlerEventArgs = item;
  1537. args.PageIndex = item.PageIndex;
  1538. args.AnnotIndex = item.AnnotIndex;
  1539. args.EventType = item.EventType;
  1540. args.CreateTime = item.CreateTime;
  1541. args.UpdateTime = item.UpdateTime;
  1542. args.Content = item.Content;
  1543. args.MarkupContent = item.MarkupContent;
  1544. args.Author = item.Author;
  1545. args.Locked = item.Locked;
  1546. args.ReadOnly = item.ReadOnly;
  1547. args.FormField = item.FormField;
  1548. args.Document = PdfViewer.Document;
  1549. }
  1550. return args;
  1551. }
  1552. /// <summary>
  1553. /// 获取手绘图案
  1554. /// </summary>
  1555. /// <param name="doc"></param>
  1556. /// <param name="pageIndex"></param>
  1557. /// <param name="annotIndex"></param>
  1558. /// <returns></returns>
  1559. public BitmapImage GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  1560. {
  1561. if (doc == null)
  1562. {
  1563. return null;
  1564. }
  1565. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  1566. if (docPage == null)
  1567. {
  1568. return null;
  1569. }
  1570. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  1571. foreach (CPDFAnnotation annot in docAnnots)
  1572. {
  1573. if (docAnnots.IndexOf(annot) == annotIndex)
  1574. {
  1575. CPDFInkAnnotation cPDFInk = (CPDFInkAnnotation)docAnnots[annotIndex];
  1576. //CRect rawRect = annot.GetRect();
  1577. CRect rawRect = cPDFInk.Rect;
  1578. double scaleDpi = 96.0 / 72.0;
  1579. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  1580. int drawWidth = (int)paintRect.Width;
  1581. int drawHeight = (int)paintRect.Height;
  1582. switch (docPage.Rotation)
  1583. {
  1584. case 0:
  1585. case 2:
  1586. drawWidth = (int)paintRect.Width;
  1587. drawHeight = (int)paintRect.Height;
  1588. break;
  1589. case 1:
  1590. case 3:
  1591. drawWidth = (int)paintRect.Height;
  1592. drawHeight = (int)paintRect.Width;
  1593. break;
  1594. }
  1595. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  1596. annot.UpdateAp();
  1597. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  1598. BitmapImage bitmapImage = null;
  1599. if (drawWidth != 0 && drawHeight != 0)
  1600. {
  1601. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  1602. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  1603. bitmapImage = ConvertWriteableBitmapToBitmapImage(wirteBitmap, docPage.Rotation);
  1604. }
  1605. else
  1606. {
  1607. bitmapImage = null;
  1608. }
  1609. cPDFInk.SetRect(rawRect);
  1610. // 测试
  1611. //using (FileStream stream = new FileStream(@"C:\Users\oyxh\Desktop\images\1.png", FileMode.Create))
  1612. //{
  1613. // PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
  1614. // bitmapEncoder.Frames.Add(BitmapFrame.Create(wirteBitmap));
  1615. // bitmapEncoder.Save(stream);
  1616. //}
  1617. return bitmapImage;
  1618. }
  1619. }
  1620. return null;
  1621. }
  1622. public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm, int rotation)
  1623. {
  1624. BitmapImage bmImage = new BitmapImage();
  1625. using (MemoryStream stream = new MemoryStream())
  1626. {
  1627. PngBitmapEncoder encoder = new PngBitmapEncoder();
  1628. encoder.Frames.Add(BitmapFrame.Create(wbm));
  1629. encoder.Save(stream);
  1630. bmImage.BeginInit();
  1631. bmImage.CacheOption = BitmapCacheOption.OnLoad;
  1632. bmImage.StreamSource = stream;
  1633. switch (rotation)
  1634. {
  1635. case 0:
  1636. bmImage.Rotation = Rotation.Rotate0;
  1637. break;
  1638. case 1:
  1639. bmImage.Rotation = Rotation.Rotate90;
  1640. break;
  1641. case 2:
  1642. bmImage.Rotation = Rotation.Rotate180;
  1643. break;
  1644. case 3:
  1645. bmImage.Rotation = Rotation.Rotate270;
  1646. break;
  1647. }
  1648. bmImage.EndInit();
  1649. bmImage.Freeze();
  1650. }
  1651. return bmImage;
  1652. }
  1653. /// <summary>
  1654. /// listbox的itemsource发生变化时
  1655. /// </summary>
  1656. /// <param name="sender"></param>
  1657. /// <param name="e"></param>
  1658. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  1659. {
  1660. if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
  1661. {
  1662. //currentAnnotationArgs.Clear();
  1663. //currentAnnotationArgs.AddRange(obsSender);
  1664. if (obsSender.Count < 1)
  1665. {
  1666. if (CurrentAnnotationLists.Count <= 0)
  1667. {
  1668. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1669. IsEmptyPanelVisibility = Visibility.Visible;
  1670. }
  1671. else
  1672. {
  1673. if (isHiddenAnnot)
  1674. {
  1675. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1676. IsEmptyPanelVisibility = Visibility.Visible;
  1677. }
  1678. else
  1679. {
  1680. IsEmptyPanelVisibility = Visibility.Visible;
  1681. IsHideAnnotPanelVisibility = Visibility.Visible;
  1682. }
  1683. }
  1684. //IsEmptyPanelVisibility = Visibility.Visible;
  1685. }
  1686. else
  1687. {
  1688. IsEmptyPanelVisibility = Visibility.Collapsed;
  1689. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1690. }
  1691. }
  1692. else
  1693. {
  1694. IsEmptyPanelVisibility = Visibility.Visible;
  1695. }
  1696. }
  1697. }
  1698. }