FindText.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Common
Imports GrapeCity.Documents.Drawing

'' This sample loads the PDF file created by the BalancedColumns sample,
'' finds all occurrences of the words 'lorem' and 'ipsum' in the loaded document,
'' and highlights these two words using different colors.
Public Class FindText
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()

        '' The original file stream must be kept open while working with the loaded PDF, see LoadPDF for details:
        Using fs = New FileStream(Path.Combine("Resources", "PDFs", "BalancedColumns.pdf"), FileMode.Open, FileAccess.Read)
            doc.Load(fs)
            '' Find all 'lorem', using case-insensitive word search:
            Dim findsLorem = doc.FindText(
                New FindTextParams("lorem", True, False),
                OutputRange.All)
            '' Ditto for 'ipsum':
            Dim findsIpsum = doc.FindText(
                New FindTextParams("ipsum", True, False),
                OutputRange.All)
            '' Highlight all 'lorem' using semi-transparent orange red:
            For Each find In findsLorem
                For Each ql In find.Bounds
                    doc.Pages(find.PageIndex).Graphics.FillPolygon(ql, Color.FromArgb(100, Color.OrangeRed))
                Next
            Next
            '' Put a violet red border around all 'ipsum':
            For Each find In findsIpsum
                For Each ql In find.Bounds
                    doc.Pages(find.PageIndex).Graphics.DrawPolygon(ql, Color.MediumVioletRed)
                Next
            Next
            ''
            '' Done:
            doc.Save(stream)
        End Using
        Return doc.Pages.Count
    End Function
End Class