AnnotationContentViewModel.cs 61 KB

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