AnnotationContentViewModel.cs 73 KB

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