AnnotationContentViewModel.cs 56 KB

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