PDFTextSearch.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using ComPDFKit.Import;
  2. using ComPDFKit.NativeMethod;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Media;
  14. using System.Windows;
  15. namespace ComPDFKit.Tool
  16. {
  17. /// <summary>
  18. /// This class is about search result object.
  19. /// </summary>
  20. public class TextSearchItem
  21. {
  22. /// <summary>
  23. /// Page index
  24. /// </summary>
  25. public int PageIndex;
  26. /// <summary>
  27. /// The bounds of the selection on the page(PDF 72DPI)
  28. /// </summary>
  29. public Rect TextRect;
  30. /// <summary>
  31. /// The text contains in the selection
  32. /// </summary>
  33. public string TextContent;
  34. /// <summary>
  35. /// Page rotation angle
  36. /// </summary>
  37. public int PageRotate;
  38. public void CreatePaintBrush(Color color)
  39. {
  40. Application.Current.Dispatcher.Invoke(() =>
  41. {
  42. this.PaintBrush = new SolidColorBrush(color);
  43. });
  44. }
  45. public SolidColorBrush PaintBrush { get; private set; } = Brushes.Transparent;
  46. public void CreateBorderBrush(Color color)
  47. {
  48. Application.Current.Dispatcher.Invoke(() =>
  49. {
  50. this.BorderBrush = new SolidColorBrush(color);
  51. });
  52. }
  53. public SolidColorBrush BorderBrush { get; private set; } = Brushes.Transparent;
  54. public int BorderThickness;
  55. public Thickness Padding;
  56. }
  57. /// <summary>
  58. /// Result of text search
  59. /// </summary>
  60. public class TextSearchResult
  61. {
  62. /// <summary>
  63. /// Start page index
  64. /// </summary>
  65. public int StartPage;
  66. /// <summary>
  67. /// End page index
  68. /// </summary>
  69. public int EndPage;
  70. /// <summary>
  71. /// Percentage of search process
  72. /// </summary>
  73. public double Percent;
  74. /// <summary>
  75. /// Current page index
  76. /// </summary>
  77. public int CurrentPage;
  78. /// <summary>
  79. /// The number of search result
  80. /// </summary>
  81. public int TotalCount;
  82. /// <summary>
  83. /// The maximum value of search result
  84. /// </summary>
  85. public int PageMaxCount;
  86. /// <summary>
  87. /// Details of current search result
  88. /// </summary>
  89. public Dictionary<int, List<TextSearchItem>> Items = new Dictionary<int, List<TextSearchItem>>();
  90. }
  91. /// <summary>
  92. /// This class is about text search.
  93. /// </summary>
  94. public class PDFTextSearch
  95. {
  96. /// <summary>
  97. /// A notification that a find operation finished working on a page of a document.
  98. /// </summary>
  99. public event EventHandler<TextSearchResult> SearchPercentHandler;
  100. /// <summary>
  101. /// A notification that a find operation cancels.
  102. /// </summary>
  103. public event EventHandler<TextSearchResult> SearchCancelHandler;
  104. /// <summary>
  105. /// A notification that a find operation finished of a document.
  106. /// </summary>
  107. public event EventHandler<TextSearchResult> SearchCompletedHandler;
  108. /// <summary>
  109. /// Associates a CPDFDocument.
  110. /// </summary>
  111. public CPDFDocument TextSearchDocument;
  112. private CPDFDocument mSearchDocument;
  113. private bool isCancel;
  114. private string searchKeywords;
  115. private string password;
  116. private C_Search_Options searchOption;
  117. private int startPage;
  118. private int endPage;
  119. /// <summary>
  120. /// Whether to allow to search.
  121. /// </summary>
  122. public bool CanDoSearch { get; private set; } = true;
  123. /// <summary>
  124. /// Constructor function.
  125. /// </summary>
  126. public PDFTextSearch()
  127. {
  128. }
  129. private void DoWork()
  130. {
  131. endPage = endPage == -1 ? TextSearchDocument.PageCount - 1 : Math.Min(TextSearchDocument.PageCount - 1, endPage);
  132. TextSearchResult searchResult = new TextSearchResult();
  133. searchResult.StartPage = startPage;
  134. searchResult.EndPage = endPage;
  135. double searchPercent = 100;
  136. try
  137. {
  138. mSearchDocument = CPDFDocument.InitWithFilePath(TextSearchDocument.FilePath);
  139. if (password != null && password != string.Empty)
  140. {
  141. mSearchDocument.UnlockWithPassword(password);
  142. }
  143. password = string.Empty;
  144. }
  145. catch (Exception ex)
  146. {
  147. }
  148. if (mSearchDocument != null)
  149. {
  150. try
  151. {
  152. int pageMaxCount = 0;
  153. int recordCount = 0;
  154. searchPercent = 0;
  155. for (int i = startPage; i <= endPage; i++)
  156. {
  157. CPDFTextSearcher mPDFTextSearcher = new CPDFTextSearcher();
  158. CPDFPage pageCore = mSearchDocument.PageAtIndex(i);
  159. CPDFTextPage textPage = pageCore.GetTextPage();
  160. int startIndex = 0;
  161. List<TextSearchItem> textSearchItems = new List<TextSearchItem>();
  162. if (mPDFTextSearcher.FindStart(textPage, searchKeywords, searchOption, startIndex))
  163. {
  164. CRect textRect = new CRect();
  165. string textContent = "";
  166. while (mPDFTextSearcher.FindNext(pageCore, textPage, ref textRect, ref textContent, ref startIndex))
  167. {
  168. if (textContent == "")
  169. {
  170. textContent = searchKeywords;
  171. }
  172. textSearchItems.Add(new TextSearchItem()
  173. {
  174. PageIndex = i,
  175. TextRect = new Rect(textRect.left, textRect.top, textRect.width(), textRect.height()),
  176. TextContent = textContent,
  177. PageRotate = pageCore.Rotation
  178. });
  179. var matchResult = Regex.Matches(textContent, searchKeywords, RegexOptions.IgnoreCase);
  180. if (matchResult != null)
  181. {
  182. recordCount += matchResult.Count;
  183. }
  184. }
  185. }
  186. mPDFTextSearcher.FindClose();
  187. if (textSearchItems.Count > 0)
  188. {
  189. searchResult.Items.Add(i, textSearchItems);
  190. }
  191. pageMaxCount = Math.Max(pageMaxCount, textSearchItems.Count);
  192. searchResult.TotalCount = recordCount;
  193. searchResult.PageMaxCount = pageMaxCount;
  194. if (SearchPercentHandler != null)
  195. {
  196. searchPercent = (int)((i + 1 - startPage) * 100 / (endPage + 1 - startPage));
  197. searchResult.Percent = searchPercent;
  198. searchResult.CurrentPage = i;
  199. SearchPercentHandler.Invoke(this, searchResult);
  200. }
  201. mSearchDocument.ReleasePages(i);
  202. if (isCancel)
  203. {
  204. break;
  205. }
  206. }
  207. searchPercent = 100;
  208. }
  209. catch (Exception ex)
  210. {
  211. }
  212. mSearchDocument.Release();
  213. }
  214. try
  215. {
  216. if (SearchCompletedHandler != null && !isCancel)
  217. {
  218. searchResult.Percent = searchPercent;
  219. SearchCompletedHandler.Invoke(this, searchResult);
  220. }
  221. if (SearchCancelHandler != null && isCancel)
  222. {
  223. SearchCancelHandler.Invoke(this, searchResult);
  224. }
  225. }
  226. catch (Exception ex)
  227. {
  228. }
  229. CanDoSearch = true;
  230. isCancel = false;
  231. }
  232. /// <summary>
  233. /// Cancles a search.
  234. /// </summary>
  235. public void CancleSearch()
  236. {
  237. isCancel = true;
  238. }
  239. /// <summary>
  240. /// Searches the specified string in the document.
  241. /// </summary>
  242. /// <param name="search">Search the specified string</param>
  243. /// <param name="option">Search options</param>
  244. /// <param name="pwd">Document password</param>
  245. /// <param name="startPage">Start page index</param>
  246. /// <param name="endPage">End page index</param>
  247. /// <returns>Returns true on success, false on failure</returns>
  248. public bool SearchText(string search, C_Search_Options option, string pwd = "", int startPage = 0, int endPage = -1)
  249. {
  250. if (CPDFSDKVerifier.TextSearch == false)
  251. {
  252. Trace.WriteLine("Your license does not support this feature, please upgrade your license privilege.");
  253. return false;
  254. }
  255. if (CanDoSearch)
  256. {
  257. searchKeywords = search;
  258. password = pwd;
  259. searchOption = option;
  260. this.startPage = startPage;
  261. this.endPage = endPage;
  262. isCancel = false;
  263. CanDoSearch = false;
  264. Thread taskThread = new Thread(DoWork);
  265. taskThread.Start();
  266. return true;
  267. }
  268. return false;
  269. }
  270. }
  271. }