AnnotationContentViewModel.cs 78 KB

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