TextSearchTest.vb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. Imports ComPDFKit.Import
  2. Imports ComPDFKit.PDFAnnotation
  3. Imports ComPDFKit.PDFDocument
  4. Imports ComPDFKit.PDFPage
  5. Imports System
  6. Imports System.Collections.Generic
  7. Imports System.IO
  8. Imports System.Windows
  9. Module TextSearchTest
  10. Private parentPath As String =
  11. Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())))
  12. Private outputPath As String = Path.Combine(parentPath, "Output", "VB")
  13. Sub Main(args As String())
  14. ' Preparation work
  15. Console.WriteLine("Running text search test sample..." & vbCrLf)
  16. If Not Directory.Exists(outputPath) Then
  17. Directory.CreateDirectory(outputPath)
  18. End If
  19. SDKLicenseHelper.LicenseVerify()
  20. ' Sample 1: Search text
  21. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("Text.pdf")
  22. SearchText(document)
  23. document.Release()
  24. Console.WriteLine("--------------------")
  25. Console.WriteLine("Done")
  26. Console.WriteLine("--------------------")
  27. Console.ReadLine()
  28. End Sub
  29. ' Search for keywords in the current page and record the search results.
  30. Private Sub SearchForPage(page As CPDFPage, searchKeywords As String, options As C_Search_Options, ByRef rects As List(Of Rect), ByRef strings As List(Of String))
  31. rects = New List(Of Rect)()
  32. strings = New List(Of String)()
  33. Dim findIndex As Integer = 0
  34. Dim textPage As CPDFTextPage = page.GetTextPage()
  35. Dim searcher As New CPDFTextSearcher()
  36. If searcher.FindStart(textPage, searchKeywords, options, 0) Then
  37. Dim textRect As New CRect()
  38. Dim textContent As String = ""
  39. While searcher.FindNext(page, textPage, textRect, textContent, findIndex)
  40. strings.Add(textContent)
  41. rects.Add(New Rect(textRect.left, textRect.top, textRect.width(), textRect.height()))
  42. End While
  43. End If
  44. End Sub
  45. ' Highlight the first result
  46. Private Function HighlightTheFirstResult(page As CPDFPage, rect As Rect) As Boolean
  47. Dim cRectList As New List(Of CRect)()
  48. cRectList.Add(New CRect(CSng(rect.Left), CSng(rect.Top), CSng(rect.Right), CSng(rect.Bottom)))
  49. Dim annotation As CPDFHighlightAnnotation = CType(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT), CPDFHighlightAnnotation)
  50. Dim color As Byte() = {0, 255, 0}
  51. annotation.SetColor(color)
  52. annotation.SetTransparency(120)
  53. annotation.SetQuardRects(cRectList)
  54. annotation.UpdateAp()
  55. Return True
  56. End Function
  57. ' Search PDF keywords on the first page of the article,
  58. ' after the search is completed,
  59. ' highlight the first searched keyword and save it
  60. Private Function SearchText(document As CPDFDocument) As Boolean
  61. Dim page As CPDFPage = document.PageAtIndex(0)
  62. 'rects: The collection of locales where keywords are located.
  63. Dim rects As New List(Of Rect)()
  64. 'strings: The full text of the keyword's area
  65. Dim strings As New List(Of String)()
  66. 'Search for single page
  67. SearchForPage(page, "PDF", C_Search_Options.Search_Case_Insensitive, rects, strings)
  68. Console.WriteLine("The pdf have {0} results", rects.Count)
  69. Console.WriteLine("Search finished, now highlight the first result. ")
  70. 'Highlight the first result
  71. HighlightTheFirstResult(page, rects(0))
  72. Dim filePath As String = Path.Combine(outputPath, "HighlightFirstTest.pdf")
  73. If Not document.WriteToFilePath(filePath) Then
  74. Return False
  75. End If
  76. Console.WriteLine("Browse the changed file in " & filePath)
  77. Return True
  78. End Function
  79. End Module