CPDFSearchControl.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using ComPDFKit.PDFPage;
  2. using Compdfkit_Tools.PDFControlUI;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. using Compdfkit_Tools.Helper;
  13. namespace Compdfkit_Tools.PDFControl
  14. {
  15. public partial class CPDFSearchControl : UserControl
  16. {
  17. /// <summary>
  18. /// PDFViewer
  19. /// </summary>
  20. private CPDFViewer pdfView;
  21. private int currentHighLightIndex { get; set; } = -1;
  22. private C_Search_Options searchOption = C_Search_Options.Search_Case_Insensitive;
  23. private PDFTextSearch textSearch;
  24. private string keyWord;
  25. private bool isClearResult = false;
  26. private SolidColorBrush highLightBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
  27. private bool _isCaseSensitive = true;
  28. public bool IsCaseSensitive
  29. {
  30. set
  31. {
  32. _isCaseSensitive = value;
  33. if (_isCaseSensitive)
  34. {
  35. searchOption &= ~C_Search_Options.Search_Case_Sensitive;
  36. }
  37. else
  38. {
  39. searchOption |= C_Search_Options.Search_Case_Sensitive;
  40. }
  41. pdfView?.StartFindText(keyWord, searchOption);
  42. }
  43. get
  44. {
  45. return _isCaseSensitive;
  46. }
  47. }
  48. public bool IsMatchWholeWord
  49. {
  50. set
  51. {
  52. if (value)
  53. {
  54. searchOption |= C_Search_Options.Search_Match_Whole_Word;
  55. }
  56. else
  57. {
  58. searchOption &= ~C_Search_Options.Search_Match_Whole_Word;
  59. }
  60. pdfView?.StartFindText(keyWord, searchOption);
  61. }
  62. }
  63. public CPDFSearchControl()
  64. {
  65. InitializeComponent();
  66. Loaded += PDFSearch_Loaded;
  67. DataContext = this;
  68. }
  69. public void InitWithPDFViewer(CPDFViewer newPDFView)
  70. {
  71. if (pdfView != newPDFView)
  72. {
  73. ClearSearchResult();
  74. pdfView = newPDFView;
  75. }
  76. }
  77. private void PDFSearch_Loaded(object sender, RoutedEventArgs e)
  78. {
  79. textSearch = new PDFTextSearch();
  80. SearchInput.SearchEvent -= SearchInput_SearchEvent;
  81. SearchInput.ClearEvent -= SearchInput_ClearEvent;
  82. textSearch.SearchCompletedHandler -= TextSearch_SearchCompletedHandler;
  83. SearchResult.SelectionChanged -= SearchResult_SelectionChanged;
  84. textSearch.SearchPercentHandler -= TextSearch_SearchPercentHandler;
  85. textSearch.SearchCancelHandler -= TextSearch_SearchCancelHandler;
  86. SearchInput.SearchEvent += SearchInput_SearchEvent;
  87. SearchInput.ClearEvent += SearchInput_ClearEvent;
  88. textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
  89. SearchResult.SelectionChanged += SearchResult_SelectionChanged;
  90. textSearch.SearchPercentHandler += TextSearch_SearchPercentHandler;
  91. textSearch.SearchCancelHandler += TextSearch_SearchCancelHandler;
  92. SearchInput.MoveResultEvent -= SearchInput_MoveResultEvent;
  93. SearchInput.MoveResultEvent += SearchInput_MoveResultEvent;
  94. SearchTog.IsChecked = true;
  95. }
  96. private void SearchInput_MoveResultEvent(object sender, CPDFSearchInputUI.MoveDirection e)
  97. {
  98. if (keyWord != SearchInput.SearchKeyWord)
  99. {
  100. keyWord = SearchInput.SearchKeyWord;
  101. pdfView.StartFindText(keyWord, searchOption);
  102. SearchResult.ClearSearchResult();
  103. }
  104. if (e == CPDFSearchInputUI.MoveDirection.Previous)
  105. {
  106. pdfView.FindPrevious();
  107. }
  108. else
  109. {
  110. pdfView.FindNext();
  111. }
  112. }
  113. private void TextSearch_SearchPercentHandler(object sender, TextSearchResult e)
  114. {
  115. Dispatcher.Invoke(() =>
  116. {
  117. if (!e.Items.ContainsKey(e.CurrentPage))
  118. {
  119. return;
  120. }
  121. ProgressBar.ProgressValue = e.CurrentPage + 1;
  122. foreach (var item in e.Items[e.CurrentPage])
  123. {
  124. if (item == null)
  125. {
  126. continue;
  127. }
  128. var result = new BindSearchResult()
  129. {
  130. PageIndex = item.PageIndex,
  131. TextContent = item.TextContent,
  132. TextRect = item.TextRect,
  133. SearchWord = keyWord,
  134. HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
  135. PageRotate = item.PageRotate
  136. };
  137. SearchResult.AddSearchResult(result);
  138. ResultNum.Text = SearchResult.ResultListControl.Items.Count.ToString();
  139. }
  140. });
  141. }
  142. private void SearchInput_ClearEvent(object sender, EventArgs e)
  143. {
  144. if (textSearch.CanDoSearch)
  145. {
  146. Dispatcher.Invoke(ClearSearchResult);
  147. }
  148. else
  149. {
  150. isClearResult = true;
  151. textSearch.CancleSearch();
  152. }
  153. ProgressBorder.Visibility = Visibility.Hidden;
  154. }
  155. private void TextSearch_SearchCancelHandler(object sender, TextSearchResult e)
  156. {
  157. if (isClearResult)
  158. {
  159. Dispatcher.Invoke(ClearSearchResult);
  160. isClearResult = false;
  161. }
  162. }
  163. private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
  164. {
  165. if (currentHighLightIndex > 0)
  166. {
  167. currentHighLightIndex--;
  168. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  169. SearchResult.ClearSearchResult();
  170. }
  171. }
  172. private void SearchInput_FindNextEvent(object sender, EventArgs e)
  173. {
  174. currentHighLightIndex++;
  175. if (currentHighLightIndex >= 0)
  176. {
  177. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  178. SearchResult.ClearSearchResult();
  179. }
  180. }
  181. private void SearchResult_SelectionChanged(object sender, int e)
  182. {
  183. if (e < 0)
  184. {
  185. return;
  186. }
  187. currentHighLightIndex = e;
  188. BindSearchResult result = SearchResult.GetSelectItem();
  189. HighLightSelectResult(result);
  190. ResultNum.Text = "";
  191. ResultText.Text = LanguageHelper.BotaManager.GetString("Text_Result") + (e + 1) + "/" + SearchResult.ResultListControl.Items.Count;
  192. }
  193. private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
  194. {
  195. Dispatcher.Invoke(() =>
  196. {
  197. ProgressBorder.Visibility = Visibility.Hidden;
  198. });
  199. }
  200. private void SearchInput_SearchEvent(object sender, string e)
  201. {
  202. if (string.IsNullOrEmpty(e))
  203. {
  204. return;
  205. }
  206. if (pdfView == null || pdfView.Document == null)
  207. {
  208. return;
  209. }
  210. if (textSearch == null || !textSearch.CanDoSearch)
  211. {
  212. return;
  213. }
  214. if (SearchTog.IsChecked == true)
  215. {
  216. if (pdfView.UndoManager.CanSave)
  217. {
  218. pdfView.Document.WriteToLoadedPath();
  219. }
  220. keyWord = e;
  221. textSearch.TextSearchDocument = pdfView.Document;
  222. SearchResult.ClearSearchResult();
  223. ResultNum.Text = "0";
  224. ResultText.Text = LanguageHelper.BotaManager.GetString("Tip_Result");
  225. ProgressBar.ProgressMaxValue = pdfView.Document.PageCount;
  226. ProgressBorder.Visibility = Visibility.Visible;
  227. pdfView.ClearSelectedText();
  228. textSearch.SearchText(keyWord, searchOption);
  229. }
  230. else if (ReplaceTog.IsChecked == true)
  231. {
  232. SearchInput_MoveResultEvent(null, CPDFSearchInputUI.MoveDirection.Next);
  233. }
  234. }
  235. private void HighLightSelectResult(BindSearchResult result)
  236. {
  237. if (result == null)
  238. {
  239. return;
  240. }
  241. List<TextSearchItem> selectList = new List<TextSearchItem>
  242. {
  243. new TextSearchItem()
  244. {
  245. PageIndex = result.PageIndex,
  246. TextRect = result.TextRect,
  247. TextContent = result.TextContent,
  248. PageRotate = result.PageRotate,
  249. }
  250. };
  251. pdfView.SetPageSelectText(selectList, highLightBrush);
  252. }
  253. private void ClearSearchResult()
  254. {
  255. SearchResult?.ClearSearchResult();
  256. ResultNum.Text = string.Empty;
  257. ResultText.Text = string.Empty;
  258. SearchInput.SearchKeyWord = string.Empty;
  259. }
  260. private void SearchCancel_Click(object sender, RoutedEventArgs e)
  261. {
  262. textSearch.CancleSearch();
  263. }
  264. private void SearchTog_Checked(object sender, RoutedEventArgs e)
  265. {
  266. if (pdfView.UndoManager.CanSave)
  267. {
  268. pdfView.Document.WriteToLoadedPath();
  269. pdfView.UndoManager.CanSave = false;
  270. }
  271. ReplaceTog.IsChecked = false;
  272. SearchInput.InputGrid.RowDefinitions[1].Height = new GridLength(0);
  273. SearchInput.Height = 40;
  274. ReplaceBorder.Visibility = Visibility.Collapsed;
  275. SearchResult.Visibility = Visibility.Visible;
  276. ResultBorder.Visibility = Visibility.Visible;
  277. }
  278. private void ReplaceTog_Checked(object sender, RoutedEventArgs e)
  279. {
  280. if (pdfView.UndoManager.CanSave)
  281. {
  282. pdfView.Document.WriteToLoadedPath();
  283. pdfView.UndoManager.CanSave = false;
  284. }
  285. SearchTog.IsChecked = false;
  286. SearchInput.InputGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
  287. SearchInput.Height = 80;
  288. ReplaceBorder.Visibility = Visibility.Visible;
  289. SearchResult.Visibility = Visibility.Collapsed;
  290. ResultBorder.Visibility = Visibility.Collapsed;
  291. keyWord = string.Empty;
  292. }
  293. private void ReplaceCurrent_Click(object sender, RoutedEventArgs e)
  294. {
  295. pdfView.ReplaceText(SearchInput.ReplaceWord);
  296. }
  297. private void ReplaceAll_Click(object sender, RoutedEventArgs e)
  298. {
  299. keyWord = SearchInput.SearchKeyWord;
  300. pdfView.StartFindText(keyWord, searchOption);
  301. pdfView.ReplaceAllText(SearchInput.ReplaceWord);
  302. }
  303. }
  304. }