CPDFSearchControl.xaml.cs 14 KB

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