PageFormXObject.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Numerics
Imports System.Collections.Generic
Imports System.Linq
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Graphics

'' This sample shows how to create FormXObject representing a page
'' from a PDF loaded into a temporary GcPdfDocument, and render that
'' object into the current document.
Public Class PageFormXObject
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics

        Dim rc = Util.AddNote(
            "The thumbnails below are pages from an existing document. " +
            "We load an arbitrary PDF into a temporary GcPdfDocument, " +
            "then create a FormXObject representing each of its pages, " +
            "and render those objects as thumbnails into the current document. " +
            "To show that the same FormXObject can be used more than once, " +
            "we also render each thumbnail a second time applying a mirror transform.",
            page)

        '' Layout params:
        Dim margin = rc.Left
        Dim pad = 36
        Dim side = (page.Size.Width - margin * 2 - pad) / 2
        Dim ip = New PointF(margin, rc.Bottom + pad)
        '' Mirror transform:
        Dim tr = Matrix3x2.CreateScale(-1, 1) * Matrix3x2.CreateTranslation(page.Size.Width, 0)
        '' Text format for the overlaid page captions:
        Dim clr = Color.DarkRed
        Dim tf = New TextFormat() With
        {
            .Font = StandardFonts.HelveticaBold,
            .FontSize = 16,
            .ForeColor = Color.FromArgb(128, clr)
        }
        '' Open an arbitrary PDF, load it into a temp document and loop over its pages,
        '' drawing each into the current document:
        Using fs = New FileStream(Path.Combine("Resources", "PDFs", "Wetlands.pdf"), FileMode.Open, FileAccess.Read)
            Dim doc1 = New GcPdfDocument()
            doc1.Load(fs)
            '' Create a list of FormXObject for the pages of the loaded PDF:
            Dim fxos = New List(Of FormXObject)()
            doc1.Pages.ToList().ForEach(Sub(p_) fxos.Add(New FormXObject(doc, p_)))
            '' Render the thumbnails into the current document:
            For i = 0 To fxos.Count - 1
                If (ip.Y + side > page.Size.Height - margin) Then
                    page = doc.NewPage()
                    g = page.Graphics
                    ip = New PointF(margin, margin)
                End If
                Dim rcfx = New RectangleF(ip.X, ip.Y, side, side)
                '' Draw direct:
                g.DrawForm(fxos(i), rcfx, Nothing, ImageAlign.ScaleImage)
                g.DrawRectangle(rcfx, clr)
                g.DrawString($"Page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, False)
                '' Draw reversed:
                g.Transform = tr
                g.DrawForm(fxos(i), rcfx, Nothing, ImageAlign.ScaleImage)
                g.DrawRectangle(rcfx, clr)
                g.Transform = Matrix3x2.Identity
                rcfx.Offset(side + pad, 0)
                g.DrawString($"Reversed page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, False)
                ''
                ip.Y += side + pad
            Next
        End Using
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class