AnnotationContentViewModel.cs 75 KB

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