CPDFSearchResultUI.xaml.cs 8.8 KB

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