AnnotationContentViewModel.cs 60 KB

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