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

Public Class MarkupPerWord
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Const word0 As String = "JavaScript"
        Const word1 As String = "framework"

        ' Load the PDF:
        Dim doc = New GcPdfDocument()
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf"))
            doc.Load(fs)

            ' A FindTextParams for each of the search words:
            Dim ft0 = New FindTextParams(word0, wholeWord:=False, matchCase:=False, regex:=False)
            Dim ft1 = New FindTextParams(word1, wholeWord:=False, matchCase:=False, regex:=False)

            ' Find and highlight words on the pages, caching and reusing the text map
            ' of each page for the two searches:
            For Each p In doc.Pages
                ' Find and highlight the first word:
                Dim textMap = p.GetTextMap()
                Dim markup0 As TextMarkupAnnotation = Nothing
                Dim area0 As List(Of Quadrilateral) = Nothing
                textMap.FindText(ft0, Sub(fp_)
                                          If markup0 Is Nothing Then
                                              markup0 = New TextMarkupAnnotation() With {
                                                  .Subject = word0,
                                                  .Page = doc.Pages(fp_.PageIndex),
                                                  .MarkupType = TextMarkupType.Highlight,
                                                  .Color = Color.Cyan
                                              }
                                          End If
                                          If area0 Is Nothing Then
                                              area0 = New List(Of Quadrilateral)()
                                          End If
                                          For Each b In fp_.Bounds
                                              area0.Add(b)
                                          Next
                                      End Sub)
                If markup0 IsNot Nothing Then
                    markup0.Area = area0
                End If

                ' Find and highlight the second word:
                Dim markup1 As TextMarkupAnnotation = Nothing
                Dim area1 As List(Of Quadrilateral) = Nothing
                textMap.FindText(ft1, Sub(fp_)
                                          If markup1 Is Nothing Then
                                              markup1 = New TextMarkupAnnotation() With {
                                                  .Subject = word1,
                                                  .Page = doc.Pages(fp_.PageIndex),
                                                  .MarkupType = TextMarkupType.Highlight,
                                                  .Color = Color.Fuchsia
                                              }
                                          End If
                                          If area1 Is Nothing Then
                                              area1 = New List(Of Quadrilateral)()
                                          End If
                                          For Each b In fp_.Bounds
                                              area1.Add(b)
                                          Next
                                      End Sub)
                If markup1 IsNot Nothing Then
                    markup1.Area = area1
                End If
            Next

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