CPDFSearchResultUI.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.Controls.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 void ClearSearchResult()
  49. {
  50. searchResults.Clear();
  51. ResultListControl.ItemsSource = null;
  52. ResultListControl.Visibility = Visibility.Collapsed;
  53. NoResultText.Visibility = Visibility.Visible;
  54. }
  55. public void AddSearchResult(BindSearchResult result)
  56. {
  57. if (result == null)
  58. {
  59. return;
  60. }
  61. searchResults.Add(new TextBindData()
  62. {
  63. BindProperty = result
  64. });
  65. ResultListControl.ItemsSource = searchResults;
  66. ResultListControl.Visibility = Visibility.Visible;
  67. NoResultText.Visibility = Visibility.Collapsed;
  68. }
  69. public BindSearchResult GetSelectItem()
  70. {
  71. TextBindData bindData = ResultListControl.SelectedItem as TextBindData;
  72. if (bindData != null)
  73. {
  74. return bindData.BindProperty;
  75. }
  76. return null;
  77. }
  78. public BindSearchResult GetItem(int index)
  79. {
  80. if (index < 0)
  81. {
  82. return null;
  83. }
  84. if (ResultListControl.HasItems && ResultListControl.Items.Count > index)
  85. {
  86. TextBindData bindData = ResultListControl.Items[index] as TextBindData;
  87. if (bindData != null)
  88. {
  89. return bindData.BindProperty;
  90. }
  91. }
  92. return null;
  93. }
  94. /// <summary>
  95. /// Clear selected results.
  96. /// </summary>
  97. public void ClearSelection()
  98. {
  99. int oldSelectionIndex = ResultListControl.SelectedIndex;
  100. ResultListControl.UnselectAll();
  101. if (oldSelectionIndex != ResultListControl.SelectedIndex)
  102. {
  103. InvokeSelectionChangedEvent(ResultListControl.SelectedIndex);
  104. }
  105. }
  106. /// <summary>
  107. /// Set selected results.
  108. /// </summary>
  109. /// <param name="selectIndex">The selected index.</param>
  110. public void SelectItem(int selectIndex)
  111. {
  112. if (ResultListControl.SelectedIndex != selectIndex)
  113. {
  114. ResultListControl.SelectedIndex = selectIndex;
  115. }
  116. }
  117. internal void InvokeSelectionChangedEvent(int selectionIndex)
  118. {
  119. SelectionChanged?.Invoke(this, selectionIndex);
  120. }
  121. }
  122. public class BindSearchResult
  123. {
  124. /// <summary>
  125. /// Page index.
  126. /// </summary>
  127. public int PageIndex { get; set; }
  128. /// <summary>
  129. /// The search text results.
  130. /// </summary>
  131. public string TextContent { get; set; }
  132. /// <summary>
  133. /// Search highlight color.
  134. /// </summary>
  135. public Color HighLightColor { get; set; }
  136. /// <summary>
  137. /// Search keywords.
  138. /// </summary>
  139. public string SearchWord { get; set; }
  140. /// <summary>
  141. /// Search result range area.
  142. /// </summary>
  143. public Rect TextRect { get; set; }
  144. /// <summary>
  145. /// The page rotation angle.
  146. /// </summary>
  147. public int PageRotate { get; set; }
  148. public object RefData { get; set; }
  149. }
  150. internal class TextBindData
  151. {
  152. public int ShowPageIndex { get { return BindProperty.PageIndex + 1; } set { BindProperty.PageIndex = value; } }
  153. public BindSearchResult BindProperty { get; set; }
  154. public TextBindData()
  155. {
  156. BindProperty = new BindSearchResult();
  157. }
  158. }
  159. internal class SearchResultBindHelper : DependencyObject
  160. {
  161. public static FlowDocument GetDocumentBind(DependencyObject obj)
  162. {
  163. return (FlowDocument)obj.GetValue(DocumentBindProperty);
  164. }
  165. public static void SetDocumentBind(DependencyObject obj, FlowDocument value)
  166. {
  167. obj.SetValue(DocumentBindProperty, value);
  168. }
  169. public static readonly DependencyProperty DocumentBindProperty =
  170. DependencyProperty.RegisterAttached("DocumentBind",
  171. typeof(BindSearchResult),
  172. typeof(SearchResultBindHelper),
  173. new FrameworkPropertyMetadata
  174. {
  175. BindsTwoWayByDefault = false,
  176. PropertyChangedCallback = (obj, e) =>
  177. {
  178. RichTextBox richTextBox = obj as RichTextBox;
  179. BindSearchResult bindItem = e.NewValue as BindSearchResult;
  180. if (richTextBox != null && bindItem != null)
  181. {
  182. richTextBox.Document = GetFlowDocument(bindItem.TextContent.Trim(), bindItem.SearchWord, bindItem.HighLightColor);
  183. }
  184. }
  185. });
  186. /// <summary>
  187. /// Get document stream data
  188. /// </summary>
  189. /// <param name="content">Search text results</param>
  190. /// <param name="keyword">Search for keywords</param>
  191. /// <param name="textColor">Highlight text color</param>
  192. /// <returns>Document flow data</returns>
  193. public static FlowDocument GetFlowDocument(string content, string keyword, Color textColor)
  194. {
  195. FlowDocument Document = new FlowDocument();
  196. Paragraph textPara = new Paragraph();
  197. Document.Blocks.Add(textPara);
  198. List<int> indexList = new List<int>();
  199. content = Regex.Replace(content, "[\r\n]", " ");
  200. if (keyword.Length > 0)
  201. {
  202. for (int i = 0; i < content.Length && i >= 0;)
  203. {
  204. i = content.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase);
  205. if (i == -1)
  206. {
  207. break;
  208. }
  209. if (indexList.Contains(i) == false)
  210. {
  211. indexList.Add(i);
  212. }
  213. i += keyword.Length;
  214. }
  215. }
  216. List<string> splitList = new List<string>();
  217. int lastIndex = -1;
  218. foreach (int index in indexList)
  219. {
  220. string prevStr = string.Empty;
  221. if (lastIndex == -1)
  222. {
  223. prevStr = content.Substring(0, index);
  224. }
  225. else
  226. {
  227. prevStr = content.Substring(lastIndex + keyword.Length, index - lastIndex - 1);
  228. }
  229. if (prevStr != string.Empty)
  230. {
  231. splitList.Add(prevStr);
  232. }
  233. splitList.Add(content.Substring(index, keyword.Length));
  234. lastIndex = index;
  235. }
  236. if (indexList.Count > 0)
  237. {
  238. lastIndex = indexList[indexList.Count - 1];
  239. if (content.Length > lastIndex + keyword.Length)
  240. {
  241. splitList.Add(content.Substring(lastIndex + keyword.Length));
  242. }
  243. }
  244. TextBlock addBlock = new TextBlock();
  245. foreach (string textappend in splitList)
  246. {
  247. Run textRun = new Run(textappend);
  248. if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase))
  249. {
  250. textRun.Background = new SolidColorBrush(textColor);
  251. }
  252. addBlock.Inlines.Add(textRun);
  253. }
  254. addBlock.TextTrimming = TextTrimming.CharacterEllipsis;
  255. textPara.Inlines.Add(addBlock);
  256. return Document;
  257. }
  258. }
  259. }