StrikeoutParagraph.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.Linq
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.TextMap
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Common

Public Class StrikeoutParagraph
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        ' Load the PDF:
        Dim doc = New GcPdfDocument()
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"))
            doc.Load(fs)

            ' Strikeout the first paragraph that starts with the words "Several species":
            Dim para As ITextParagraph = Nothing
            For Each p In doc.Pages
                Dim paras = p.GetTextMap().Paragraphs
                para = paras.FirstOrDefault(Function(p_) p_.GetText().StartsWith("Several species"))
                If para IsNot Nothing Then
                    Exit For
                End If
            Next
            If para Is Nothing Then
                Return doc.Pages.Count ' Should not happen.
            End If

            ' Text markup strikeout annotation:
            Dim markup = New TextMarkupAnnotation() With {
                .Page = para.Page,
                .MarkupType = TextMarkupType.StrikeOut,
                .Color = Color.Red
            }

            ' Get the coordinates of all text run fragments in the paragraph,
            ' and add them to the annotation's area:
            Dim area As New List(Of Quadrilateral)
            For Each run In para.Runs
                For Each frag In run
                    ' Note: in v6.0.3 a method ITextRunFragment.GetBounds() was added,
                    ' so this code can be replaced with a single 'frag.GetBounds()' call.
                    ' Find the text run fragment's index in the containing line:
                    Dim index As Integer = 0
                    Dim line = frag.Line
                    Dim frags = line.RunFragments
                    For i = 0 To frags.Count - 1
                        If frags(i) Is frag Then Exit For
                        index += frags(i).Count
                    Next
                    ' Add the fragment's bounds (quadrilateral) to the annotation area:
                    area.Add(frag.Line.GetCoords(index, frag.Count))
                Next
            Next
            markup.Area = area

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