FindAndHighlight.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf.Annotations

Namespace GcPdfWeb.Samples
    ' 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
                }
                    For Each b In f.Bounds
                        markup.Area.Add(b)
                    Next
                Next

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