AnnotationContentViewModel.cs 66 KB

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