CPDFSearchResultUI.xaml.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace compdfkit_tools.PDFControlUI
  21. {
  22. /// <summary>
  23. /// 搜索结果控件
  24. /// </summary>
  25. public partial class CPDFSearchResultUI : UserControl
  26. {
  27. /// <summary>
  28. /// 搜索结果列表点击选中事件
  29. /// </summary>
  30. public event EventHandler<int> SelectionChanged;
  31. /// <summary>
  32. /// 绑定搜索结果集合
  33. /// </summary>
  34. private ObservableCollection<TextBindData> searchResults;
  35. public CPDFSearchResultUI()
  36. {
  37. InitializeComponent();
  38. searchResults = new ObservableCollection<TextBindData>();
  39. ICollectionView groupView = CollectionViewSource.GetDefaultView(searchResults);
  40. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(TextBindData.ShowPageIndex)));
  41. }
  42. /// <summary>
  43. /// 选中改变事件
  44. /// </summary>
  45. private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  46. {
  47. InvokeSelectionChangedEvent(ResultListControl.SelectedIndex);
  48. }
  49. /// <summary>
  50. /// 绑定搜索结果集合
  51. /// </summary>
  52. /// <param name="results"></param>
  53. public void SetSearchResult(List<BindSearchResult> results)
  54. {
  55. searchResults.Clear();
  56. if (results == null || results.Count == 0)
  57. {
  58. ResultListControl.ItemsSource = null;
  59. NoResultText.Visibility = Visibility.Visible;
  60. return;
  61. }
  62. foreach (BindSearchResult item in results)
  63. {
  64. searchResults.Add(new TextBindData()
  65. {
  66. BindProperty = item
  67. });
  68. }
  69. ResultListControl.ItemsSource = searchResults;
  70. ResultListControl.Visibility = Visibility.Visible;
  71. NoResultText.Visibility = Visibility.Collapsed;
  72. }
  73. /// <summary>
  74. /// 获取选中搜索结果
  75. /// </summary>
  76. /// <returns>搜索结果</returns>
  77. public BindSearchResult GetSelectItem()
  78. {
  79. TextBindData bindData = ResultListControl.SelectedItem as TextBindData;
  80. if (bindData != null)
  81. {
  82. return bindData.BindProperty;
  83. }
  84. return null;
  85. }
  86. /// <summary>
  87. /// 获取搜索结果对象
  88. /// </summary>
  89. /// <param name="index">指定索引</param>
  90. /// <returns></returns>
  91. public BindSearchResult GetItem(int index)
  92. {
  93. if(index < 0)
  94. {
  95. return null;
  96. }
  97. if(ResultListControl.HasItems && ResultListControl.Items.Count>index)
  98. {
  99. TextBindData bindData = ResultListControl.Items[index] as TextBindData;
  100. if(bindData != null)
  101. {
  102. return bindData.BindProperty;
  103. }
  104. }
  105. return null;
  106. }
  107. /// <summary>
  108. /// 清除选中结果
  109. /// </summary>
  110. public void ClearSelection()
  111. {
  112. int oldSelectionIndex = ResultListControl.SelectedIndex;
  113. ResultListControl.UnselectAll();
  114. if (oldSelectionIndex != ResultListControl.SelectedIndex)
  115. {
  116. InvokeSelectionChangedEvent(ResultListControl.SelectedIndex);
  117. }
  118. }
  119. /// <summary>
  120. /// 设置选中结果
  121. /// </summary>
  122. /// <param name="selectIndex">选中索引</param>
  123. public void SelectItem(int selectIndex)
  124. {
  125. if (ResultListControl.SelectedIndex != selectIndex)
  126. {
  127. ResultListControl.SelectedIndex = selectIndex;
  128. }
  129. }
  130. internal void InvokeSelectionChangedEvent(int selectionIndex)
  131. {
  132. SelectionChanged?.Invoke(this, selectionIndex);
  133. }
  134. }
  135. public class BindSearchResult
  136. {
  137. /// <summary>
  138. /// 页面索引
  139. /// </summary>
  140. public int PageIndex { get; set; }
  141. /// <summary>
  142. /// 搜索文字结果
  143. /// </summary>
  144. public string TextContent { get; set; }
  145. /// <summary>
  146. /// 搜索高亮颜色
  147. /// </summary>
  148. public Color HighLightColor { get; set; }
  149. /// <summary>
  150. /// 搜索关键字
  151. /// </summary>
  152. public string SearchWord { get; set; }
  153. /// <summary>
  154. /// 搜索结果范围区域
  155. /// </summary>
  156. public Rect TextRect { get; set; }
  157. /// <summary>
  158. /// 页面旋转角度
  159. /// </summary>
  160. public int PageRotate { get; set; }
  161. }
  162. internal class TextBindData
  163. {
  164. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  165. public BindSearchResult BindProperty { get; set; }
  166. public TextBindData()
  167. {
  168. BindProperty = new BindSearchResult();
  169. }
  170. }
  171. internal class SearchResultBindHelper : DependencyObject
  172. {
  173. public static FlowDocument GetDocumentBind(DependencyObject obj)
  174. {
  175. return (FlowDocument)obj.GetValue(DocumentBindProperty);
  176. }
  177. public static void SetDocumentBind(DependencyObject obj, FlowDocument value)
  178. {
  179. obj.SetValue(DocumentBindProperty, value);
  180. }
  181. public static readonly DependencyProperty DocumentBindProperty =
  182. DependencyProperty.RegisterAttached("DocumentBind",
  183. typeof(BindSearchResult),
  184. typeof(SearchResultBindHelper),
  185. new FrameworkPropertyMetadata
  186. {
  187. BindsTwoWayByDefault = false,
  188. PropertyChangedCallback = (obj, e) =>
  189. {
  190. RichTextBox richTextBox = obj as RichTextBox;
  191. BindSearchResult bindItem = e.NewValue as BindSearchResult;
  192. if (richTextBox != null && bindItem != null)
  193. {
  194. richTextBox.Document = GetFlowDocument(bindItem.TextContent.Trim(), bindItem.SearchWord, bindItem.HighLightColor);
  195. }
  196. }
  197. });
  198. /// <summary>
  199. /// 获取文档流数据
  200. /// </summary>
  201. /// <param name="content">搜索文字结果</param>
  202. /// <param name="keyword">搜索关键字</param>
  203. /// <param name="textColor">高亮文字颜色</param>
  204. /// <returns>文档流数据</returns>
  205. public static FlowDocument GetFlowDocument(string content, string keyword, Color textColor)
  206. {
  207. FlowDocument Document = new FlowDocument();
  208. Paragraph textPara = new Paragraph();
  209. Document.Blocks.Add(textPara);
  210. List<int> indexList = new List<int>();
  211. content = Regex.Replace(content, "[\r\n]", " ");
  212. if (keyword.Length > 0)
  213. {
  214. for (int i = 0; i < content.Length && i >= 0;)
  215. {
  216. i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase);
  217. if (i == -1)
  218. {
  219. break;
  220. }
  221. if (indexList.Contains(i) == false)
  222. {
  223. indexList.Add(i);
  224. }
  225. i += keyword.Length;
  226. }
  227. }
  228. List<string> splitList = new List<string>();
  229. int lastIndex = -1;
  230. foreach (int index in indexList)
  231. {
  232. string prevStr = string.Empty;
  233. if (lastIndex == -1)
  234. {
  235. prevStr = content.Substring(0, index);
  236. }
  237. else
  238. {
  239. prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1);
  240. }
  241. if (prevStr != string.Empty)
  242. {
  243. splitList.Add(prevStr);
  244. }
  245. splitList.Add(content.Substring(index, keyword.Length));
  246. lastIndex = index;
  247. }
  248. if (indexList.Count > 0)
  249. {
  250. lastIndex = indexList[indexList.Count - 1];
  251. if (content.Length > lastIndex + keyword.Length)
  252. {
  253. splitList.Add(content.Substring(lastIndex + keyword.Length));
  254. }
  255. }
  256. TextBlock addBlock = new TextBlock();
  257. foreach (string textappend in splitList)
  258. {
  259. Run textRun = new Run(textappend);
  260. if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase))
  261. {
  262. textRun.Background = new SolidColorBrush(textColor);
  263. }
  264. addBlock.Inlines.Add(textRun);
  265. }
  266. addBlock.TextTrimming = TextTrimming.CharacterEllipsis;
  267. textPara.Inlines.Add(addBlock);
  268. return Document;
  269. }
  270. }
  271. }