CPDFSearchResultUI.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. return;
  60. }
  61. foreach (BindSearchResult item in results)
  62. {
  63. searchResults.Add(new TextBindData()
  64. {
  65. BindProperty = item
  66. });
  67. }
  68. ResultListControl.ItemsSource = searchResults;
  69. }
  70. /// <summary>
  71. /// 获取选中搜索结果
  72. /// </summary>
  73. /// <returns>搜索结果</returns>
  74. public BindSearchResult GetSelectItem()
  75. {
  76. TextBindData bindData = ResultListControl.SelectedItem as TextBindData;
  77. if (bindData != null)
  78. {
  79. return bindData.BindProperty;
  80. }
  81. return null;
  82. }
  83. /// <summary>
  84. /// 获取搜索结果对象
  85. /// </summary>
  86. /// <param name="index">指定索引</param>
  87. /// <returns></returns>
  88. public BindSearchResult GetItem(int index)
  89. {
  90. if(index < 0)
  91. {
  92. return null;
  93. }
  94. if(ResultListControl.HasItems && ResultListControl.Items.Count>index)
  95. {
  96. TextBindData bindData = ResultListControl.Items[index] as TextBindData;
  97. if(bindData != null)
  98. {
  99. return bindData.BindProperty;
  100. }
  101. }
  102. return null;
  103. }
  104. /// <summary>
  105. /// 清除选中结果
  106. /// </summary>
  107. public void ClearSelection()
  108. {
  109. int oldSelectionIndex = ResultListControl.SelectedIndex;
  110. ResultListControl.UnselectAll();
  111. if (oldSelectionIndex != ResultListControl.SelectedIndex)
  112. {
  113. InvokeSelectionChangedEvent(ResultListControl.SelectedIndex);
  114. }
  115. }
  116. /// <summary>
  117. /// 设置选中结果
  118. /// </summary>
  119. /// <param name="selectIndex">选中索引</param>
  120. public void SelectItem(int selectIndex)
  121. {
  122. if (ResultListControl.SelectedIndex != selectIndex)
  123. {
  124. ResultListControl.SelectedIndex = selectIndex;
  125. }
  126. }
  127. internal void InvokeSelectionChangedEvent(int selectionIndex)
  128. {
  129. SelectionChanged?.Invoke(this, selectionIndex);
  130. }
  131. }
  132. public class BindSearchResult
  133. {
  134. /// <summary>
  135. /// 页面索引
  136. /// </summary>
  137. public int PageIndex { get; set; }
  138. /// <summary>
  139. /// 搜索文字结果
  140. /// </summary>
  141. public string TextContent { get; set; }
  142. /// <summary>
  143. /// 搜索高亮颜色
  144. /// </summary>
  145. public Color HighLightColor { get; set; }
  146. /// <summary>
  147. /// 搜索关键字
  148. /// </summary>
  149. public string SearchWord { get; set; }
  150. /// <summary>
  151. /// 搜索结果范围区域
  152. /// </summary>
  153. public Rect TextRect { get; set; }
  154. /// <summary>
  155. /// 页面旋转角度
  156. /// </summary>
  157. public int PageRotate { get; set; }
  158. }
  159. internal class TextBindData
  160. {
  161. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  162. public BindSearchResult BindProperty { get; set; }
  163. public TextBindData()
  164. {
  165. BindProperty = new BindSearchResult();
  166. }
  167. }
  168. internal class SearchResultBindHelper : DependencyObject
  169. {
  170. public static FlowDocument GetDocumentBind(DependencyObject obj)
  171. {
  172. return (FlowDocument)obj.GetValue(DocumentBindProperty);
  173. }
  174. public static void SetDocumentBind(DependencyObject obj, FlowDocument value)
  175. {
  176. obj.SetValue(DocumentBindProperty, value);
  177. }
  178. public static readonly DependencyProperty DocumentBindProperty =
  179. DependencyProperty.RegisterAttached("DocumentBind",
  180. typeof(BindSearchResult),
  181. typeof(SearchResultBindHelper),
  182. new FrameworkPropertyMetadata
  183. {
  184. BindsTwoWayByDefault = false,
  185. PropertyChangedCallback = (obj, e) =>
  186. {
  187. RichTextBox richTextBox = obj as RichTextBox;
  188. BindSearchResult bindItem = e.NewValue as BindSearchResult;
  189. if (richTextBox != null && bindItem != null)
  190. {
  191. richTextBox.Document = GetFlowDocument(bindItem.TextContent.Trim(), bindItem.SearchWord, bindItem.HighLightColor);
  192. }
  193. }
  194. });
  195. /// <summary>
  196. /// 获取文档流数据
  197. /// </summary>
  198. /// <param name="content">搜索文字结果</param>
  199. /// <param name="keyword">搜索关键字</param>
  200. /// <param name="textColor">高亮文字颜色</param>
  201. /// <returns>文档流数据</returns>
  202. public static FlowDocument GetFlowDocument(string content, string keyword, Color textColor)
  203. {
  204. FlowDocument Document = new FlowDocument();
  205. Paragraph textPara = new Paragraph();
  206. Document.Blocks.Add(textPara);
  207. List<int> indexList = new List<int>();
  208. content = Regex.Replace(content, "[\r\n]", " ");
  209. if (keyword.Length > 0)
  210. {
  211. for (int i = 0; i < content.Length && i >= 0;)
  212. {
  213. i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase);
  214. if (i == -1)
  215. {
  216. break;
  217. }
  218. if (indexList.Contains(i) == false)
  219. {
  220. indexList.Add(i);
  221. }
  222. i += keyword.Length;
  223. }
  224. }
  225. List<string> splitList = new List<string>();
  226. int lastIndex = -1;
  227. foreach (int index in indexList)
  228. {
  229. string prevStr = string.Empty;
  230. if (lastIndex == -1)
  231. {
  232. prevStr = content.Substring(0, index);
  233. }
  234. else
  235. {
  236. prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1);
  237. }
  238. if (prevStr != string.Empty)
  239. {
  240. splitList.Add(prevStr);
  241. }
  242. splitList.Add(content.Substring(index, keyword.Length));
  243. lastIndex = index;
  244. }
  245. if (indexList.Count > 0)
  246. {
  247. lastIndex = indexList[indexList.Count - 1];
  248. if (content.Length > lastIndex + keyword.Length)
  249. {
  250. splitList.Add(content.Substring(lastIndex + keyword.Length));
  251. }
  252. }
  253. TextBlock addBlock = new TextBlock();
  254. foreach (string textappend in splitList)
  255. {
  256. Run textRun = new Run(textappend);
  257. if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase))
  258. {
  259. textRun.Background = new SolidColorBrush(textColor);
  260. }
  261. addBlock.Inlines.Add(textRun);
  262. }
  263. addBlock.TextTrimming = TextTrimming.CharacterEllipsis;
  264. textPara.Inlines.Add(addBlock);
  265. return Document;
  266. }
  267. }
  268. }