CPDFSearchControl.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. pdfView = newPDFView;
  46. }
  47. private void PDFSearch_Loaded(object sender, RoutedEventArgs e)
  48. {
  49. textSearch = new PDFTextSearch();
  50. SearchInput.SearchEvent += SearchInput_SearchEvent;
  51. SearchInput.ClearEvent += SearchInput_ClearEvent;
  52. textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
  53. SearchResult.SelectionChanged += SearchResult_SelectionChanged;
  54. }
  55. /// <summary>
  56. /// 清除搜索
  57. /// </summary>
  58. private void SearchInput_ClearEvent(object sender, EventArgs e)
  59. {
  60. SearchResult?.SetSearchResult(null);
  61. ResultText.Text = string.Empty;
  62. }
  63. /// <summary>
  64. ///高亮上一个搜索结果
  65. /// </summary>
  66. private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
  67. {
  68. if (currentHighLightIndex > 0)
  69. {
  70. currentHighLightIndex--;
  71. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  72. HighLightSelectResult(result);
  73. }
  74. }
  75. /// <summary>
  76. /// 高亮下一个搜索结果
  77. /// </summary>
  78. private void SearchInput_FindNextEvent(object sender, EventArgs e)
  79. {
  80. currentHighLightIndex++;
  81. if (currentHighLightIndex >= 0)
  82. {
  83. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  84. HighLightSelectResult(result);
  85. }
  86. }
  87. /// <summary>
  88. /// 高亮选中搜索结果
  89. /// </summary>
  90. private void SearchResult_SelectionChanged(object sender, int e)
  91. {
  92. currentHighLightIndex = e;
  93. BindSearchResult result = SearchResult.GetSelectItem();
  94. HighLightSelectResult(result);
  95. ResultText.Text = string.Format("{0}/{1} results found",e+1,ResultCount);
  96. }
  97. /// <summary>
  98. /// 搜索完成绑定数据
  99. /// </summary>
  100. private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
  101. {
  102. Dispatcher.Invoke(() =>
  103. {
  104. List<BindSearchResult> resultList = new List<BindSearchResult>();
  105. foreach (int pageIndex in e.Items.Keys)
  106. {
  107. List<TextSearchItem> textSearchItems = e.Items[pageIndex];
  108. foreach (TextSearchItem item in textSearchItems)
  109. {
  110. resultList.Add(new BindSearchResult()
  111. {
  112. PageIndex = item.PageIndex,
  113. TextContent = item.TextContent,
  114. TextRect = item.TextRect,
  115. SearchWord = keyWord,
  116. HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
  117. PageRotate = item.PageRotate
  118. });
  119. }
  120. }
  121. SearchResult.SetSearchResult(resultList);
  122. ResultCount=resultList.Count;
  123. if (resultList==null || resultList.Count==0)
  124. {
  125. ResultText.Text= string.Empty;
  126. }
  127. else
  128. {
  129. ResultText.Text = string.Format("{0} results found", ResultCount);
  130. }
  131. });
  132. }
  133. /// <summary>
  134. /// 搜索执行事件
  135. /// </summary>
  136. private void SearchInput_SearchEvent(object sender, string e)
  137. {
  138. if (string.IsNullOrEmpty(e))
  139. {
  140. return;
  141. }
  142. if (pdfView == null || pdfView.Document == null)
  143. {
  144. return;
  145. }
  146. if (textSearch != null && textSearch.CanDoSearch)
  147. {
  148. keyWord = e;
  149. textSearch.TextSearchDocument = pdfView.Document;
  150. textSearch.SearchText(e, C_Search_Options.Search_Case_Insensitive);
  151. }
  152. }
  153. /// <summary>
  154. /// 高亮搜索结果
  155. /// </summary>
  156. private void HighLightSelectResult(BindSearchResult result)
  157. {
  158. if (result == null)
  159. {
  160. return;
  161. }
  162. List<TextSearchItem> selectList = new List<TextSearchItem>();
  163. selectList.Add(new TextSearchItem()
  164. {
  165. PageIndex = result.PageIndex,
  166. TextRect = result.TextRect,
  167. TextContent = result.TextContent,
  168. PageRotate = result.PageRotate,
  169. });
  170. pdfView.SetPageSelectText(selectList, highLightBrush);
  171. }
  172. }
  173. }