AnnotationContentViewModel.cs 50 KB

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