AnnotationContentViewModel.cs 68 KB

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