TextSearch.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. namespace TextSearchTest
  13. {
  14. internal class TextSearch
  15. {
  16. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\TextSearch";
  17. static void Main(string[] args)
  18. {
  19. #region Perparation work
  20. Console.WriteLine("Running text search test sample…\r\n");
  21. if (!Directory.Exists(outputPath))
  22. {
  23. Directory.CreateDirectory(outputPath);
  24. }
  25. SDKLicenseHelper.LicenseVerify();
  26. #endregion
  27. #region Sample 1: Search text
  28. CPDFDocument document = CPDFDocument.InitWithFilePath("Text.pdf");
  29. SearchText(document);
  30. document.Release();
  31. Console.WriteLine("--------------------");
  32. #endregion
  33. Console.WriteLine("Done");
  34. Console.WriteLine("--------------------");
  35. Console.ReadLine();
  36. }
  37. /// <summary>
  38. /// Search for keywords in the current page and record the search results.
  39. /// </summary>
  40. static private void SearchForPage(CPDFPage page, string searchKeywords, C_Search_Options option, ref List<Rect> rects, ref List<string> strings)
  41. {
  42. rects = new List<Rect>();
  43. strings = new List<string>();
  44. int findIndex = 0;
  45. CPDFTextPage textPage = page.GetTextPage();
  46. CPDFTextSearcher searcher = new CPDFTextSearcher();
  47. if (searcher.FindStart(textPage, searchKeywords, option, 0))
  48. {
  49. CRect textRect = new CRect();
  50. string textContent = "";
  51. while (searcher.FindNext(page, textPage, ref textRect, ref textContent, ref findIndex))
  52. {
  53. strings.Add(textContent);
  54. rects.Add(new Rect(textRect.left, textRect.top, textRect.width(), textRect.height()));
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// Highlight the first result
  60. /// </summary>
  61. static private bool HighlightTheFirstResult(CPDFPage page, Rect rect)
  62. {
  63. List<CRect> cRectList = new List<CRect>();
  64. cRectList.Add(new CRect((float)rect.Left, (float)rect.Top, (float)rect.Right, (float)rect.Bottom));
  65. CPDFHighlightAnnotation annotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  66. byte[] color = { 0, 255, 0 };
  67. annotation.SetColor(color);
  68. annotation.SetTransparency(120);
  69. annotation.SetQuardRects(cRectList);
  70. annotation.UpdateAp();
  71. return true;
  72. }
  73. /// <summary>
  74. /// Search PDF keywords on the first page of the article,
  75. /// after the search is completed,
  76. /// highlight the first searched keyword and save it
  77. /// </summary>
  78. /// <param name="document">PDF with text</param>
  79. static private bool SearchText(CPDFDocument document)
  80. {
  81. CPDFPage page = document.PageAtIndex(0);
  82. //rects: The collection of locales where keywords are located.
  83. List<Rect> rects = new List<Rect>();
  84. //strings: The full text of the keyword's area
  85. List<string> strings = new List<string>();
  86. //Search for single page
  87. SearchForPage(page, "PDF", C_Search_Options.Search_Case_Insensitive, ref rects, ref strings);
  88. Console.WriteLine("The pdf have {0} results", rects.Count);
  89. Console.WriteLine("Search finished, now highlight the first result. ");
  90. //Highlight the first result
  91. HighlightTheFirstResult(page, rects[0]);
  92. string path = outputPath + "\\HighlightFirstTest.pdf";
  93. if (!document.WriteToFilePath(path))
  94. {
  95. return false;
  96. }
  97. Console.WriteLine("Browse the changed file in " + path);
  98. return true;
  99. }
  100. }
  101. }