SearchContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. private string searchTextContext;
  79. public string SearchTextContext
  80. {
  81. get { return searchTextContext; }
  82. set
  83. {
  84. SetProperty(ref searchTextContext, value);
  85. }
  86. }
  87. #endregion
  88. public SearchContentViewModel()
  89. {
  90. textSearch = new PDFTextSearch();
  91. SearchItemList = new ObservableCollection<SearchItem>();
  92. MyProperty = new ObservableCollection<string>() { "1", "2" };
  93. SearchChangedCommand = new DelegateCommand<object>(SearchChanged);
  94. CleanCommand = new DelegateCommand(clean);
  95. ICollectionView groupView = CollectionViewSource.GetDefaultView(SearchItemList);
  96. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(SearchItem.ShowPageIndex)));
  97. textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler;
  98. textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
  99. Completed = Visibility.Collapsed;
  100. }
  101. private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
  102. {
  103. App.Current.Dispatcher.Invoke(() =>
  104. {
  105. Completed = Visibility.Visible;
  106. SearchCount = e.TotalCount.ToString();
  107. List<TextSearchItem> pageTextList = new List<TextSearchItem>();
  108. foreach (var item in SearchItemList)
  109. {
  110. item.TextProperty.SearchItem.PaintBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
  111. pageTextList.Add(item.TextProperty.SearchItem);
  112. }
  113. PDFViewer.SetPageSelectText(pageTextList);
  114. });
  115. }
  116. private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e)
  117. {
  118. App.Current.Dispatcher.Invoke(() =>
  119. {
  120. if (e.Items.ContainsKey(e.CurrentPage))
  121. {
  122. foreach (TextSearchItem item in e.Items[e.CurrentPage])
  123. {
  124. SearchItem addItem = new SearchItem();
  125. addItem.TextProperty = new TextBindProperty();
  126. addItem.TextProperty.PageIndex = item.PageIndex;
  127. addItem.TextProperty.TextContent = item.TextContent;
  128. addItem.TextProperty.TextRect = item.TextRect;
  129. addItem.TextProperty.SearchWord = CurrentSearchText;
  130. addItem.TextProperty.HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00);
  131. addItem.TextProperty.PageRotate = item.PageRotate;
  132. addItem.TextProperty.SearchItem = item;
  133. SearchItemList.Add(addItem);
  134. }
  135. }
  136. SearchCount = e.TotalCount.ToString();
  137. });
  138. }
  139. /// <summary>
  140. /// 文字搜索
  141. /// </summary>
  142. public async void SearchText(string Text)
  143. {
  144. //防止正在进行搜索,先取消一次。(若没有搜索,该方法不进行操作)
  145. textSearch.CancleSearch();
  146. //为了防止死循环,当超过十次,即0.1秒还是不能进行搜索,则取消本次搜索
  147. int counter = 0;
  148. while (!textSearch.CanDoSearch)
  149. {
  150. await Task.Delay(10);
  151. if (counter > 10)
  152. {
  153. return;
  154. }
  155. }
  156. //成功取消后,清除之前的结果,并搜索新内容
  157. SearchItemList.Clear();
  158. Completed = Visibility.Collapsed;
  159. if (CaseInsensitive)
  160. {
  161. textSearch.SearchText(Text, C_Search_Options.Search_Case_Insensitive);
  162. }
  163. else
  164. {
  165. textSearch.SearchText(Text, C_Search_Options.Search_Case_Sensitive);
  166. }
  167. CurrentSearchText = Text;
  168. }
  169. /// <summary>
  170. /// 根据UI的Tag与绑定的对象来创建注释的方法
  171. /// </summary>
  172. public void CreateAnnotate(object item, string Tag)
  173. {
  174. SearchItem searchItem = item as SearchItem;
  175. if (searchItem == null)
  176. {
  177. return;
  178. }
  179. switch (Tag)
  180. {
  181. case "Round":
  182. CircleAnnotArgs circleannotArgs = new CircleAnnotArgs();
  183. circleannotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect);
  184. circleannotArgs.Transparency = 1;
  185. circleannotArgs.BgColor = Colors.Transparent;
  186. circleannotArgs.LineColor = Colors.Red;
  187. circleannotArgs.LineWidth = 2;
  188. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, circleannotArgs);
  189. break;
  190. case "Rectangle":
  191. SquareAnnotArgs squareAnnotArgs = new SquareAnnotArgs();
  192. squareAnnotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect);
  193. squareAnnotArgs.Transparency = 1;
  194. squareAnnotArgs.BgColor = Colors.Transparent;
  195. squareAnnotArgs.LineColor = Colors.Red;
  196. squareAnnotArgs.LineWidth = 2;
  197. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, squareAnnotArgs);
  198. break;
  199. case "Highlight":
  200. TextHighlightAnnotArgs highlightAnnotArgs = new TextHighlightAnnotArgs();
  201. highlightAnnotArgs.Color = Settings.Default.AppProperties.Annotate.HighLightColor;
  202. highlightAnnotArgs.Transparency = 1;
  203. highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  204. highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  205. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, highlightAnnotArgs);
  206. break;
  207. case "Underline":
  208. TextUnderlineAnnotArgs underlineAnnotArgs = new TextUnderlineAnnotArgs();
  209. underlineAnnotArgs.Color = Settings.Default.AppProperties.Annotate.UnderLineColor;
  210. underlineAnnotArgs.Transparency = 1;
  211. underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  212. underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  213. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, underlineAnnotArgs);
  214. break;
  215. case "Strikethrough":
  216. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  217. strikeoutAnnotArgs.Color = Settings.Default.AppProperties.Annotate.StrikethroughColor;
  218. strikeoutAnnotArgs.Transparency = 1;
  219. strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  220. strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  221. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, strikeoutAnnotArgs);
  222. break;
  223. default:
  224. break;
  225. }
  226. }
  227. /// <summary>
  228. /// 选项改变时,跳转并且高亮搜索内容
  229. /// </summary>
  230. private void SearchChanged(object obj)
  231. {
  232. if (PDFViewer != null && textSearch != null)
  233. {
  234. SearchItem currentItem = obj as SearchItem;
  235. if (currentItem != null && PDFViewer != null)
  236. {
  237. List<TextSearchItem> pageTextList = new List<TextSearchItem>();
  238. currentItem.TextProperty.SearchItem.BorderBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0x17, 0x70, 0xF4));
  239. currentItem.TextProperty.SearchItem.Padding = 3;
  240. currentItem.TextProperty.SearchItem.BorderThickness = 1;
  241. pageTextList.Add(currentItem.TextProperty.SearchItem);
  242. PDFViewer.HighLightSearchText(pageTextList);
  243. }
  244. }
  245. }
  246. private void clean()
  247. {
  248. SearchTextContext = "";
  249. PDFViewer.ClearPageSelectText();
  250. SearchItemList.Clear();
  251. viewContentViewModel.OpenBOTA = false;
  252. }
  253. public bool IsNavigationTarget(NavigationContext navigationContext)
  254. {
  255. return true;
  256. }
  257. public void OnNavigatedFrom(NavigationContext navigationContext)
  258. {
  259. return;
  260. }
  261. public void OnNavigatedTo(NavigationContext navigationContext)
  262. {
  263. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  264. if (PDFViewer != null)
  265. {
  266. textSearch.TextSearchDocument = PDFViewer.Document;
  267. }
  268. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  269. }
  270. }
  271. }