TextSearchTest.vb 3.6 KB

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