AnnotationContentViewModel.cs 65 KB

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