SearchContentViewModel.cs 12 KB

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