AnnotationContentViewModel.cs 53 KB

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