AnnotationContentViewModel.cs 78 KB

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