AnnotationContentViewModel.cs 64 KB

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