SearchContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.BOTA;
  6. using PDF_Office.Properties;
  7. using PDF_Office.Views.BOTA;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.ComponentModel;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Data;
  20. using System.Windows.Media;
  21. using System.Windows.Threading;
  22. namespace PDF_Office.ViewModels.BOTA
  23. {
  24. class SearchContentViewModel : BindableBase, INavigationAware
  25. {
  26. #region 属性
  27. private Visibility completed;
  28. public Visibility Completed
  29. {
  30. get { return completed; }
  31. set
  32. {
  33. SetProperty(ref completed, value);
  34. }
  35. }
  36. private string searchCount;
  37. public string SearchCount
  38. {
  39. get { return searchCount; }
  40. set
  41. {
  42. SetProperty(ref searchCount, value);
  43. }
  44. }
  45. private ObservableCollection<SearchItem> searchItemList;
  46. public ObservableCollection<SearchItem> SearchItemList
  47. {
  48. get { return searchItemList; }
  49. set
  50. {
  51. SetProperty(ref searchItemList, value);
  52. }
  53. }
  54. private PDFTextSearch textSearch;
  55. private CPDFViewer PDFViewer;
  56. private ViewContentViewModel viewContentViewModel;
  57. private string CurrentSearchText = "";
  58. private ObservableCollection<string> myVar;
  59. public ObservableCollection<string> MyProperty
  60. {
  61. get { return myVar; }
  62. set
  63. {
  64. SetProperty(ref myVar, value);
  65. }
  66. }
  67. private bool caseInsensitive = true;
  68. public bool CaseInsensitive
  69. {
  70. get { return caseInsensitive; }
  71. set
  72. {
  73. SetProperty(ref caseInsensitive, value);
  74. }
  75. }
  76. public DelegateCommand<object> SearchChangedCommand { get; set; }
  77. public DelegateCommand CleanCommand { get; set; }
  78. #endregion
  79. public SearchContentViewModel()
  80. {
  81. textSearch = new PDFTextSearch();
  82. SearchItemList = new ObservableCollection<SearchItem>();
  83. MyProperty = new ObservableCollection<string>() { "1", "2" };
  84. SearchChangedCommand = new DelegateCommand<object>(SearchChanged);
  85. CleanCommand = new DelegateCommand(clean);
  86. ICollectionView groupView = CollectionViewSource.GetDefaultView(SearchItemList);
  87. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SearchItem.ShowPageIndex)));
  88. textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler;
  89. textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
  90. Completed = Visibility.Collapsed;
  91. }
  92. private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
  93. {
  94. App.Current.Dispatcher.Invoke(() =>
  95. {
  96. Completed = Visibility.Visible;
  97. SearchCount = e.TotalCount.ToString();
  98. });
  99. }
  100. private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e)
  101. {
  102. App.Current.Dispatcher.Invoke(() =>
  103. {
  104. if (e.Items.ContainsKey(e.CurrentPage))
  105. {
  106. foreach (TextSearchItem item in e.Items[e.CurrentPage])
  107. {
  108. SearchItem addItem = new SearchItem();
  109. addItem.TextProperty = new TextBindProperty();
  110. addItem.TextProperty.PageIndex = item.PageIndex;
  111. addItem.TextProperty.TextContent = item.TextContent;
  112. addItem.TextProperty.TextRect = item.TextRect;
  113. addItem.TextProperty.SearchWord = CurrentSearchText;
  114. addItem.TextProperty.HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00);
  115. addItem.TextProperty.PageRotate = item.PageRotate;
  116. SearchItemList.Add(addItem);
  117. }
  118. }
  119. SearchCount = e.TotalCount.ToString();
  120. });
  121. }
  122. /// <summary>
  123. /// 文字搜索
  124. /// </summary>
  125. public async void SearchText(string Text)
  126. {
  127. //防止正在进行搜索,先取消一次。(若没有搜索,该方法不进行操作)
  128. textSearch.CancleSearch();
  129. //为了防止死循环,当超过十次,即0.1秒还是不能进行搜索,则取消本次搜索
  130. int counter = 0;
  131. while (!textSearch.CanDoSearch)
  132. {
  133. await Task.Delay(10);
  134. if (counter > 10)
  135. {
  136. return;
  137. }
  138. }
  139. //成功取消后,清除之前的结果,并搜索新内容
  140. SearchItemList.Clear();
  141. Completed = Visibility.Collapsed;
  142. if (CaseInsensitive)
  143. {
  144. textSearch.SearchText(Text, C_Search_Options.Search_Case_Insensitive);
  145. }
  146. else
  147. {
  148. textSearch.SearchText(Text, C_Search_Options.Search_Case_Sensitive);
  149. }
  150. CurrentSearchText = Text;
  151. }
  152. /// <summary>
  153. /// 根据UI的Tag与绑定的对象来创建注释的方法
  154. /// </summary>
  155. public void CreateAnnotate(object item, string Tag)
  156. {
  157. SearchItem searchItem = item as SearchItem;
  158. if (searchItem == null)
  159. {
  160. return;
  161. }
  162. switch (Tag)
  163. {
  164. case "Round":
  165. CircleAnnotArgs circleannotArgs = new CircleAnnotArgs();
  166. circleannotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect);
  167. circleannotArgs.Transparency = 1;
  168. circleannotArgs.BgColor = Colors.Transparent;
  169. circleannotArgs.LineColor = Colors.Red;
  170. circleannotArgs.LineWidth = 2;
  171. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, circleannotArgs);
  172. break;
  173. case "Rectangle":
  174. SquareAnnotArgs squareAnnotArgs = new SquareAnnotArgs();
  175. squareAnnotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect);
  176. squareAnnotArgs.Transparency = 1;
  177. squareAnnotArgs.BgColor = Colors.Transparent;
  178. squareAnnotArgs.LineColor = Colors.Red;
  179. squareAnnotArgs.LineWidth = 2;
  180. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, squareAnnotArgs);
  181. break;
  182. case "Highlight":
  183. TextHighlightAnnotArgs highlightAnnotArgs = new TextHighlightAnnotArgs();
  184. highlightAnnotArgs.Color = Settings.Default.AppProperties.Annotate.HighLightColor;
  185. highlightAnnotArgs.Transparency = 1;
  186. highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  187. highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  188. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, highlightAnnotArgs);
  189. break;
  190. case "Underline":
  191. TextUnderlineAnnotArgs underlineAnnotArgs = new TextUnderlineAnnotArgs();
  192. underlineAnnotArgs.Color = Settings.Default.AppProperties.Annotate.UnderLineColor;
  193. underlineAnnotArgs.Transparency = 1;
  194. underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  195. underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  196. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, underlineAnnotArgs);
  197. break;
  198. case "Strikethrough":
  199. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  200. strikeoutAnnotArgs.Color = Settings.Default.AppProperties.Annotate.StrikethroughColor;
  201. strikeoutAnnotArgs.Transparency = 1;
  202. strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  203. strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  204. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, strikeoutAnnotArgs);
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. /// <summary>
  211. /// 选项改变时,跳转并且高亮搜索内容
  212. /// </summary>
  213. private void SearchChanged(object obj)
  214. {
  215. if (PDFViewer != null && textSearch != null)
  216. {
  217. SearchItem currentItem = obj as SearchItem;
  218. if (currentItem != null && PDFViewer != null)
  219. {
  220. List<TextSearchItem> pageTextList = new List<TextSearchItem>();
  221. pageTextList.Add(new TextSearchItem()
  222. {
  223. PageIndex = currentItem.TextProperty.PageIndex,
  224. TextRect = currentItem.TextProperty.TextRect,
  225. TextContent = currentItem.TextProperty.TextContent,
  226. PageRotate = currentItem.TextProperty.PageRotate
  227. });
  228. PDFViewer.SetPageSelectText(pageTextList, new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00)));
  229. }
  230. }
  231. }
  232. private void clean()
  233. {
  234. SearchItemList.Clear();
  235. viewContentViewModel.OpenBOTA = false;
  236. }
  237. public bool IsNavigationTarget(NavigationContext navigationContext)
  238. {
  239. return true;
  240. }
  241. public void OnNavigatedFrom(NavigationContext navigationContext)
  242. {
  243. return;
  244. }
  245. public void OnNavigatedTo(NavigationContext navigationContext)
  246. {
  247. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  248. if (PDFViewer != null)
  249. {
  250. textSearch.TextSearchDocument = PDFViewer.Document;
  251. }
  252. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  253. }
  254. }
  255. }