CPDFSearchControl.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using ComPDFKit.PDFPage;
  2. using compdfkit_tools.PDFControlUI;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace compdfkit_tools.PDFControl
  20. {
  21. /// <summary>
  22. /// UserControl1.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class CPDFSearchControl : UserControl
  25. {
  26. /// <summary>
  27. /// PDFViewer
  28. /// </summary>
  29. private CPDFViewer pdfView;
  30. private int currentHighLightIndex { get; set; } = -1;
  31. private PDFTextSearch textSearch;
  32. private string keyWord;
  33. private SolidColorBrush highLightBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
  34. private int ResultCount = 0;
  35. public CPDFSearchControl()
  36. {
  37. InitializeComponent();
  38. Loaded += PDFSearch_Loaded;
  39. }
  40. /// <summary>
  41. /// 设置PDFViewer
  42. /// </summary>
  43. public void InitWithPDFViewer(CPDFViewer newPDFView)
  44. {
  45. if(pdfView!=newPDFView)
  46. {
  47. ClearSearchResult();
  48. pdfView = newPDFView;
  49. }
  50. }
  51. private void PDFSearch_Loaded(object sender, RoutedEventArgs e)
  52. {
  53. textSearch = new PDFTextSearch();
  54. SearchInput.SearchEvent += SearchInput_SearchEvent;
  55. SearchInput.ClearEvent += SearchInput_ClearEvent;
  56. textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
  57. SearchResult.SelectionChanged += SearchResult_SelectionChanged;
  58. }
  59. /// <summary>
  60. /// 清除搜索
  61. /// </summary>
  62. private void SearchInput_ClearEvent(object sender, EventArgs e)
  63. {
  64. ClearSearchResult();
  65. }
  66. /// <summary>
  67. ///高亮上一个搜索结果
  68. /// </summary>
  69. private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
  70. {
  71. if (currentHighLightIndex > 0)
  72. {
  73. currentHighLightIndex--;
  74. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  75. HighLightSelectResult(result);
  76. }
  77. }
  78. /// <summary>
  79. /// 高亮下一个搜索结果
  80. /// </summary>
  81. private void SearchInput_FindNextEvent(object sender, EventArgs e)
  82. {
  83. currentHighLightIndex++;
  84. if (currentHighLightIndex >= 0)
  85. {
  86. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  87. HighLightSelectResult(result);
  88. }
  89. }
  90. /// <summary>
  91. /// 高亮选中搜索结果
  92. /// </summary>
  93. private void SearchResult_SelectionChanged(object sender, int e)
  94. {
  95. currentHighLightIndex = e;
  96. BindSearchResult result = SearchResult.GetSelectItem();
  97. HighLightSelectResult(result);
  98. ResultText.Text = string.Format("Results:{0}/{1}", e+1,ResultCount);
  99. }
  100. /// <summary>
  101. /// 搜索完成绑定数据
  102. /// </summary>
  103. private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
  104. {
  105. Dispatcher.Invoke(() =>
  106. {
  107. List<BindSearchResult> resultList = new List<BindSearchResult>();
  108. foreach (int pageIndex in e.Items.Keys)
  109. {
  110. List<TextSearchItem> textSearchItems = e.Items[pageIndex];
  111. foreach (TextSearchItem item in textSearchItems)
  112. {
  113. resultList.Add(new BindSearchResult()
  114. {
  115. PageIndex = item.PageIndex,
  116. TextContent = item.TextContent,
  117. TextRect = item.TextRect,
  118. SearchWord = keyWord,
  119. HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
  120. PageRotate = item.PageRotate
  121. });
  122. }
  123. }
  124. SearchResult.SetSearchResult(resultList);
  125. ResultCount=resultList.Count;
  126. if (resultList==null || resultList.Count==0)
  127. {
  128. ResultText.Text= string.Empty;
  129. }
  130. else
  131. {
  132. ResultText.Text = string.Format("{0} results found", ResultCount);
  133. }
  134. });
  135. }
  136. /// <summary>
  137. /// 搜索执行事件
  138. /// </summary>
  139. private void SearchInput_SearchEvent(object sender, string e)
  140. {
  141. if (string.IsNullOrEmpty(e))
  142. {
  143. return;
  144. }
  145. if (pdfView == null || pdfView.Document == null)
  146. {
  147. return;
  148. }
  149. if (textSearch != null && textSearch.CanDoSearch)
  150. {
  151. keyWord = e;
  152. textSearch.TextSearchDocument = pdfView.Document;
  153. textSearch.SearchText(e, C_Search_Options.Search_Case_Insensitive);
  154. }
  155. }
  156. /// <summary>
  157. /// 高亮搜索结果
  158. /// </summary>
  159. private void HighLightSelectResult(BindSearchResult result)
  160. {
  161. if (result == null)
  162. {
  163. return;
  164. }
  165. List<TextSearchItem> selectList = new List<TextSearchItem>();
  166. selectList.Add(new TextSearchItem()
  167. {
  168. PageIndex = result.PageIndex,
  169. TextRect = result.TextRect,
  170. TextContent = result.TextContent,
  171. PageRotate = result.PageRotate,
  172. });
  173. pdfView.SetPageSelectText(selectList, highLightBrush);
  174. }
  175. /// <summary>
  176. /// 清除搜索结果
  177. /// </summary>
  178. private void ClearSearchResult()
  179. {
  180. SearchResult?.SetSearchResult(null);
  181. ResultText.Text = string.Empty;
  182. SearchInput.SearchKeyWord=string.Empty;
  183. }
  184. }
  185. }