CPDFSearchResultUI.xaml.cs 8.6 KB

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