AnnotationContentViewModel.cs 70 KB

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