SearchContentViewModel.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.Model;
  4. using PDF_Office.Views.BOTA;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.ComponentModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Data;
  16. using System.Windows.Media;
  17. using System.Windows.Threading;
  18. namespace PDF_Office.ViewModels.BOTA
  19. {
  20. class SearchContentViewModel : BindableBase, INavigationAware
  21. {
  22. #region 属性
  23. public PDFTextSearch textSearch;
  24. private ObservableCollection<TextSearchBindItem> _searchResults = new ObservableCollection<TextSearchBindItem>();
  25. public ObservableCollection<TextSearchBindItem> searchResults
  26. {
  27. get { return _searchResults; }
  28. set { SetProperty(ref _searchResults, value); }
  29. }
  30. public List<TextSearchBindItem> lists;
  31. private string _searchContent = "";
  32. public string SearchContent
  33. {
  34. get { return _searchContent; }
  35. set { SetProperty(ref _searchContent, value); }
  36. }
  37. private int _searchCount = 0;
  38. public int SearchCount
  39. {
  40. get { return _searchCount; }
  41. set { SetProperty(ref _searchCount, value); }
  42. }
  43. private bool _isEmpty = true;
  44. public bool IsEmpty
  45. {
  46. get { return _isEmpty; }
  47. set { SetProperty(ref _isEmpty, value); }
  48. }
  49. #endregion
  50. #region 事件
  51. public DelegateCommand<object> SearchTextCommand { get; set; }
  52. public DelegateCommand<object> SearchChangedCommand { get; set; }
  53. #endregion
  54. public SearchContentViewModel()
  55. {
  56. InitVariable();
  57. InitCommand();
  58. BindEvent();
  59. lists = new List<TextSearchBindItem>();
  60. }
  61. private void InitVariable()
  62. {
  63. textSearch = new PDFTextSearch();
  64. ICollectionView groupView = CollectionViewSource.GetDefaultView(searchResults);
  65. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(TextSearchBindItem.ShowPageIndex)));
  66. }
  67. private void InitCommand()
  68. {
  69. SearchTextCommand = new DelegateCommand<object>(SearchText);
  70. SearchChangedCommand = new DelegateCommand<object>(SearchChanged);
  71. }
  72. private void BindEvent()
  73. {
  74. textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler;
  75. textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
  76. }
  77. private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
  78. {
  79. issearched = true;
  80. TotalCount = e.TotalCount;
  81. }
  82. bool issearched = true;
  83. int TotalCount = 0;
  84. private bool cancelTask = false;
  85. private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e)
  86. {
  87. if (cancelTask == false)
  88. {
  89. string keywords = SearchContent;
  90. if (e.Items.ContainsKey(e.CurrentPage))
  91. {
  92. {
  93. TextSearchBindItem addItem = new TextSearchBindItem();
  94. addItem.PageCount = e.Items[e.CurrentPage].Count;
  95. addItem.ShowPageIndex = e.CurrentPage;
  96. }
  97. {
  98. foreach (TextSearchItem item in e.Items[e.CurrentPage])
  99. {
  100. TextSearchBindItem addItem = new TextSearchBindItem();
  101. addItem.BindProperty.PageIndex = item.PageIndex;
  102. addItem.BindProperty.TextContent = item.TextContent;
  103. addItem.BindProperty.TextRect = item.TextRect;
  104. addItem.BindProperty.SearchWord = keywords;
  105. addItem.BindProperty.HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00);
  106. addItem.BindProperty.PageRotate = item.PageRotate;
  107. lists.Add(addItem);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. private async void SearchText(object obj)
  114. {
  115. if (PDFViewer != null && textSearch != null)
  116. {
  117. issearched = false;
  118. C_Search_Options option = C_Search_Options.Search_Case_Insensitive;
  119. textSearch.TextSearchDocument = PDFViewer.Document;
  120. searchResults.Clear();
  121. lists.Clear();
  122. textSearch.SearchText(SearchContent, option);
  123. }
  124. while (issearched == false)
  125. await Task.Delay(10);
  126. foreach (var item in lists)
  127. searchResults.Add(item);
  128. SearchCount = TotalCount;
  129. if (TotalCount == 0)
  130. IsEmpty = true;
  131. else
  132. IsEmpty = false;
  133. }
  134. private void SearchChanged(object obj)
  135. {
  136. if (PDFViewer != null && textSearch != null)
  137. {
  138. TextSearchBindItem currentItem = obj as TextSearchBindItem;
  139. if (currentItem != null && PDFViewer != null)
  140. {
  141. List<TextSearchItem> pageTextList = new List<TextSearchItem>();
  142. pageTextList.Add(new TextSearchItem()
  143. {
  144. PageIndex = currentItem.BindProperty.PageIndex,
  145. TextRect = currentItem.BindProperty.TextRect,
  146. TextContent = currentItem.BindProperty.TextContent,
  147. PageRotate = currentItem.BindProperty.PageRotate
  148. });
  149. PDFViewer.SetPageSelectText(pageTextList, new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00)));
  150. }
  151. }
  152. }
  153. public bool IsNavigationTarget(NavigationContext navigationContext)
  154. {
  155. return true;
  156. }
  157. public void OnNavigatedFrom(NavigationContext navigationContext)
  158. {
  159. return;
  160. }
  161. private CPDFViewer PDFViewer;
  162. public void OnNavigatedTo(NavigationContext navigationContext)
  163. {
  164. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  165. if (PDFViewer != null)
  166. {
  167. }
  168. }
  169. }
  170. }