SearchContentViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using PDF_Master.Model;
  5. using PDF_Master.Model.BOTA;
  6. using PDF_Master.Properties;
  7. using PDF_Master.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_Master.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. counter++;
  156. }
  157. //成功取消后,清除之前的结果,并搜索新内容
  158. SearchItemList.Clear();
  159. Completed = Visibility.Collapsed;
  160. if (CaseInsensitive)
  161. {
  162. textSearch.SearchText(Text, C_Search_Options.Search_Case_Insensitive, viewContentViewModel.PassWord);
  163. }
  164. else
  165. {
  166. textSearch.SearchText(Text, C_Search_Options.Search_Case_Sensitive, viewContentViewModel.PassWord);
  167. }
  168. CurrentSearchText = Text;
  169. }
  170. /// <summary>
  171. /// 根据UI的Tag与绑定的对象来创建注释的方法
  172. /// </summary>
  173. public void CreateAnnotate(object item, string Tag)
  174. {
  175. SearchItem searchItem = item as SearchItem;
  176. if (searchItem == null)
  177. {
  178. return;
  179. }
  180. switch (Tag)
  181. {
  182. case "Round":
  183. CircleAnnotArgs circleannotArgs = new CircleAnnotArgs();
  184. circleannotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect);
  185. circleannotArgs.Transparency = 1;
  186. circleannotArgs.BgColor = Colors.Transparent;
  187. circleannotArgs.LineColor = Colors.Red;
  188. circleannotArgs.LineWidth = 1;
  189. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, circleannotArgs);
  190. //PDFViewer.ToolManager.CurrentAnnotArgs.EventType
  191. break;
  192. case "Rectangle":
  193. SquareAnnotArgs squareAnnotArgs = new SquareAnnotArgs();
  194. squareAnnotArgs.ClientRect = Helper.DpiHelpers.GetRawRelatedRect(searchItem.TextProperty.TextRect);
  195. squareAnnotArgs.Transparency = 1;
  196. squareAnnotArgs.BgColor = Colors.Transparent;
  197. squareAnnotArgs.LineColor = Colors.Red;
  198. squareAnnotArgs.LineWidth = 1;
  199. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, squareAnnotArgs);
  200. break;
  201. case "Highlight":
  202. TextHighlightAnnotArgs highlightAnnotArgs = new TextHighlightAnnotArgs();
  203. highlightAnnotArgs.Color = Settings.Default.AppProperties.Annotate.HighLightColor;
  204. highlightAnnotArgs.Transparency = 1;
  205. highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  206. highlightAnnotArgs.PagesHiglightRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  207. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, highlightAnnotArgs);
  208. break;
  209. case "Underline":
  210. TextUnderlineAnnotArgs underlineAnnotArgs = new TextUnderlineAnnotArgs();
  211. underlineAnnotArgs.Color = Settings.Default.AppProperties.Annotate.UnderLineColor;
  212. underlineAnnotArgs.Transparency = 1;
  213. underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  214. underlineAnnotArgs.PagesUnderlineRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  215. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, underlineAnnotArgs);
  216. break;
  217. case "Strikethrough":
  218. TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
  219. strikeoutAnnotArgs.Color = Settings.Default.AppProperties.Annotate.StrikethroughColor;
  220. strikeoutAnnotArgs.Transparency = 1;
  221. strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex] = new List<Rect>();
  222. strikeoutAnnotArgs.PagesStrikeoutRectList[searchItem.TextProperty.PageIndex].Add(searchItem.TextProperty.TextRect);
  223. PDFViewer.CreatePageAnnot(searchItem.TextProperty.PageIndex, strikeoutAnnotArgs);
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. /// <summary>
  230. /// 选项改变时,跳转并且高亮搜索内容
  231. /// </summary>
  232. private void SearchChanged(object obj)
  233. {
  234. if (PDFViewer != null && textSearch != null)
  235. {
  236. System.Collections.IList currentItem = obj as System.Collections.IList;
  237. if (currentItem != null && PDFViewer != null)
  238. {
  239. CleaSelect();
  240. List<TextSearchItem> pageTextList = new List<TextSearchItem>();
  241. for (int i = 0; i < currentItem.Count; i++)
  242. {
  243. if (currentItem[i] is SearchItem)
  244. {
  245. (currentItem[i] as SearchItem).TextProperty.SearchItem.PaintBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xD6, 0x00));
  246. (currentItem[i] as SearchItem).TextProperty.SearchItem.BorderBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0x17, 0x70, 0xF4));
  247. (currentItem[i] as SearchItem).TextProperty.SearchItem.Padding = new Thickness(3);
  248. (currentItem[i] as SearchItem).TextProperty.SearchItem.BorderThickness = 1;
  249. pageTextList.Add((currentItem[i] as SearchItem).TextProperty.SearchItem);
  250. }
  251. }
  252. if (currentItem.Count > 0)
  253. {
  254. PDFViewer.HighLightSearchText(pageTextList);
  255. }
  256. }
  257. }
  258. }
  259. private void clean()
  260. {
  261. SearchTextContext = "";
  262. PDFViewer.ClearPageSelectText();
  263. SearchItemList.Clear();
  264. viewContentViewModel.OpenBOTA = false;
  265. }
  266. public void CleaSelect()
  267. {
  268. PDFViewer.ClearPageHighLightText(new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00)));
  269. }
  270. public bool IsNavigationTarget(NavigationContext navigationContext)
  271. {
  272. return true;
  273. }
  274. public void OnNavigatedFrom(NavigationContext navigationContext)
  275. {
  276. //切换到缩略图等模块时,需要清空搜索结果显示
  277. if (SearchItemList.Count>0)
  278. {
  279. PDFViewer.ClearPageSelectText();
  280. }
  281. return;
  282. }
  283. public void OnNavigatedTo(NavigationContext navigationContext)
  284. {
  285. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  286. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  287. if (PDFViewer != null)
  288. {
  289. textSearch.TextSearchDocument = PDFViewer.Document;
  290. //是否使用同一个Document对象,如果传入文件路径,中间层则会自动再创建一个Document对象
  291. textSearch.UsePassedDoc = true;
  292. }
  293. //如果之前已经有搜索结果,切换到搜索时,需要恢复高亮显示
  294. if (SearchItemList.Count > 0)
  295. {
  296. List<TextSearchItem> pageTextList = new List<TextSearchItem>();
  297. foreach (var item in SearchItemList)
  298. {
  299. item.TextProperty.SearchItem.PaintBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
  300. pageTextList.Add(item.TextProperty.SearchItem);
  301. }
  302. PDFViewer.SetPageSelectText(pageTextList);
  303. }
  304. }
  305. }
  306. }