''
'' 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 GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Graphics
Imports GrapeCity.Documents.Svg
Imports GrapeCity.Documents.Drawing
'' Shows how to programmatically control which annotations to include when saving a PDF as images.
'' The sample loads a one-page PDF containing several annotations, and saves that page as SVG
'' using DrawAnnotationFilter to include only annotations which satisfy certain conditions
'' (free text annotations that are within the page bounds).
'' The SVG is then inserted as the first page of the resulting PDF.
'' The second page shows the original PDF for reference.
Public Class AnnotationDrawFilter
Public Function CreatePDF(ByVal stream As Stream) As Integer
Dim doc = New GcPdfDocument()
' Load a PDF with some annotations into the document:
Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "AnnotationTypes.pdf"))
doc.Load(fs)
Using svgStream = New MemoryStream()
' Draw the first page of the PDF into an SVG:
doc.Pages(0).SaveAsSvg(svgStream,
New SaveAsImageOptions() With {
.BackColor = Color.Transparent,
.DrawAnnotationFilter = AddressOf FilterAnnotations
})
svgStream.Position = 0
' Create a GcSvgDocument from the saved SVG so that we can draw it in the resulting PDF:
Using svgDoc = GcSvgDocument.FromStream(svgStream)
' Insert a page into the PDF and draw the SVG on it, compare to the original on the 2nd page:
Dim page = doc.Pages.Insert(0)
Dim g = page.Graphics
Dim rc = New RectangleF(36, 36, page.Size.Width - 72, page.Size.Height - 72)
g.DrawRectangle(rc, Color.DarkGoldenrod)
g.FillRectangle(rc, Color.PaleGoldenrod)
g.DrawSvg(svgDoc, rc)
End Using
End Using
' Done:
doc.Save(stream)
End Using
Return doc.Pages.Count
End Function
Private Sub FilterAnnotations(ByVal d As GcPdfDocument, ByVal p As Page, ByVal a As AnnotationBase, ByRef draw As Boolean)
' Draw only free text annotations which also fit horizontally within the page bounds:
If TypeOf a Is FreeTextAnnotation Then
Dim fta = DirectCast(a, FreeTextAnnotation)
draw = fta.Rect.Left > 0 AndAlso fta.Rect.Right < p.Size.Width
Else
draw = False
End If
End Sub
End Class