TextSearch.cs 4.2 KB

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