TextSearchTest.cs 4.3 KB

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