TextSearch.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Console.WriteLine("Running text search test sample…\r\n");
  20. if (!Directory.Exists(outputPath))
  21. {
  22. Directory.CreateDirectory(outputPath);
  23. }
  24. SDKLicenseHelper.LicenseVerify();
  25. CPDFDocument document = CPDFDocument.InitWithFilePath("Text.pdf");
  26. SearchText(document);
  27. Console.WriteLine("--------------------");
  28. Console.WriteLine("Done");
  29. Console.WriteLine("--------------------");
  30. Console.ReadLine();
  31. }
  32. static void SearchForPage(CPDFPage page, string searchKeywords, C_Search_Options option, ref List<Rect> rects, ref List<string> strings)
  33. {
  34. rects = new List<Rect>();
  35. strings = new List<string>();
  36. int findIndex = 0;
  37. CPDFTextPage textPage = page.GetTextPage();
  38. CPDFTextSearcher searcher = new CPDFTextSearcher();
  39. if (searcher.FindStart(textPage, searchKeywords, option, 0))
  40. {
  41. CRect textRect = new CRect();
  42. string textContent = "";
  43. while (searcher.FindNext(page, textPage, ref textRect, ref textContent, ref findIndex))
  44. {
  45. strings.Add(textContent);
  46. rects.Add(new Rect(textRect.left, textRect.top, textRect.width(), textRect.height()));
  47. }
  48. }
  49. }
  50. static private bool HighlightTheFirstResult(CPDFPage page, Rect rect)
  51. {
  52. List<CRect> cRectList = new List<CRect>();
  53. cRectList.Add(new CRect((float)rect.Left, (float)rect.Top, (float)rect.Right, (float)rect.Bottom));
  54. CPDFHighlightAnnotation annotation = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  55. byte[] color = { 0, 255, 0 };
  56. annotation.SetColor(color);
  57. annotation.SetTransparency(120);
  58. annotation.SetQuardRects(cRectList);
  59. annotation.UpdateAp();
  60. return true;
  61. }
  62. static private bool SearchText(CPDFDocument document)
  63. {
  64. CPDFPage page = document.PageAtIndex(0);
  65. List<Rect> rects = new List<Rect>();
  66. List<string> strings = new List<string>();
  67. SearchForPage(page, "PDF", C_Search_Options.Search_Case_Insensitive, ref rects, ref strings);
  68. Console.WriteLine("the key PDF have {0} results", rects.Count);
  69. Console.WriteLine("Search finished, now highlight the first result. ");
  70. HighlightTheFirstResult(page, rects[0]);
  71. string path = outputPath + "\\HighlightFirstTest.pdf";
  72. if (!document.WriteToFilePath(path))
  73. {
  74. return false;
  75. }
  76. Console.WriteLine("Browse the changed file in " + path);
  77. return true;
  78. }
  79. }
  80. }