CPDFSearchResultUI.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. public static int MapIndexToContent(string a, string b, int indexInA)
  187. {
  188. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b) || indexInA < 0 || indexInA >= a.Length)
  189. {
  190. return -1;
  191. }
  192. int indexInB = 0;
  193. int aIndexCounter = 0;
  194. // Iterate over b and match characters to those in a
  195. for (int i = 0; i < b.Length; i++)
  196. {
  197. if (b[i] != ' ')
  198. {
  199. if (aIndexCounter == indexInA)
  200. {
  201. indexInB = i;
  202. break;
  203. }
  204. aIndexCounter++;
  205. }
  206. }
  207. return indexInB;
  208. }
  209. public static string RestoreStringWithSpaces(string a, string b, int startIndexInB)
  210. {
  211. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b) || startIndexInB < 0 || startIndexInB >= b.Length)
  212. {
  213. return string.Empty;
  214. }
  215. a = Regex.Replace(a, @"[\r\n\s]", "");
  216. int aIndex = 0;
  217. string result = string.Empty;
  218. for (int i = startIndexInB; i < b.Length && aIndex < a.Length; i++)
  219. {
  220. if (b[i] == ' ')
  221. {
  222. result += ' ';
  223. }
  224. else
  225. {
  226. result += a[aIndex];
  227. aIndex++;
  228. }
  229. }
  230. return result;
  231. }
  232. /// <summary>
  233. /// Get document stream data
  234. /// </summary>
  235. /// <param name="content">Search text results</param>
  236. /// <param name="keyword">Search for keywords</param>
  237. /// <param name="textColor">Highlight text color</param>
  238. /// <returns>Document flow data</returns>
  239. public static FlowDocument GetFlowDocument(string content, string keyword, Color textColor)
  240. {
  241. FlowDocument Document = new FlowDocument();
  242. Paragraph textPara = new Paragraph();
  243. Document.Blocks.Add(textPara);
  244. List<int> indexList = new List<int>();
  245. content = Regex.Replace(content, @"[\r\n]", " ");
  246. int originalIndex = -1;
  247. string contentForMatch = Regex.Replace(content, @"[\r\n\s]", "");
  248. string keywordForMatch = Regex.Replace(keyword, @"\s", "");
  249. if (keywordForMatch.Length > 0)
  250. {
  251. for (int i = 0, offset = 0; i < contentForMatch.Length && i >= 0;)
  252. {
  253. i = contentForMatch.IndexOf(keywordForMatch, i, StringComparison.OrdinalIgnoreCase);
  254. if (i == -1)
  255. {
  256. break;
  257. }
  258. originalIndex = content.IndexOf(keyword, offset, StringComparison.OrdinalIgnoreCase);
  259. if (originalIndex != -1 && !indexList.Contains(originalIndex))
  260. {
  261. indexList.Add(originalIndex);
  262. offset = originalIndex + keyword.Length;
  263. i += keywordForMatch.Length;
  264. }
  265. else if(originalIndex == -1)
  266. {
  267. originalIndex = MapIndexToContent(contentForMatch, content, i);
  268. indexList.Add(originalIndex);
  269. offset = originalIndex + keyword.Length;
  270. i += keywordForMatch.Length;
  271. }
  272. }
  273. }
  274. if(originalIndex != -1)
  275. {
  276. keyword = RestoreStringWithSpaces(keyword, content, originalIndex);
  277. }
  278. List<string> splitList = new List<string>();
  279. int lastIndex = -1;
  280. foreach (int index in indexList)
  281. {
  282. string prevStr = lastIndex == -1 ? content.Substring(0, index) : content.Substring(lastIndex + keyword.Length, index - lastIndex - keyword.Length);
  283. if (prevStr != string.Empty)
  284. {
  285. splitList.Add(prevStr);
  286. }
  287. splitList.Add(content.Substring(index, keyword.Length));
  288. lastIndex = index;
  289. }
  290. if (indexList.Count > 0)
  291. {
  292. lastIndex = indexList[indexList.Count - 1];
  293. if (content.Length > lastIndex + keyword.Length)
  294. {
  295. splitList.Add(content.Substring(lastIndex + keyword.Length));
  296. }
  297. }
  298. else
  299. {
  300. splitList.Add(content);
  301. }
  302. TextBlock addBlock = new TextBlock();
  303. foreach (string textappend in splitList)
  304. {
  305. Run textRun = new Run(textappend);
  306. if (textappend.Equals(keyword, StringComparison.OrdinalIgnoreCase))
  307. {
  308. textRun.Background = new SolidColorBrush(textColor);
  309. }
  310. addBlock.Inlines.Add(textRun);
  311. }
  312. addBlock.TextTrimming = TextTrimming.CharacterEllipsis;
  313. textPara.Inlines.Add(addBlock);
  314. return Document;
  315. }
  316. }
  317. }