AnnotationContentViewModel.cs 61 KB

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