AnnotationContentViewModel.cs 77 KB

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