''
'' 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.Common
Public Class MarkupPerPage
Public Function CreatePDF(ByVal stream As Stream) As Integer
' Load the PDF:
Dim doc = New GcPdfDocument()
Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "The-Rich-History-of-JavaScript.pdf"))
doc.Load(fs)
' Find all occurrences of "JavaScript" or "ECMAScript" using regex:
Dim ft = New FindTextParams("JavaScript|ECMAScript", wholeWord:=False, matchCase:=False, regex:=True)
Dim found = doc.FindText(ft, Nothing)
' Add a text markup annotation per page (keys are page indices):
Dim markups As New Dictionary(Of Integer, Tuple(Of TextMarkupAnnotation, List(Of Quadrilateral)))
For Each f In found
Dim markup As Tuple(Of TextMarkupAnnotation, List(Of Quadrilateral)) = Nothing
If Not markups.TryGetValue(f.PageIndex, markup) Then
markup = New Tuple(Of TextMarkupAnnotation, List(Of Quadrilateral))(
New TextMarkupAnnotation() With {
.Page = doc.Pages(f.PageIndex),
.MarkupType = TextMarkupType.Highlight,
.Color = Color.Chartreuse
},
New List(Of Quadrilateral)())
markups(f.PageIndex) = markup
End If
For Each b In f.Bounds
markup.Item2.Add(b)
Next
Next
For Each v In markups.Values
v.Item1.Area = v.Item2
Next
' Done:
doc.Save(stream)
Return doc.Pages.Count
End Using
End Function
End Class