AnnotationContentViewModel.cs 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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. //筛选注释
  244. private List<string> colors = new List<string>();
  245. private List<string> authors = new List<string>();
  246. private List<AnnotArgsType> annotArgsTypes = new List<AnnotArgsType>();
  247. public DelegateCommand<object> LoadedCommand { get; set; }
  248. public DelegateCommand<object> ListBoxItemPreviewMouseLeftButtonDown { get; set; }
  249. public DelegateCommand<object> AddNotesCommand { get; set; }
  250. public DelegateCommand<object> ScreenCommand { get; set; }
  251. public DelegateCommand ExportCommentsCommand { get; set; }
  252. public DelegateCommand ImportCommentsCommand { get; set; }
  253. public DelegateCommand<object> DeleteCommand { get; set; }
  254. public DelegateCommand DeleteAllCommand { get; set; }
  255. public DelegateCommand PageSortCommand { get; set; }
  256. public DelegateCommand TimeAscendingCommand { get; set; }
  257. public DelegateCommand TimeDescendingCommand { get; set; }
  258. public string Unicode { get; private set; }
  259. private bool IsFirst = true;
  260. private bool isHiddenAnnot = false;
  261. //判断是否为属性修改进入,属性修改,改变后不让再次其选中注释
  262. public bool IsModified = true;
  263. public AnnotationContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  264. {
  265. region = regionManager;
  266. dialogs = dialogService;
  267. events = eventAggregator;
  268. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  269. LoadedCommand = new DelegateCommand<object>(Loaded);
  270. ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand<object>(ListBoxItem_PreviewMouseLeftButtonDown);
  271. AddNotesCommand = new DelegateCommand<object>(AddNotesEvent);
  272. ScreenCommand = new DelegateCommand<object>(FilterCommentsEvent);
  273. ExportCommentsCommand = new DelegateCommand(ExportCommentsEvent, CanIsEnabled).ObservesProperty(() => AnnotationListItems);
  274. ImportCommentsCommand = new DelegateCommand(ImportCommentsEvent);
  275. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  276. DeleteAllCommand = new DelegateCommand(DeleteAllEvent, CanIsEnabled).ObservesProperty(() => AnnotationListItems);
  277. PageSortCommand = new DelegateCommand(PageSortEvent);
  278. TimeAscendingCommand = new DelegateCommand(TimeAscendingEvent);
  279. TimeDescendingCommand = new DelegateCommand(TimeDescendingEvent);
  280. InitString();
  281. events.GetEvent<RefreshAnnotEvent>().Subscribe(RefreshData, e => e.Unicode == Unicode);
  282. events.GetEvent<PageEditNotifyEvent>().Subscribe(OnNotifyEvent, e => e.Unicode == Unicode && e.Type == NotifyType.PageCountChanged);
  283. }
  284. private void OnNotifyEvent(PageEditNotifyEventArgs obj)
  285. {
  286. RefreshAnnotationListItems();
  287. }
  288. public void HiddenAnnot()
  289. {
  290. isHiddenAnnot = true;
  291. colors.Clear();
  292. authors.Clear();
  293. annotArgsTypes.Clear();
  294. AnnotationListItems.Clear();
  295. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  296. AnnotationListItems.AddRange(CurrentAnnotationLists);
  297. Screen = Visibility.Collapsed;
  298. }
  299. public void DisplayAnnot()
  300. {
  301. isHiddenAnnot = false;
  302. colors.Clear();
  303. authors.Clear();
  304. annotArgsTypes.Clear();
  305. AnnotationListItems.Clear();
  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. //删除多个
  443. if (obj is Dictionary<int, List<int>> dics)
  444. {
  445. foreach (KeyValuePair<int, List<int>> kvp in dics)
  446. {
  447. int key = kvp.Key;
  448. List<int> list = kvp.Value;
  449. foreach (var item in list)
  450. {
  451. var annotation2 = AnnotationListItems.FirstOrDefault(x => x.PageIndex == key && x.AnnotIndex == item);
  452. if (annotation2 != null)
  453. {
  454. AnnotationListItems.Remove(annotation2);
  455. }
  456. var annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == key && x.AnnotIndex == item);
  457. if (annotation1 != null)
  458. {
  459. CurrentAnnotationLists.Remove(annotation1);
  460. }
  461. if (annotation2 != null && annotation1 != null)
  462. {
  463. UpdateAnnotListAfterDelete(key, item);
  464. }
  465. }
  466. }
  467. PdfViewer.UndoManager.CanSave = true;
  468. PdfViewer.RemovePageAnnot(dics);
  469. }
  470. }
  471. public void RemoveItem(AnnotationHandlerEventArgs annotation)
  472. {
  473. AnnotationListItems.Remove(annotation);
  474. var annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == annotation.PageIndex && x.AnnotIndex == annotation.AnnotIndex);
  475. CurrentAnnotationLists.Remove(annotation1);
  476. //记录是删除了哪些页面的注释,然后更新对应页面的注释即可
  477. UpdateAnnotListAfterDelete(annotation.PageIndex, annotation.AnnotIndex);
  478. PdfViewer.UndoManager.CanSave = true;
  479. }
  480. /// <summary>
  481. /// 删除之后,更新
  482. /// </summary>
  483. /// <param name="pageIndex"></param>
  484. /// <param name="annoteIndex"></param>
  485. private void UpdateAnnotListAfterDelete(int pageIndex, int annoteIndex)
  486. {
  487. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  488. for (int j = 0; j < items.Count; j++)//用修改赋值的方式 可以解决删除后表头折叠的问题
  489. {
  490. if (items[j].AnnotIndex >= annoteIndex)//只需要更新比删除元素索引大的注释
  491. {
  492. for (int k = 0; k < AnnotationListItems.Count; k++)
  493. {
  494. //相当于将后面的索引-1
  495. if (AnnotationListItems[k].PageIndex == pageIndex &&
  496. AnnotationListItems[k].AnnotIndex == (items[j].AnnotIndex + 1) &&
  497. string.Equals(AnnotationListItems[k].MarkupContent, items[j].MarkupContent) &&
  498. string.Equals(AnnotationListItems[k].Content, items[j].Content) &&
  499. string.Equals(AnnotationListItems[k].CreateTime, items[j].CreateTime) &&
  500. string.Equals(AnnotationListItems[k].Author, items[j].Author)
  501. )
  502. {
  503. AnnotationListItems[k].AnnotHandlerEventArgs = items[j];
  504. AnnotationListItems[k].PageIndex = items[j].PageIndex;
  505. AnnotationListItems[k].AnnotIndex = items[j].AnnotIndex;
  506. AnnotationListItems[k].EventType = items[j].EventType;
  507. AnnotationListItems[k].CreateTime = items[j].CreateTime;
  508. AnnotationListItems[k].UpdateTime = items[j].UpdateTime;
  509. if (items[j].EventType == AnnotArgsType.AnnotFreehand)
  510. {
  511. BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, items[j].PageIndex, items[j].AnnotIndex);
  512. if (bitmap != null)
  513. AnnotationListItems[k].WriteableBitmap = bitmap;
  514. }
  515. AnnotationListItems[k].Content = items[j].Content;
  516. AnnotationListItems[k].MarkupContent = items[j].MarkupContent;
  517. AnnotationListItems[k].Author = items[j].Author;
  518. AnnotationListItems[k].Locked = items[j].Locked;
  519. AnnotationListItems[k].ReadOnly = items[j].ReadOnly;
  520. AnnotationListItems[k].FormField = items[j].FormField;
  521. AnnotationListItems[k].Document = PdfViewer.Document;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. /// <summary>
  528. /// 导出注释
  529. /// </summary>
  530. /// <param name="obj"></param>
  531. private void ExportCommentsEvent()
  532. {
  533. if (ViewContentViewModel.CanSave)
  534. {
  535. ViewContentViewModel.SaveFile.Execute();
  536. }
  537. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  538. dlg.Filter = "PDF|*.xfdf";
  539. dlg.DefaultExt = ".xfdf";
  540. dlg.FileName = PdfViewer.Document.FileName;
  541. if (dlg.ShowDialog() == true)
  542. {
  543. string fileName = dlg.FileName;
  544. var result = PdfViewer.ExportAnnotationToXFDFPath(fileName);
  545. if (result)
  546. {
  547. AlertsMessage alertsMessage = new AlertsMessage();
  548. alertsMessage.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportSuccess"), App.ServiceLoader.GetString("Text_ok"));
  549. }
  550. else
  551. {
  552. AlertsMessage alertsMessage = new AlertsMessage();
  553. alertsMessage.ShowDialog(App.MainPageLoader.GetString("Dialog_Hint"), App.MainPageLoader.GetString("Annot_ExportSuccess"), App.ServiceLoader.GetString("Text_ok"));
  554. }
  555. }
  556. }
  557. /// <summary>
  558. /// 筛选注释
  559. /// </summary>
  560. /// <param name="obj"></param>
  561. private void FilterCommentsEvent(object obj)
  562. {
  563. if (obj is System.Windows.Controls.Button button)
  564. {
  565. DialogParameters value = new DialogParameters();
  566. value.Add(ParameterNames.AnnotationList, CurrentAnnotationLists);
  567. value.Add(ParameterNames.AnnotArgsTypes, annotArgsTypes);
  568. value.Add(ParameterNames.AnnotationColors, colors);
  569. value.Add(ParameterNames.AnnotationAuthor, authors);
  570. dialogs.ShowDialog(DialogNames.ScreenAnnotationDialog, value, e =>
  571. {
  572. if (e.Result == ButtonResult.OK && e.Parameters != null)
  573. {
  574. if (e.Parameters.ContainsKey(ParameterNames.AnnotArgsTypes))
  575. {
  576. annotArgsTypes = e.Parameters.GetValue<List<AnnotArgsType>>(ParameterNames.AnnotArgsTypes);
  577. }
  578. if (e.Parameters.ContainsKey(ParameterNames.AnnotationColors))
  579. {
  580. colors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationColors);
  581. }
  582. if (e.Parameters.ContainsKey(ParameterNames.AnnotationAuthor))
  583. {
  584. authors = e.Parameters.GetValue<List<string>>(ParameterNames.AnnotationAuthor);
  585. }
  586. if (annotArgsTypes.Count > 0 || colors.Count > 0 || authors.Count > 0)
  587. {
  588. ObservableCollection<AnnotationHandlerEventArgs> screenAnnotationArgs = SelectAnnotationListItems(annotArgsTypes, colors, authors);
  589. AnnotationListItems.Clear();
  590. AnnotationListItems.AddRange(screenAnnotationArgs);
  591. //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  592. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  593. RefreshAnnotationListItems(button);
  594. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  595. Screen = Visibility.Visible;
  596. }
  597. else
  598. {
  599. CleanAnnotation(button);
  600. Screen = Visibility.Collapsed;
  601. }
  602. }
  603. else if (e.Result == ButtonResult.Cancel && e.Parameters != null)
  604. {
  605. //CleanAnnotation(button);
  606. }
  607. });
  608. }
  609. }
  610. private void CleanAnnotation(System.Windows.Controls.Button button)
  611. {
  612. colors.Clear();
  613. authors.Clear();
  614. annotArgsTypes.Clear();
  615. AnnotationListItems.Clear();
  616. AnnotationListItems.AddRange(CurrentAnnotationLists);
  617. //BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  618. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  619. RefreshAnnotationListItems(button);
  620. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  621. }
  622. /// <summary>
  623. /// 刷新列表数据
  624. /// </summary>
  625. /// <param name="button"></param>
  626. public async void RefreshAnnotationListItems(System.Windows.Controls.Button button = null)
  627. {
  628. switch (AnnotationSortOrder)
  629. {
  630. case AnnotationSortOrder.TimeAscending:
  631. TimeAscendingEvent();
  632. break;
  633. case AnnotationSortOrder.TimeDescending:
  634. TimeDescendingEvent();
  635. break;
  636. case AnnotationSortOrder.PageIndexAscending:
  637. PageSortEvent();
  638. break;
  639. default:
  640. break;
  641. }
  642. if (AnnotationListItems == null) return;
  643. if (AnnotationListItems.Count > 0)
  644. {
  645. IsEmptyPanelVisibility = Visibility.Collapsed;
  646. await Task.Delay(5);
  647. ////展开数据
  648. //ExpandGroupHeader(AnnotationListItems, listBox);
  649. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = true });
  650. }
  651. else
  652. {
  653. if (button != null)
  654. {
  655. button.IsEnabled = true;
  656. }
  657. IsEmptyPanelVisibility = Visibility.Visible;
  658. IsHideAnnotPanelVisibility = Visibility.Visible;
  659. }
  660. }
  661. /// <summary>
  662. /// 根据条件筛选列表数据
  663. /// </summary>
  664. /// <param name="annotArgsTypes"></param>
  665. /// <param name="colors"></param>
  666. /// <param name="authors"></param>
  667. /// <returns></returns>
  668. private ObservableCollection<AnnotationHandlerEventArgs> SelectAnnotationListItems(List<AnnotArgsType> annotArgsTypes, List<string> colors, List<string> authors)
  669. {
  670. ObservableCollection<AnnotationHandlerEventArgs> annotationArgs = new ObservableCollection<AnnotationHandlerEventArgs>();
  671. #region 类型 颜色 作者
  672. if (annotArgsTypes.Count > 0 && colors.Count > 0 && authors.Count > 0)
  673. {
  674. foreach (var item in CurrentAnnotationLists)
  675. {
  676. foreach (var type in annotArgsTypes)
  677. {
  678. foreach (var color in colors)
  679. {
  680. foreach (var author in authors)
  681. {
  682. if (item.EventType == type && item.Author == author)
  683. {
  684. SelectAnnotationColors(item, color, annotationArgs);
  685. }
  686. }
  687. }
  688. }
  689. }
  690. }
  691. #endregion 类型 颜色 作者
  692. #region 颜色 作者
  693. else if (colors.Count > 0 && authors.Count > 0)
  694. {
  695. foreach (var item in CurrentAnnotationLists)
  696. {
  697. foreach (var color in colors)
  698. {
  699. foreach (var author in authors)
  700. {
  701. if (item.Author == author)
  702. {
  703. SelectAnnotationColors(item, color, annotationArgs);
  704. }
  705. }
  706. }
  707. }
  708. }
  709. #endregion 颜色 作者
  710. #region 类型 作者
  711. else if (annotArgsTypes.Count > 0 && authors.Count > 0)
  712. {
  713. foreach (var item in CurrentAnnotationLists)
  714. {
  715. foreach (var type in annotArgsTypes)
  716. {
  717. foreach (var author in authors)
  718. {
  719. if (item.EventType == type && item.Author == author)
  720. {
  721. annotationArgs.Add(item);
  722. }
  723. }
  724. }
  725. }
  726. }
  727. #endregion 类型 作者
  728. #region 类型 颜色
  729. else if (annotArgsTypes.Count > 0 && colors.Count > 0)
  730. {
  731. foreach (var item in CurrentAnnotationLists)
  732. {
  733. foreach (var type in annotArgsTypes)
  734. {
  735. foreach (var color in colors)
  736. {
  737. if (item.EventType == type)
  738. {
  739. SelectAnnotationColors(item, color, annotationArgs);
  740. }
  741. }
  742. }
  743. }
  744. }
  745. #endregion 类型 颜色
  746. #region 类型
  747. else if (annotArgsTypes.Count > 0)
  748. {
  749. foreach (var item in CurrentAnnotationLists)
  750. {
  751. foreach (var type in annotArgsTypes)
  752. {
  753. if (item.EventType == type)
  754. {
  755. annotationArgs.Add(item);
  756. }
  757. }
  758. }
  759. }
  760. #endregion 类型
  761. #region 颜色
  762. else if (colors.Count > 0)
  763. {
  764. foreach (var item in CurrentAnnotationLists)
  765. {
  766. foreach (var color in colors)
  767. {
  768. SelectAnnotationColors(item, color, annotationArgs);
  769. }
  770. }
  771. }
  772. #endregion 颜色
  773. #region 作者
  774. else if (authors.Count > 0)
  775. {
  776. foreach (var item in CurrentAnnotationLists)
  777. {
  778. foreach (var author in authors)
  779. {
  780. if (item.Author == author)
  781. {
  782. annotationArgs.Add(item);
  783. }
  784. }
  785. }
  786. }
  787. #endregion 作者
  788. return annotationArgs;
  789. }
  790. /// <summary>
  791. /// 根据颜色筛选注释
  792. /// </summary>
  793. /// <param name="item"></param>
  794. /// <param name="color"></param>
  795. /// <param name="annotationArgs"></param>
  796. private void SelectAnnotationColors(AnnotationHandlerEventArgs item, string color, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  797. {
  798. AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs;
  799. switch (item.EventType)
  800. {
  801. case AnnotArgsType.AnnotFreeText://文本
  802. if (data is FreeTextAnnotArgs textAnnotArgs)
  803. {
  804. AddScreenAnnotationArgs(textAnnotArgs.FontColor, color, item, annotationArgs);
  805. }
  806. break;
  807. case AnnotArgsType.AnnotHighlight://高亮
  808. if (data is TextHighlightAnnotArgs highlightAnnotArgs)
  809. {
  810. AddScreenAnnotationArgs(highlightAnnotArgs.Color, color, item, annotationArgs);
  811. }
  812. break;
  813. case AnnotArgsType.AnnotFreehand://手绘
  814. if (data is FreehandAnnotArgs freehandAnnotArgs)
  815. {
  816. AddScreenAnnotationArgs(freehandAnnotArgs.InkColor, color, item, annotationArgs);
  817. }
  818. break;
  819. case AnnotArgsType.AnnotSquiggly://波浪线
  820. break;
  821. case AnnotArgsType.AnnotStamp://图章
  822. break;
  823. case AnnotArgsType.AnnotStrikeout://删除线
  824. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  825. {
  826. AddScreenAnnotationArgs(textStrikeoutAnnotArgs.Color, color, item, annotationArgs);
  827. }
  828. break;
  829. case AnnotArgsType.AnnotSticky://便签
  830. if (data is StickyAnnotArgs stickyAnnotArgs)
  831. {
  832. AddScreenAnnotationArgs(stickyAnnotArgs.Color, color, item, annotationArgs);
  833. }
  834. break;
  835. case AnnotArgsType.AnnotUnderline://下划线
  836. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  837. {
  838. AddScreenAnnotationArgs(textUnderlineAnnotArgs.Color, color, item, annotationArgs);
  839. }
  840. break;
  841. case AnnotArgsType.AnnotLine:
  842. //if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
  843. //{
  844. // //箭头
  845. //}
  846. //else
  847. //{
  848. // //线
  849. //}
  850. if (data is LineAnnotArgs lineAnnotArgs)
  851. {
  852. AddScreenAnnotationArgs(lineAnnotArgs.LineColor, color, item, annotationArgs);
  853. }
  854. break;
  855. case AnnotArgsType.AnnotSquare://矩形
  856. if (data is SquareAnnotArgs squareAnnotArgs)
  857. {
  858. AddScreenAnnotationArgs(squareAnnotArgs.LineColor, color, item, annotationArgs);
  859. AddScreenAnnotationArgs(squareAnnotArgs.BgColor, color, item, annotationArgs);
  860. }
  861. break;
  862. case AnnotArgsType.AnnotCircle://圆
  863. if (data is CircleAnnotArgs circleAnnotArgs)
  864. {
  865. AddScreenAnnotationArgs(circleAnnotArgs.LineColor, color, item, annotationArgs);
  866. AddScreenAnnotationArgs(circleAnnotArgs.BgColor, color, item, annotationArgs);
  867. }
  868. break;
  869. }
  870. }
  871. /// <summary>
  872. /// 对比颜色
  873. /// </summary>
  874. /// <param name="color1"></param>
  875. /// <param name="color"></param>
  876. /// <param name="item"></param>
  877. /// <param name="annotationArgs"></param>
  878. private void AddScreenAnnotationArgs(System.Windows.Media.Color color1, string color, AnnotationHandlerEventArgs item, ObservableCollection<AnnotationHandlerEventArgs> annotationArgs)
  879. {
  880. if (color1.ToString() == color)
  881. {
  882. annotationArgs.Add(item);
  883. }
  884. }
  885. /// <summary>
  886. /// 添加附注的注释
  887. /// </summary>
  888. /// <param name="obj"></param>
  889. private void AddNotesEvent(object obj)
  890. {
  891. if (obj is ListBoxItem listBoxItem)
  892. {
  893. if (listBoxItem.DataContext is AnnotationHandlerEventArgs args)
  894. {
  895. //if (args.EventType != AnnotArgsType.AnnotSticky && args.EventType != AnnotArgsType.AnnotFreeText && args.EventType != AnnotArgsType.AnnotLink)
  896. if (args.EventType != AnnotArgsType.AnnotSticky && args.EventType != AnnotArgsType.AnnotFreeText && args.EventType != AnnotArgsType.AnnotLink/*&&
  897. args.EventType != AnnotArgsType.AnnotHighlight && args.EventType != AnnotArgsType.AnnotUnderline
  898. && args.EventType != AnnotArgsType.AnnotStrikeout*/)
  899. {
  900. DialogParameters value = new DialogParameters();
  901. value.Add(ParameterNames.Annotation, args);
  902. dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
  903. {
  904. if (e.Result == ButtonResult.OK && e.Parameters != null)
  905. {
  906. PdfViewer.UndoManager.CanSave = true;
  907. if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
  908. {
  909. AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
  910. RefreshOneListItem(annotation.AnnotHandlerEventArgs);
  911. }
  912. }
  913. });
  914. }
  915. //else if (args.EventType == AnnotArgsType.AnnotFreeText)
  916. //{
  917. // StackPanel stackPanel = CommonHelper.FindVisualChild<StackPanel>(listBoxItem);
  918. // TextBox textBox = CommonHelper.FindVisualChild<TextBox>(stackPanel);
  919. // TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(stackPanel);
  920. // textBlock.Visibility = Visibility.Collapsed;
  921. // textBox.Visibility = Visibility.Visible;
  922. // textBox.Dispatcher.BeginInvoke(new Action(() =>
  923. // {
  924. // textBox.Focus();
  925. // textBox.SelectAll();
  926. // }));
  927. //}
  928. }
  929. }
  930. }
  931. private void RefreshOneListItem(AnnotHandlerEventArgs annotation)
  932. {
  933. for (int i = 0; i < AnnotationListItems.Count; i++)
  934. {
  935. if (AnnotationListItems[i].PageIndex == annotation.PageIndex && AnnotationListItems[i].AnnotIndex == annotation.AnnotIndex)
  936. {
  937. AnnotationListItems[i] = GetAddAnnotEventArgs(annotation);
  938. for (int j = 0; j < CurrentAnnotationLists.Count; j++)
  939. {
  940. if (CurrentAnnotationLists[j].PageIndex == annotation.PageIndex && CurrentAnnotationLists[j].AnnotIndex == annotation.AnnotIndex)
  941. {
  942. CurrentAnnotationLists[j] = GetAddAnnotEventArgs(annotation);
  943. break;
  944. }
  945. }
  946. if (AnnotationListItems[i] != null)
  947. {
  948. //if (!listBox.SelectedItems.Contains(AnnotationListItems[i]))
  949. //{
  950. // listBox.SelectedItem = AnnotationListItems[i];
  951. //}
  952. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, IsSelect = true, annotHandlerArgs = AnnotationListItems[i] });
  953. }
  954. //await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  955. //await Task.Delay(1);//不加延时 每页的第一个注释不会展开
  956. //ExpandGroupHeader(AnnotationListItems[i], listBox);
  957. //await Task.Delay(1);//不加延时 会有不滚动的现象
  958. //listBox.ScrollIntoView(AnnotationListItems[i]);
  959. break;
  960. }
  961. }
  962. }
  963. public void ScrollToAnnot(int pageindex, int annotindex, ListBox AnnotationList)
  964. {
  965. //var list = PdfViewer.GetAnnotCommentList(pageindex, PdfViewer.Document);
  966. for (int i = 0; i < AnnotationList.Items.Count; i++)
  967. {
  968. if (AnnotationList.Items[i] is AnnotationHandlerEventArgs item)
  969. {
  970. if (item.AnnotIndex == annotindex && item.PageIndex == pageindex)
  971. {
  972. //var item = AnnotationList.Items[i] as AnnotationHandlerEventArgs;
  973. if (item.EventType != AnnotArgsType.AnnotLink)
  974. {
  975. //需要手动搜寻在哪一个分组 展开分组头
  976. //ExpandGroupHeader(item, AnnotationList);
  977. //if (AnnotationList.SelectedItems.IndexOf(item) == -1)
  978. //{
  979. // AnnotationList.SelectedItem = item;
  980. //}
  981. //await Task.Delay(5);
  982. //AnnotationList.ScrollIntoView(item);
  983. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, IsSelect = true, annotHandlerArgs = item });
  984. }
  985. }
  986. }
  987. }
  988. }
  989. /// <summary>
  990. /// listboxitem鼠标左键点击,显示分组的数据
  991. /// </summary>
  992. /// <param name="obj"></param>
  993. private void ListBoxItem_PreviewMouseLeftButtonDown(object obj)
  994. {
  995. if (obj is ListBoxItem item)
  996. {
  997. var data = item.DataContext as AnnotationHandlerEventArgs;
  998. if (data != null)
  999. {
  1000. PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex);
  1001. }
  1002. }
  1003. if (obj is AnnotationHandlerEventArgs annotation)
  1004. {
  1005. PdfViewer.SelectAnnotation(annotation.PageIndex, annotation.AnnotIndex);
  1006. }
  1007. if (obj is Dictionary<int, List<int>> eventArgs)
  1008. {
  1009. PdfViewer.SelectAnnotation(eventArgs);
  1010. }
  1011. }
  1012. public async void UpdateAddedAnnot(int pageIndex, int annotIndex)
  1013. {
  1014. var annots = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  1015. for (int i = 0; i < annots.Count; i++)
  1016. {
  1017. AnnotHandlerEventArgs item = annots[i];
  1018. if (item.AnnotIndex == annotIndex && item.EventType != AnnotArgsType.AnnotRedaction)
  1019. {
  1020. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  1021. // 注释列表 不显示链接
  1022. if (args.AnnotHandlerEventArgs is LinkAnnotArgs || args == null)
  1023. {
  1024. continue;
  1025. }
  1026. if (args.AnnotHandlerEventArgs != null)
  1027. {
  1028. AnnotationListItems.Add(args);
  1029. CurrentAnnotationLists.Add(args);
  1030. await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  1031. if (ViewContentViewModel.IsMultiSelected == false)
  1032. {
  1033. //添加 时 ,形状、图章、签名,添加后选中
  1034. if (args.EventType == AnnotArgsType.AnnotSquare ||
  1035. args.EventType == AnnotArgsType.AnnotCircle ||
  1036. args.EventType == AnnotArgsType.AnnotLine ||
  1037. args.EventType == AnnotArgsType.AnnotStamp
  1038. /* ||args.EventType == AnnotArgsType.AnnotLink*/)
  1039. {
  1040. listBox.SelectedItem = args;
  1041. }
  1042. }
  1043. //ExpandGroupHeader(args, listBox);
  1044. // 关闭后选中关闭前的位置
  1045. if (ViewContentViewModel.OpenBOTA)
  1046. {
  1047. //await Task.Delay(5);//不加延时 会有不滚动的现象
  1048. //events.GetEvent<ScrollIntoAnnotViewEvent>().Publish(new ScrollIntoAnnotViewArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, Annotation = item });
  1049. //listBox.ScrollIntoView(args);
  1050. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, annotHandlerArgs = args });
  1051. }
  1052. //if (!listBox.SelectedItems.Contains(args))
  1053. //{
  1054. // listBox.SelectedItem = args;
  1055. // //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  1056. //}
  1057. }
  1058. }
  1059. }
  1060. listBox.UpdateLayout();
  1061. //SetGroupHeader(listBox);
  1062. if (IsEmptyPanelVisibility == Visibility.Visible && AnnotationListItems.Count > 0)
  1063. {
  1064. IsEmptyPanelVisibility = Visibility.Collapsed;
  1065. }
  1066. }
  1067. public void DeleteModifiedAnnot(int pageIndex, int annotIndex)
  1068. {
  1069. #region 历史版本
  1070. //var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  1071. //if (items.Count == 0)
  1072. //{
  1073. // for (int k = 0; k < AnnotationListItems.Count; k++)
  1074. // {
  1075. // if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  1076. // {
  1077. // //AnnotationListItems.RemoveAt(k);
  1078. // //AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  1079. // //CurrentAnnotationLists.Remove(annotation1);
  1080. // //UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  1081. // //AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
  1082. // //listBox.ScrollIntoView(args);
  1083. // //删除之后要拿到最新的数据源
  1084. // CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  1085. // AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  1086. // AnnotationListItems.AddRange(CurrentAnnotationLists);
  1087. // AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1088. // RefreshAnnotationListItems();
  1089. // return;
  1090. // }
  1091. // }
  1092. //}
  1093. //else
  1094. //{
  1095. // for (int k = 0; k < AnnotationListItems.Count; k++)
  1096. // {
  1097. // //不能加item[j].annoteIndex = annotIndex 删除后必匹配不上
  1098. // if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex)
  1099. // {
  1100. // //AnnotationHandlerEventArgs annotation1 = CurrentAnnotationLists.FirstOrDefault(x => x.PageIndex == AnnotationListItems[k].PageIndex && x.AnnotIndex == AnnotationListItems[k].AnnotIndex);
  1101. // //CurrentAnnotationLists.Remove(annotation1);
  1102. // //AnnotationListItems.RemoveAt(k);
  1103. // //UpdateAnnotListAfterDelete(pageIndex, annotIndex);
  1104. // //删除之后要拿到最新的数据源
  1105. // RefreshAnnotation(pageIndex);
  1106. // return;
  1107. // }
  1108. // }
  1109. //}
  1110. #endregion 历史版本
  1111. //更改更新注释方法 5.16
  1112. RefreshAnnotationPage(pageIndex);
  1113. }
  1114. /// <summary>
  1115. /// 将BOTA原本的对应页面所有注释删除,将更新后的所对应页面的注释更新到BOTA中,在滚动到对应页面第一个注释
  1116. /// </summary>
  1117. /// <param name="pageIndex">页面索引</param>
  1118. private void RefreshAnnotationPage(int pageIndex)
  1119. {
  1120. List<int> pageindex = new List<int>();
  1121. for (int k = 0; k < AnnotationListItems.Count; k++)
  1122. {
  1123. if (AnnotationListItems[k].PageIndex == pageIndex)
  1124. {
  1125. // AnnotationListItems.RemoveAt(k);
  1126. pageindex.Add(k);
  1127. }
  1128. }
  1129. CommonHelper.Reverseorder(ref pageindex);
  1130. foreach (var a in pageindex)
  1131. {
  1132. AnnotationListItems.RemoveAt(a);
  1133. }
  1134. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  1135. for (int k = 0; k < CurrentAnnotationLists.Count; k++)
  1136. {
  1137. if (CurrentAnnotationLists[k].PageIndex == pageIndex)
  1138. {
  1139. AnnotationListItems.Add(CurrentAnnotationLists[k]);
  1140. }
  1141. }
  1142. //PageSortEvent();
  1143. AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
  1144. ////await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  1145. ////ExpandGroupHeader(args, listBox);
  1146. ////await Task.Delay(5);//不加延时 会有不滚动的现象
  1147. ////listBox.ScrollIntoView(args);
  1148. ///
  1149. IsModified = true;
  1150. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, IsDel = true, annotHandlerArgs = args });
  1151. }
  1152. private void RefreshAnnotation(int pageIndex)
  1153. {
  1154. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  1155. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  1156. AnnotationListItems.AddRange(CurrentAnnotationLists);
  1157. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1158. //RefreshAnnotationListItems();
  1159. PageSortEvent();
  1160. AnnotationHandlerEventArgs args = AnnotationListItems.FindFirst(a => a.PageIndex == pageIndex);
  1161. //await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  1162. //ExpandGroupHeader(args, listBox);
  1163. //await Task.Delay(5);//不加延时 会有不滚动的现象
  1164. //listBox.ScrollIntoView(args);
  1165. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, annotHandlerArgs = args });
  1166. }
  1167. public void UpdateModifiedAnnot(int pageIndex, int annotIndex/*, bool isDelete*/)
  1168. {
  1169. try
  1170. {
  1171. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  1172. for (int j = 0; j < items.Count; j++)
  1173. {
  1174. for (int k = 0; k < AnnotationListItems.Count; k++)
  1175. {
  1176. if (AnnotationListItems[k].PageIndex == pageIndex && AnnotationListItems[k].AnnotIndex == annotIndex && items[j].AnnotIndex == annotIndex)
  1177. {
  1178. IsModified = false;
  1179. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(items[j]);
  1180. if (args != null)
  1181. {
  1182. AnnotationListItems[k] = args;
  1183. AnnotationListItems[k].Author = Settings.Default.AppProperties.Description.Author;
  1184. for (int i = 0; i < CurrentAnnotationLists.Count; i++)
  1185. {
  1186. if (CurrentAnnotationLists[i].PageIndex == pageIndex && CurrentAnnotationLists[i].AnnotIndex == annotIndex)
  1187. {
  1188. CurrentAnnotationLists[i] = GetAddAnnotEventArgs(items[j]);
  1189. break;
  1190. }
  1191. }
  1192. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = false, annotHandlerArgs = AnnotationListItems[k], IsSelect = true });
  1193. }
  1194. //await Task.Delay(5);//不加延时 每页的第一个注释不会展开
  1195. //if (ViewContentViewModel.IsMultiSelected == false)
  1196. //{
  1197. // if (listBox.SelectedItems.IndexOf(AnnotationListItems[k]) == -1)
  1198. // //if (!listBox.SelectedItems.Contains(AnnotationListItems[k]))
  1199. // {
  1200. // IsModified = false;
  1201. // listBox.SelectedItem = AnnotationListItems[k];
  1202. // //PdfViewer.SelectAnnotation(args.PageIndex, args.AnnotIndex);
  1203. // }
  1204. //}
  1205. //ExpandGroupHeader(AnnotationListItems[k], listBox);
  1206. //await Task.Delay(5);//不加延时 会有不滚动的现象
  1207. //listBox.ScrollIntoView(AnnotationListItems[k]);
  1208. }
  1209. }
  1210. }
  1211. listBox.UpdateLayout();
  1212. //SetGroupHeader(listBox);
  1213. }
  1214. catch { }
  1215. }
  1216. /// <summary>
  1217. /// 展开item所在分组
  1218. /// </summary>
  1219. private async void ExpandGroupHeader(AnnotationHandlerEventArgs item, ListBox listBox)
  1220. {
  1221. try
  1222. {
  1223. var groups = listBox.Items.Groups;
  1224. if (groups != null && groups.Count > 0)
  1225. {
  1226. for (int i = 0; i < groups.Count; i++)
  1227. {
  1228. var l = groups[i] as CollectionViewGroup;
  1229. if (l.Items.Contains(item))
  1230. {
  1231. var scroller = GetScrollHost(listBox);
  1232. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  1233. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  1234. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  1235. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  1236. if (g != null)
  1237. {
  1238. await Task.Delay(5);
  1239. g.IsExpanded = true;
  1240. }
  1241. }
  1242. }
  1243. }
  1244. }
  1245. catch { }
  1246. }
  1247. /// <summary>
  1248. /// 页面加载时
  1249. /// </summary>
  1250. /// <param name="obj"></param>
  1251. private void Loaded(object obj)
  1252. {
  1253. if (obj is CompositeCommandParameter composite)
  1254. {
  1255. if (composite.Parameter is ListBox listBox)
  1256. {
  1257. this.listBox = listBox;
  1258. //RefreshAnnotationListItems();
  1259. SetGroupHeader(listBox);
  1260. }
  1261. }
  1262. }
  1263. public bool IsNavigationTarget(NavigationContext navigationContext)
  1264. {
  1265. return true;
  1266. }
  1267. public void OnNavigatedFrom(NavigationContext navigationContext)
  1268. {
  1269. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1270. }
  1271. private ObservableCollection<AnnotationHandlerEventArgs> HideAnnots;
  1272. public void OnNavigatedTo(NavigationContext navigationContext)
  1273. {
  1274. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  1275. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  1276. if (pdfview != null && viewContentViewModel != null)
  1277. {
  1278. ViewContentViewModel = viewContentViewModel;
  1279. PdfViewer = pdfview;
  1280. }
  1281. if (IsFirst)
  1282. {
  1283. //加密文档未解密进入时,不算进入次数,方便后续方法再次导航进来刷新
  1284. if (!pdfview.Document.IsLocked)
  1285. {
  1286. IsFirst = false;
  1287. }
  1288. CurrentAnnotationLists = GetDocumentAnnotionList(out HideAnnots);
  1289. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  1290. AnnotationListItems.CollectionChanged -= AnnotationListItems_CollectionChanged;
  1291. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1292. AnnotationListItems.AddRange(CurrentAnnotationLists);
  1293. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  1294. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  1295. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  1296. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  1297. //按照PageIndex,分组 排序
  1298. SetGroupHeader(listBox);
  1299. Screen = Visibility.Collapsed;
  1300. colors.Clear();
  1301. authors.Clear();
  1302. annotArgsTypes.Clear();
  1303. }
  1304. if (viewContentViewModel.IsHiddenAnnot)
  1305. {
  1306. HiddenAnnot();
  1307. }
  1308. else
  1309. {
  1310. DisplayAnnot();
  1311. }
  1312. AnnotationListItems.CollectionChanged -= AnnotationListItems_CollectionChanged;
  1313. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1314. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1315. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  1316. }
  1317. private void RefreshData(RefreshAnnotArgs obj)
  1318. {
  1319. if (obj.Annotation != null)
  1320. {
  1321. RefreshOneListItem(obj.Annotation.AnnotHandlerEventArgs);
  1322. }
  1323. }
  1324. private void ShortCut_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  1325. {
  1326. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  1327. {
  1328. if (listBox != null)
  1329. {
  1330. if (listBox.SelectedItems == null)
  1331. {
  1332. return;
  1333. }
  1334. listBox.SelectedItems.Clear();
  1335. }
  1336. }
  1337. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Delete))
  1338. {
  1339. if (listBox != null)
  1340. {
  1341. if (listBox.SelectedItems == null)
  1342. {
  1343. return;
  1344. }
  1345. List<int> pagelist = new List<int>();
  1346. for (int i = 0; i < listBox.SelectedItems.Count; i++)
  1347. {
  1348. AnnotationHandlerEventArgs annotation = listBox.SelectedItems[i] as AnnotationHandlerEventArgs;
  1349. pagelist.Add(listBox.Items.IndexOf(annotation));
  1350. }
  1351. pagelist.Sort();
  1352. for (int i = 0; i < pagelist.Count; i++)
  1353. {
  1354. AnnotationHandlerEventArgs annotation = listBox.Items[pagelist[pagelist.Count - i - 1]] as AnnotationHandlerEventArgs;
  1355. if (annotation == null)
  1356. {
  1357. continue;
  1358. }
  1359. DeleteCommand.Execute(annotation);
  1360. }
  1361. }
  1362. }
  1363. }
  1364. /// <summary>
  1365. /// 默认按照PageIndex,分组 排序
  1366. /// </summary>
  1367. /// <param name="listBox"></param>
  1368. private void SetGroupHeader(ListBox listBox)
  1369. {
  1370. if (listBox == null) { return; }
  1371. #region BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  1372. //AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>(AnnotationListItems.OrderBy(item => item.ClientRect.Y).ThenBy(item => item.ClientRect.X));
  1373. //CurrentAnnotationLists.AddRange(AnnotationListItems);
  1374. //AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  1375. //ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  1376. //iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  1377. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  1378. ////iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  1379. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.Y), ListSortDirection.Ascending));
  1380. //iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.ClientRect.X), ListSortDirection.Ascending));
  1381. #endregion BOTA,注释列表,添加在页面上的注释要默认按照页面上的位置排序,从上往下,从左往右排列
  1382. if (AnnotationListItems == null)
  1383. {
  1384. return;
  1385. }
  1386. if (AnnotationListItems.Count > 0)
  1387. {
  1388. IsEmptyPanelVisibility = Visibility.Collapsed;
  1389. //await Task.Delay(5);
  1390. //展开数据
  1391. //ExpandGroupHeader(AnnotationListItems, listBox);
  1392. events.GetEvent<RefreshAnnotationEvent>().Publish(new RefreshAnnotationArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsAll = true });
  1393. }
  1394. else
  1395. {
  1396. if (CurrentAnnotationLists.Count <= 0 && HideAnnots.Count > 1)
  1397. {
  1398. IsEmptyPanelVisibility = Visibility.Visible;
  1399. IsHideAnnotPanelVisibility = Visibility.Visible;
  1400. }
  1401. else
  1402. {
  1403. IsEmptyPanelVisibility = Visibility.Visible;
  1404. IsHideAnnotPanelVisibility = Visibility.Visible;
  1405. }
  1406. }
  1407. }
  1408. /// <summary>
  1409. /// 展开列表项
  1410. /// </summary>
  1411. /// <param name="annotationListItems"></param>
  1412. /// <param name="listBox"></param>
  1413. private async void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  1414. {
  1415. try
  1416. {
  1417. foreach (var item in annotationListItems)
  1418. {
  1419. var groups = listBox.Items.Groups;
  1420. for (int i = 0; i < groups.Count; i++)
  1421. {
  1422. var group = groups[i] as CollectionViewGroup;
  1423. if (group.Items.Contains(item))
  1424. {
  1425. var scroller = GetScrollHost(listBox);
  1426. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  1427. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  1428. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  1429. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  1430. if (g != null)
  1431. {
  1432. await Task.Delay(5);
  1433. g.IsExpanded = true;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. }
  1439. catch { }
  1440. }
  1441. private ScrollViewer GetScrollHost(ListBox listBox)
  1442. {
  1443. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  1444. {
  1445. int s = VisualTreeHelper.GetChildrenCount(listBox);
  1446. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  1447. if (border != null)
  1448. {
  1449. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  1450. }
  1451. }
  1452. return null;
  1453. }
  1454. /// <summary>
  1455. /// 获取listbox绑定数据
  1456. /// </summary>
  1457. /// <returns></returns>
  1458. public ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList(out ObservableCollection<AnnotationHandlerEventArgs> HideAnnots)
  1459. {
  1460. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  1461. HideAnnots = new ObservableCollection<AnnotationHandlerEventArgs>();
  1462. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  1463. {
  1464. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  1465. foreach (var item in items)
  1466. {
  1467. //原型图上,目前对波浪线的类型,在注释列表不显示
  1468. //注释列表 不显示链接注释
  1469. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly
  1470. && item.EventType != AnnotArgsType.AnnotLink)
  1471. {
  1472. if (item.EventType == AnnotArgsType.AnnotLink)
  1473. {
  1474. if (item is LinkAnnotArgs link)
  1475. {
  1476. if (link.LinkType == LINK_TYPE.URI)
  1477. {
  1478. continue;
  1479. }
  1480. }
  1481. }
  1482. AnnotationHandlerEventArgs args = GetAddAnnotEventArgs(item);
  1483. if (args != null)
  1484. {
  1485. bool isVisible = PdfViewer.CheckAnnotVisible(args.PageIndex, args.AnnotIndex);
  1486. if (args.AnnotHandlerEventArgs != null)
  1487. {
  1488. if (isVisible)
  1489. {
  1490. list.Add(args);
  1491. }
  1492. else
  1493. {
  1494. HideAnnots.Add(args);
  1495. }
  1496. }
  1497. }
  1498. }
  1499. }
  1500. }
  1501. return list;
  1502. }
  1503. public AnnotationHandlerEventArgs GetAddAnnotEventArgs(AnnotHandlerEventArgs item)
  1504. {
  1505. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  1506. //原型图上,目前对波浪线的类型,在注释列表不显示
  1507. //注释列表 不显示链接注释
  1508. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly
  1509. && item.EventType != AnnotArgsType.AnnotLink)
  1510. {
  1511. if (item.EventType == AnnotArgsType.AnnotFreehand)
  1512. {
  1513. BitmapImage bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  1514. if (bitmap != null)
  1515. args.WriteableBitmap = bitmap;
  1516. }
  1517. args.ClientRect = item.ClientRect;
  1518. args.AnnotHandlerEventArgs = item;
  1519. args.PageIndex = item.PageIndex;
  1520. args.AnnotIndex = item.AnnotIndex;
  1521. args.EventType = item.EventType;
  1522. args.CreateTime = item.CreateTime;
  1523. args.UpdateTime = item.UpdateTime;
  1524. args.Content = item.Content;
  1525. args.MarkupContent = item.MarkupContent;
  1526. args.Author = item.Author;
  1527. args.Locked = item.Locked;
  1528. args.ReadOnly = item.ReadOnly;
  1529. args.FormField = item.FormField;
  1530. args.Document = PdfViewer.Document;
  1531. }
  1532. return args;
  1533. }
  1534. /// <summary>
  1535. /// 获取手绘图案
  1536. /// </summary>
  1537. /// <param name="doc"></param>
  1538. /// <param name="pageIndex"></param>
  1539. /// <param name="annotIndex"></param>
  1540. /// <returns></returns>
  1541. public BitmapImage GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  1542. {
  1543. if (doc == null)
  1544. {
  1545. return null;
  1546. }
  1547. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  1548. if (docPage == null)
  1549. {
  1550. return null;
  1551. }
  1552. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  1553. foreach (CPDFAnnotation annot in docAnnots)
  1554. {
  1555. if (docAnnots.IndexOf(annot) == annotIndex)
  1556. {
  1557. CPDFInkAnnotation cPDFInk = (CPDFInkAnnotation)docAnnots[annotIndex];
  1558. //CRect rawRect = annot.GetRect();
  1559. CRect rawRect = cPDFInk.Rect;
  1560. double scaleDpi = 96.0 / 72.0;
  1561. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  1562. int drawWidth = (int)paintRect.Width;
  1563. int drawHeight = (int)paintRect.Height;
  1564. switch (docPage.Rotation)
  1565. {
  1566. case 0:
  1567. case 2:
  1568. drawWidth = (int)paintRect.Width;
  1569. drawHeight = (int)paintRect.Height;
  1570. break;
  1571. case 1:
  1572. case 3:
  1573. drawWidth = (int)paintRect.Height;
  1574. drawHeight = (int)paintRect.Width;
  1575. break;
  1576. }
  1577. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  1578. annot.UpdateAp();
  1579. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  1580. BitmapImage bitmapImage = null;
  1581. if (drawWidth != 0 && drawHeight != 0)
  1582. {
  1583. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  1584. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  1585. bitmapImage = ConvertWriteableBitmapToBitmapImage(wirteBitmap, docPage.Rotation);
  1586. }
  1587. else
  1588. {
  1589. bitmapImage = null;
  1590. }
  1591. cPDFInk.SetRect(rawRect);
  1592. // 测试
  1593. //using (FileStream stream = new FileStream(@"C:\Users\oyxh\Desktop\images\1.png", FileMode.Create))
  1594. //{
  1595. // PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
  1596. // bitmapEncoder.Frames.Add(BitmapFrame.Create(wirteBitmap));
  1597. // bitmapEncoder.Save(stream);
  1598. //}
  1599. return bitmapImage;
  1600. }
  1601. }
  1602. return null;
  1603. }
  1604. public BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wbm, int rotation)
  1605. {
  1606. BitmapImage bmImage = new BitmapImage();
  1607. using (MemoryStream stream = new MemoryStream())
  1608. {
  1609. PngBitmapEncoder encoder = new PngBitmapEncoder();
  1610. encoder.Frames.Add(BitmapFrame.Create(wbm));
  1611. encoder.Save(stream);
  1612. bmImage.BeginInit();
  1613. bmImage.CacheOption = BitmapCacheOption.OnLoad;
  1614. bmImage.StreamSource = stream;
  1615. switch (rotation)
  1616. {
  1617. case 0:
  1618. bmImage.Rotation = Rotation.Rotate0;
  1619. break;
  1620. case 1:
  1621. bmImage.Rotation = Rotation.Rotate90;
  1622. break;
  1623. case 2:
  1624. bmImage.Rotation = Rotation.Rotate180;
  1625. break;
  1626. case 3:
  1627. bmImage.Rotation = Rotation.Rotate270;
  1628. break;
  1629. }
  1630. bmImage.EndInit();
  1631. bmImage.Freeze();
  1632. }
  1633. return bmImage;
  1634. }
  1635. /// <summary>
  1636. /// listbox的itemsource发生变化时
  1637. /// </summary>
  1638. /// <param name="sender"></param>
  1639. /// <param name="e"></param>
  1640. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  1641. {
  1642. if (sender is ObservableCollection<AnnotationHandlerEventArgs> obsSender)
  1643. {
  1644. //currentAnnotationArgs.Clear();
  1645. //currentAnnotationArgs.AddRange(obsSender);
  1646. if (obsSender.Count < 1)
  1647. {
  1648. if (CurrentAnnotationLists.Count <= 0)
  1649. {
  1650. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1651. IsEmptyPanelVisibility = Visibility.Visible;
  1652. }
  1653. else
  1654. {
  1655. if (isHiddenAnnot)
  1656. {
  1657. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1658. IsEmptyPanelVisibility = Visibility.Visible;
  1659. }
  1660. else
  1661. {
  1662. IsEmptyPanelVisibility = Visibility.Visible;
  1663. IsHideAnnotPanelVisibility = Visibility.Visible;
  1664. }
  1665. }
  1666. //IsEmptyPanelVisibility = Visibility.Visible;
  1667. }
  1668. else
  1669. {
  1670. IsEmptyPanelVisibility = Visibility.Collapsed;
  1671. IsHideAnnotPanelVisibility = Visibility.Collapsed;
  1672. }
  1673. }
  1674. else
  1675. {
  1676. IsEmptyPanelVisibility = Visibility.Visible;
  1677. }
  1678. }
  1679. }
  1680. }