CPDFSearchControl.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKit.Tool;
  3. using Compdfkit_Tools.PDFControlUI;
  4. using ComPDFKitViewer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. namespace Compdfkit_Tools.PDFControl
  11. {
  12. public partial class CPDFSearchControl : UserControl
  13. {
  14. /// <summary>
  15. /// PDFViewer
  16. /// </summary>
  17. private CPDFViewer pdfView;
  18. private int currentHighLightIndex { get; set; } = -1;
  19. private PDFTextSearch textSearch;
  20. private string keyWord;
  21. private SolidColorBrush highLightBrush = new SolidColorBrush(Color.FromArgb(0x99, 0xFF, 0xF7, 0x00));
  22. private int ResultCount = 0;
  23. private PDFViewControl ViewControl;
  24. public CPDFSearchControl()
  25. {
  26. InitializeComponent();
  27. Loaded += PDFSearch_Loaded;
  28. }
  29. public void InitWithPDFViewer(PDFViewControl newViewControl)
  30. {
  31. ViewControl = newViewControl;
  32. if (ViewControl != null)
  33. {
  34. CPDFViewerTool viewTool = ViewControl.PDFViewTool;
  35. if (viewTool != null)
  36. {
  37. CPDFViewer newPDFView = viewTool.GetCPDFViewer();
  38. if (pdfView != newPDFView)
  39. {
  40. ClearSearchResult();
  41. pdfView = newPDFView;
  42. }
  43. }
  44. }
  45. else
  46. {
  47. ClearSearchResult();
  48. }
  49. }
  50. private void PDFSearch_Loaded(object sender, RoutedEventArgs e)
  51. {
  52. textSearch = new PDFTextSearch();
  53. SearchInput.SearchEvent += SearchInput_SearchEvent;
  54. SearchInput.ClearEvent += SearchInput_ClearEvent;
  55. textSearch.SearchCompletedHandler += TextSearch_SearchCompletedHandler;
  56. SearchResult.SelectionChanged += SearchResult_SelectionChanged;
  57. }
  58. private void SearchInput_ClearEvent(object sender, EventArgs e)
  59. {
  60. ClearSearchResult();
  61. }
  62. private void SearchInput_FindPreviousEvent(object sender, EventArgs e)
  63. {
  64. if (currentHighLightIndex > 0)
  65. {
  66. currentHighLightIndex--;
  67. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  68. HighLightSelectResult(result);
  69. }
  70. }
  71. private void SearchInput_FindNextEvent(object sender, EventArgs e)
  72. {
  73. currentHighLightIndex++;
  74. if (currentHighLightIndex >= 0)
  75. {
  76. BindSearchResult result = SearchResult.GetItem(currentHighLightIndex);
  77. HighLightSelectResult(result);
  78. }
  79. }
  80. private void SearchResult_SelectionChanged(object sender, int e)
  81. {
  82. currentHighLightIndex = e;
  83. BindSearchResult result = SearchResult.GetSelectItem();
  84. HighLightSelectResult(result);
  85. ResultText.Text = string.Format("Results:{0}/{1}", e+1,ResultCount);
  86. }
  87. private void TextSearch_SearchCompletedHandler(object sender, TextSearchResult e)
  88. {
  89. Dispatcher.Invoke(() =>
  90. {
  91. List<BindSearchResult> resultList = new List<BindSearchResult>();
  92. List<TextSearchItem> totalItems = new List<TextSearchItem>();
  93. foreach (int pageIndex in e.Items.Keys)
  94. {
  95. List<TextSearchItem> textSearchItems = e.Items[pageIndex];
  96. foreach (TextSearchItem item in textSearchItems)
  97. {
  98. resultList.Add(new BindSearchResult()
  99. {
  100. PageIndex = item.PageIndex,
  101. TextContent = item.TextContent,
  102. TextRect = item.TextRect,
  103. SearchWord = keyWord,
  104. HighLightColor = Color.FromArgb(0x99, 0xFF, 0xF7, 0x00),
  105. PageRotate = item.PageRotate,
  106. RefData=item
  107. });
  108. totalItems.Add(item);
  109. }
  110. }
  111. SearchResult.SetSearchResult(resultList);
  112. ResultCount=resultList.Count;
  113. ViewControl.PDFViewTool?.SetPageSelectText(totalItems);
  114. if (resultList==null || resultList.Count==0)
  115. {
  116. ResultText.Text= string.Empty;
  117. }
  118. else
  119. {
  120. ResultText.Text = string.Format("{0} results found", ResultCount);
  121. }
  122. });
  123. }
  124. private void SearchInput_SearchEvent(object sender, string e)
  125. {
  126. if (string.IsNullOrEmpty(e))
  127. {
  128. return;
  129. }
  130. if (pdfView == null || pdfView.GetDocument() == null)
  131. {
  132. return;
  133. }
  134. if (textSearch != null && textSearch.CanDoSearch)
  135. {
  136. keyWord = e;
  137. textSearch.TextSearchDocument = pdfView.GetDocument();
  138. textSearch.SearchText(e, C_Search_Options.Search_Case_Insensitive);
  139. }
  140. }
  141. private void HighLightSelectResult(BindSearchResult result)
  142. {
  143. if (result == null || result.RefData==null)
  144. {
  145. return;
  146. }
  147. List<TextSearchItem> selectList = new List<TextSearchItem>();
  148. TextSearchItem highlightItem=result.RefData as TextSearchItem;
  149. if(highlightItem != null)
  150. {
  151. highlightItem.CreatePaintBrush(highLightBrush.Color);
  152. selectList.Add(highlightItem);
  153. ViewControl.PDFViewTool?.HighLightSearchText(selectList);
  154. }
  155. }
  156. private void ClearSearchResult()
  157. {
  158. SearchResult?.SetSearchResult(null);
  159. ResultText.Text = string.Empty;
  160. SearchInput.SearchKeyWord=string.Empty;
  161. }
  162. }
  163. }