SaveAsSvg.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 GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Svg
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text

Public Class SaveAsSvg
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()

        Dim rc = Util.AddNote(
            "We load an existing PDF into a temporary GcPdfDocument, " &
            "save its first page as an SVG image to a stream, and then " &
            "draw that image on the page of the resulting PDF.",
            page)

        ' Load a one page document into a temp PDF:
        Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "svg-spec-art.pdf"))
            Dim docSrc = New GcPdfDocument()
            docSrc.Load(fs)

            ' Save the first page of the loaded PDF as SVG:
            Using svgStream = New MemoryStream()
                docSrc.Pages(0).SaveAsSvg(svgStream, New SaveAsImageOptions() With {.BackColor = Color.Transparent})
                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)
                    ' For illustration purposes we render the SVG with transparent background
                    ' on a rectangle with a custom fill and border:
                    rc = New RectangleF(0, rc.Bottom, page.Size.Width, page.Size.Height - rc.Bottom)
                    rc.Inflate(-4.0F, -4.0F)
                    page.Graphics.DrawRectangle(rc, Color.DarkGoldenrod)
                    page.Graphics.FillRectangle(rc, Color.PaleGoldenrod)
                    page.Graphics.DrawSvg(svgDoc, rc)
                End Using
            End Using
        End Using

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