FindAndHighlight.vb
'' 完毕:
Imports System
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Common

' This example shows how to find all occurrences of a word in a PDF document
' and highlight all those occurrences using the text markup highlight annotation.
'
' The PDF used in this example was downloaded from https://www.frontiersin.org/articles/10.3389/fendo.2022.1005722/full.
Public Class FindAndHighlight
    Public Function CreatePDF(stream As Stream) As Integer
        ' Load an existing PDF:
        Dim doc = New GcPdfDocument()
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "fendo-13-1005722.pdf"))
            doc.Load(fs)

            ' Find all occurrences of the word "childbirths":
            Dim found = doc.FindText(New FindTextParams("childbirths", True, False), Nothing)

            ' Add a text markup annotation to highlight each occurrence:
            For Each f In found
                Dim markup = New TextMarkupAnnotation() With {
                    .Page = doc.Pages(f.PageIndex),
                    .MarkupType = TextMarkupType.Highlight,
                    .Color = Color.Yellow
                }
                Dim area As List(Of Quadrilateral) = New List(Of Quadrilateral)
                For Each b In f.Bounds
                    area.Add(b)
                Next
                markup.Area = area
            Next

            ' Done:
            doc.Save(stream)
            Return doc.Pages.Count
        End Using
    End Function
End Class