AnnotationContentViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 Microsoft.Office.Interop.Excel;
  10. using Microsoft.Office.Interop.Word;
  11. using PDF_Office.CustomControl;
  12. using PDF_Office.DataConvert;
  13. using PDF_Office.Helper;
  14. using PDF_Office.Model;
  15. using PDF_Office.Model.BOTA;
  16. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  17. using Prism.Commands;
  18. using Prism.Mvvm;
  19. using Prism.Regions;
  20. using Prism.Services.Dialogs;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Collections.ObjectModel;
  24. using System.ComponentModel;
  25. using System.Configuration;
  26. using System.Linq;
  27. using System.Text;
  28. using System.Threading.Tasks;
  29. using System.Windows;
  30. using System.Windows.Controls;
  31. using System.Windows.Data;
  32. using System.Windows.Forms;
  33. using System.Windows.Markup;
  34. using System.Windows.Media;
  35. using System.Windows.Media.Imaging;
  36. using System.Windows.Shapes;
  37. using static Dropbox.Api.Files.SearchMatchType;
  38. using static System.Net.Mime.MediaTypeNames;
  39. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  40. using Border = System.Windows.Controls.Border;
  41. using ListBox = System.Windows.Controls.ListBox;
  42. using Task = System.Threading.Tasks.Task;
  43. using TextBox = System.Windows.Controls.TextBox;
  44. using Winform = System.Windows.Forms;
  45. namespace PDF_Office.ViewModels.BOTA
  46. {
  47. public class AnnotationContentViewModel : BindableBase, INavigationAware
  48. {
  49. private ListBox listBox;
  50. private IRegionManager region;
  51. private IDialogService dialogs;
  52. private Visibility isEmptyPanelVisibility = Visibility.Visible;
  53. public ViewContentViewModel ViewContentViewModel { get; set; }
  54. public CPDFViewer PdfViewer { get; set; }
  55. public Visibility IsEmptyPanelVisibility
  56. {
  57. get { return isEmptyPanelVisibility; }
  58. set
  59. {
  60. SetProperty(ref isEmptyPanelVisibility, value);
  61. }
  62. }
  63. private ObservableCollection<AnnotationHandlerEventArgs> annotationListItems;
  64. public ObservableCollection<AnnotationHandlerEventArgs> AnnotationListItems
  65. {
  66. get { return annotationListItems; }
  67. set
  68. {
  69. SetProperty(ref annotationListItems, value);
  70. }
  71. }
  72. public DelegateCommand<object> LoadedCommand { get; set; }
  73. public DelegateCommand<object> ListBoxItemPreviewMouseLeftButtonDown { get; set; }
  74. public DelegateCommand<object> AddNotesCommand { get; set; }
  75. public DelegateCommand<object> ScreenCommand { get; set; }
  76. public DelegateCommand<object> ExportCommentsCommand { get; set; }
  77. public DelegateCommand<object> DeleteCommand { get; set; }
  78. public DelegateCommand DeleteAllCommand { get; set; }
  79. public AnnotationContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  80. {
  81. region = regionManager;
  82. dialogs = dialogService;
  83. LoadedCommand = new DelegateCommand<object>(Loaded);
  84. ListBoxItemPreviewMouseLeftButtonDown = new DelegateCommand<object>(ListBoxItem_PreviewMouseLeftButtonDown);
  85. AddNotesCommand = new DelegateCommand<object>(AddNotesEvent);
  86. ScreenCommand = new DelegateCommand<object>(ScreenEvent);
  87. ExportCommentsCommand = new DelegateCommand<object>(ExportCommentsEvent);
  88. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  89. DeleteAllCommand = new DelegateCommand(DeleteAllEvent);
  90. }
  91. /// <summary>
  92. /// 删除所有注释
  93. /// </summary>
  94. private void DeleteAllEvent()
  95. {
  96. //调用集中删除的接口 方便一次性undo
  97. Dictionary<int, List<int>> deleteLists = new Dictionary<int, List<int>>();
  98. for (int i = 0; i < annotationListItems.Count; i++)
  99. {
  100. AnnotationHandlerEventArgs item = annotationListItems[i] as AnnotationHandlerEventArgs;
  101. if (!deleteLists.ContainsKey(item.PageIndex))
  102. {
  103. deleteLists.Add(item.PageIndex, new List<int>() { item.AnnotIndex });
  104. }
  105. else
  106. {
  107. var pagelist = deleteLists[item.PageIndex];
  108. pagelist.Add(item.AnnotIndex);
  109. }
  110. }
  111. PdfViewer.RemovePageAnnot(deleteLists);
  112. annotationListItems.Clear();
  113. PdfViewer.UndoManager.CanSave = true;
  114. }
  115. /// <summary>
  116. /// 删除注释,单个/多个
  117. /// </summary>
  118. /// <param name="obj"></param>
  119. private void DelegateEvent(object obj)
  120. {
  121. if (obj is AnnotationHandlerEventArgs annotation)
  122. {
  123. if (annotation != null)
  124. {
  125. var result = PdfViewer.RemovePageAnnot(annotation.PageIndex, annotation.AnnotIndex);
  126. if (result)
  127. {
  128. AnnotationListItems.Remove(annotation);
  129. //记录是删除了哪些页面的注释,然后更新对应页面的注释即可
  130. UpdateAnnotListAfterDelete(annotation.PageIndex, annotation.AnnotIndex);
  131. PdfViewer.UndoManager.CanSave = true;
  132. }
  133. }
  134. }
  135. }
  136. private void UpdateAnnotListAfterDelete(int pageIndex, int annoteIndex)
  137. {
  138. var items = PdfViewer.GetAnnotCommentList(pageIndex, PdfViewer.Document);
  139. for (int j = 0; j < items.Count; j++)//用修改赋值的方式 可以解决删除后表头折叠的问题
  140. {
  141. if (items[j].AnnotIndex >= annoteIndex)//只需要更新比删除元素索引大的注释
  142. {
  143. for (int k = 0; k < AnnotationListItems.Count; k++)
  144. {
  145. //相当于将后面的索引-1
  146. if (AnnotationListItems[k].PageIndex == pageIndex &&
  147. AnnotationListItems[k].AnnotIndex == (items[j].AnnotIndex + 1) &&
  148. string.Equals(AnnotationListItems[k].MarkupContent, items[j].MarkupContent) &&
  149. string.Equals(AnnotationListItems[k].Content, items[j].Content) &&
  150. string.Equals(AnnotationListItems[k].CreateTime, items[j].CreateTime) &&
  151. string.Equals(AnnotationListItems[k].Author, items[j].Author)
  152. )
  153. {
  154. AnnotationListItems[k].AnnotHandlerEventArgs = items[j];
  155. AnnotationListItems[k].PageIndex = items[j].PageIndex;
  156. AnnotationListItems[k].AnnotIndex = items[j].AnnotIndex;
  157. AnnotationListItems[k].EventType = items[j].EventType;
  158. AnnotationListItems[k].CreateTime = items[j].CreateTime;
  159. AnnotationListItems[k].UpdateTime = items[j].UpdateTime;
  160. AnnotationListItems[k].Content = items[j].Content;
  161. AnnotationListItems[k].MarkupContent = items[j].MarkupContent;
  162. AnnotationListItems[k].Author = items[j].Author;
  163. AnnotationListItems[k].Locked = items[j].Locked;
  164. AnnotationListItems[k].ReadOnly = items[j].ReadOnly;
  165. AnnotationListItems[k].FormField = items[j].FormField;
  166. AnnotationListItems[k].Document = PdfViewer.Document;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. private void ExportCommentsEvent(object obj)
  173. {
  174. if (ViewContentViewModel.CanSave)
  175. {
  176. ViewContentViewModel.SaveFile.Execute();
  177. }
  178. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  179. dlg.Filter = "PDF|*.xfdf";
  180. dlg.DefaultExt = ".xfdf";
  181. dlg.FileName = PdfViewer.Document.FileName;
  182. if (dlg.ShowDialog() == true)
  183. {
  184. string fileName = dlg.FileName;
  185. var result = PdfViewer.ExportAnnotationToXFDFPath(fileName);
  186. if (result)
  187. {
  188. MessageBoxEx.Show("导出成功", "", Winform.MessageBoxButtons.OK);
  189. }
  190. else
  191. {
  192. MessageBoxEx.Show("导出失败", "", Winform.MessageBoxButtons.OK, Winform.MessageBoxIcon.Error);
  193. }
  194. }
  195. }
  196. private void ScreenEvent(object obj)
  197. {
  198. DialogParameters value = new DialogParameters();
  199. value.Add(ParameterNames.AnnotationList, AnnotationListItems);
  200. dialogs.ShowDialog(DialogNames.ScreenAnnotationDialog, value, e =>
  201. {
  202. });
  203. }
  204. private void AddNotesEvent(object obj)
  205. {
  206. if (obj is AnnotationHandlerEventArgs data)
  207. {
  208. if (data != null)
  209. {
  210. if (data.EventType != AnnotArgsType.AnnotSticky)
  211. {
  212. DialogParameters value = new DialogParameters();
  213. value.Add(ParameterNames.Annotation, data);
  214. dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
  215. {
  216. if (e.Result == ButtonResult.OK && e.Parameters != null)
  217. {
  218. //PdfViewer.UndoManager.CanSave = true;
  219. // if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
  220. // {
  221. // AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
  222. // //AnnotAttribEvent annotEvent = e.Parameters.GetValue<AnnotAttribEvent>(ParameterNames.AnnotEvent);
  223. // //annotEvent?.UpdateAttrib(AnnotAttrib.NoteText, annotation.MarkupContent);
  224. // //annotEvent?.UpdateAnnot();
  225. // }
  226. }
  227. });
  228. }
  229. //if(data.EventType == AnnotArgsType.AnnotFreeText)
  230. //{
  231. // ListBoxItem myListBoxItem = (ListBoxItem)(listBox.ItemContainerGenerator.ContainerFromItem(data));
  232. // TextBlock txbContent = CommonHelper.FindVisualChild<TextBlock>(myListBoxItem);
  233. // TextBox txtContent = CommonHelper.FindVisualChild<TextBox>(myListBoxItem);
  234. // txbContent.Visibility = Visibility.Collapsed;
  235. // txtContent.Visibility = Visibility.Visible;
  236. //}
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// listboxitem鼠标左键点击,显示分组的数据
  242. /// </summary>
  243. /// <param name="obj"></param>
  244. private void ListBoxItem_PreviewMouseLeftButtonDown(object obj)
  245. {
  246. if (obj is ListBoxItem item)
  247. {
  248. var data = item.DataContext as AnnotationHandlerEventArgs;
  249. if (data != null)
  250. {
  251. PdfViewer.SelectAnnotation(data.PageIndex, data.AnnotIndex);
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// 页面加载时
  257. /// </summary>
  258. /// <param name="obj"></param>
  259. private void Loaded(object obj)
  260. {
  261. if (obj is CompositeCommandParameter composite)
  262. {
  263. if (composite.Parameter is ListBox listBox)
  264. {
  265. this.listBox = listBox;
  266. SetGroupHeader(listBox);
  267. }
  268. }
  269. }
  270. public bool IsNavigationTarget(NavigationContext navigationContext)
  271. {
  272. return true;
  273. }
  274. public void OnNavigatedFrom(NavigationContext navigationContext)
  275. {
  276. }
  277. public void OnNavigatedTo(NavigationContext navigationContext)
  278. {
  279. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  280. var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  281. if (pdfview != null && viewContentViewModel != null)
  282. {
  283. ViewContentViewModel = viewContentViewModel;
  284. PdfViewer = pdfview;
  285. AnnotationListItems = new ObservableCollection<AnnotationHandlerEventArgs>();
  286. AnnotationListItems = GetDocumentAnnotionList();
  287. AnnotationListItems.CollectionChanged += AnnotationListItems_CollectionChanged;
  288. }
  289. }
  290. private async void SetGroupHeader(ListBox listBox)
  291. {
  292. //按照PageIndex,分组 排序
  293. ICollectionView iCollectionView = CollectionViewSource.GetDefaultView(AnnotationListItems);
  294. CreateTimeToDate createTimeToDate = new CreateTimeToDate();
  295. iCollectionView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(AnnotationHandlerEventArgs.PageIndex)));
  296. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.PageIndex), ListSortDirection.Ascending));
  297. iCollectionView.SortDescriptions.Add(new SortDescription(nameof(AnnotationHandlerEventArgs.AnnotIndex), ListSortDirection.Ascending));
  298. if (AnnotationListItems.Count > 0)
  299. {
  300. IsEmptyPanelVisibility = Visibility.Collapsed;
  301. await Task.Delay(5);
  302. //展开数据
  303. ExpandGroupHeader(AnnotationListItems, listBox);
  304. }
  305. else
  306. {
  307. IsEmptyPanelVisibility = Visibility.Visible;
  308. }
  309. }
  310. /// <summary>
  311. /// 展开列表项
  312. /// </summary>
  313. /// <param name="annotationListItems"></param>
  314. /// <param name="listBox"></param>
  315. private void ExpandGroupHeader(ObservableCollection<AnnotationHandlerEventArgs> annotationListItems, ListBox listBox)
  316. {
  317. try
  318. {
  319. foreach (var item in annotationListItems)
  320. {
  321. var groups = listBox.Items.Groups;
  322. for (int i = 0; i < groups.Count; i++)
  323. {
  324. var group = groups[i] as CollectionViewGroup;
  325. if (group.Items.Contains(item))
  326. {
  327. var scroller = GetScrollHost(listBox);
  328. var stackpanel = CommonHelper.FindVisualChild<StackPanel>(scroller);
  329. int count = VisualTreeHelper.GetChildrenCount(stackpanel);
  330. var groupItem = VisualTreeHelper.GetChild(stackpanel, i) as GroupItem;
  331. var g = CommonHelper.FindVisualChild<Expander>(groupItem);
  332. if (g != null)
  333. {
  334. g.IsExpanded = true;
  335. }
  336. }
  337. }
  338. }
  339. }
  340. catch { }
  341. }
  342. private ScrollViewer GetScrollHost(ListBox listBox)
  343. {
  344. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  345. {
  346. int s = VisualTreeHelper.GetChildrenCount(listBox);
  347. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  348. if (border != null)
  349. {
  350. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  351. }
  352. }
  353. return null;
  354. }
  355. private ObservableCollection<AnnotationHandlerEventArgs> GetDocumentAnnotionList()
  356. {
  357. ObservableCollection<AnnotationHandlerEventArgs> list = new ObservableCollection<AnnotationHandlerEventArgs>();
  358. for (int i = 0; i < PdfViewer.Document.PageCount; i++)
  359. {
  360. var items = PdfViewer.GetAnnotCommentList(i, PdfViewer.Document);
  361. foreach (var item in items)
  362. {
  363. //原型图上,目前对波浪线的类型,在注释列表不显示
  364. if (item.EventType != AnnotArgsType.AnnotRedaction && item.EventType != AnnotArgsType.AnnotSquiggly)
  365. {
  366. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  367. if (item.EventType == AnnotArgsType.AnnotFreehand)
  368. {
  369. WriteableBitmap bitmap = GetAnnotImage(PdfViewer.Document, item.PageIndex, item.AnnotIndex);
  370. args.WriteableBitmap = bitmap;
  371. }
  372. args.AnnotHandlerEventArgs = item;
  373. args.PageIndex = item.PageIndex;
  374. args.AnnotIndex = item.AnnotIndex;
  375. args.EventType = item.EventType;
  376. args.CreateTime = item.CreateTime;
  377. args.UpdateTime = item.UpdateTime;
  378. args.Content = item.Content;
  379. args.MarkupContent = item.MarkupContent;
  380. args.Author = item.Author;
  381. args.Locked = item.Locked;
  382. args.ReadOnly = item.ReadOnly;
  383. args.FormField = item.FormField;
  384. args.Document = PdfViewer.Document;
  385. list.Add(args);
  386. }
  387. }
  388. }
  389. return list;
  390. }
  391. /// <summary>
  392. /// 获取手绘图案
  393. /// </summary>
  394. /// <param name="doc"></param>
  395. /// <param name="pageIndex"></param>
  396. /// <param name="annotIndex"></param>
  397. /// <returns></returns>
  398. public WriteableBitmap GetAnnotImage(CPDFDocument doc, int pageIndex, int annotIndex)
  399. {
  400. if (doc == null)
  401. {
  402. return null;
  403. }
  404. CPDFPage docPage = doc.PageAtIndex(pageIndex, false);
  405. if (docPage == null)
  406. {
  407. return null;
  408. }
  409. List<CPDFAnnotation> docAnnots = docPage.GetAnnotations();
  410. foreach (CPDFAnnotation annot in docAnnots)
  411. {
  412. if (docAnnots.IndexOf(annot) == annotIndex)
  413. {
  414. CRect rawRect = annot.GetRect();
  415. double scaleDpi = 96.0 / 72.0;
  416. Rect paintRect = new Rect(rawRect.left * scaleDpi, rawRect.top * scaleDpi, rawRect.width() * scaleDpi, rawRect.height() * scaleDpi);
  417. int drawWidth = (int)paintRect.Width;
  418. int drawHeight = (int)paintRect.Height;
  419. byte[] bitmapArray = new byte[drawWidth * drawHeight * 4];
  420. annot.RenderAnnot(drawWidth, drawHeight, bitmapArray);
  421. WriteableBitmap wirteBitmap = new WriteableBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Bgra32, null);
  422. wirteBitmap.WritePixels(new Int32Rect(0, 0, drawWidth, drawHeight), bitmapArray, wirteBitmap.BackBufferStride, 0);
  423. return wirteBitmap;
  424. }
  425. }
  426. return null;
  427. }
  428. private void AnnotationListItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  429. {
  430. ObservableCollection<AnnotationHandlerEventArgs> obsSender = sender as ObservableCollection<AnnotationHandlerEventArgs>;
  431. if (obsSender != null)
  432. {
  433. if (obsSender.Count < 1)
  434. {
  435. IsEmptyPanelVisibility = Visibility.Visible;
  436. }
  437. else
  438. {
  439. IsEmptyPanelVisibility = Visibility.Collapsed;
  440. }
  441. }
  442. else
  443. {
  444. IsEmptyPanelVisibility = Visibility.Visible;
  445. }
  446. }
  447. }
  448. }