AnnotationContentViewModel.cs 66 KB

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