AnnotationContentViewModel.cs 58 KB

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