''
'' 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.Text
Imports GrapeCity.Documents.Pdf.TextMap
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Structure
Imports GrapeCity.Documents.Pdf.Recognition.Structure
'' Highlight paragraphs which have associated structure tags.
Public Class ReadTagsShowParas
Public Function CreatePDF(ByVal stream As Stream) As Integer
Dim user = "DsPdfWeb Demo"
Dim doc = New GcPdfDocument()
Using s = File.OpenRead(Path.Combine("Resources", "PDFs", "C1Olap-QuickStart.pdf"))
doc.Load(s)
'' 1st step - remove all but the first 5 pages from the loaded PDF,
' also removing tags that point to the removed pages:
For i = doc.Pages.Count - 1 To 5 Step -1
RemoveStructNodesForPage(doc.StructTreeRoot.Children, doc.Pages(i))
doc.Pages.RemoveAt(i)
Next
'' 2nd step - get the logical structure, highlight paragraphs
' and add sticky notes to them:
Dim ls As LogicalStructure = doc.GetLogicalStructure()
HighlightParagraphs(ls.Elements, user)
'' Done:
doc.Save(stream)
Return doc.Pages.Count
End Using
End Function
Private Sub RemoveStructNodesForPage(ses As StructElementCollection, p As Page)
For i = ses.Count - 1 To 0 Step -1
Dim se = ses(i)
If se.DefaultPage Is p Then
ses.RemoveAt(i)
Else
RemoveStructNodesForPage(se.Children, p)
End If
Next
End Sub
Private Sub HighlightParagraphs(items As IReadOnlyList(Of Element), user As String)
Dim colorVal = Color.FromArgb(64, Color.Magenta)
For Each e In items
If e.HasContentItems Then
For Each i In e.ContentItems
If TypeOf i Is ContentItem Then
Dim ci = DirectCast(i, ContentItem)
Dim p = ci.GetParagraph()
If p IsNot Nothing Then
Dim rc = p.GetCoords().ToRect()
rc.Offset(rc.Width, 0)
rc.Size = New SizeF(16, 12)
Dim ta = New TextAnnotation() With {
.UserName = user,
.Rect = rc,
.Page = ci.Page,
.Contents = p.GetText(),
.Color = Color.Yellow
}
ci.Page.Graphics.DrawPolygon(p.GetCoords(), colorVal)
End If
End If
Next
End If
If e.HasChildren Then
HighlightParagraphs(e.Children, user)
End If
Next
End Sub
End Class