PageFormXObject.vb
'' 完毕:
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

'' 此示例演示如何创建表示页面的 FormXObject
'' 从加载到临时 GcPdfDocument 的 PDF 中,然后渲染该文件
'' 对象到当前文档中。
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(
            "下面的缩略图是现有文档的页面。" +
            "",
            page)

        '' 布局参数:
        Dim margin = rc.Left
        Dim pad = 36
        Dim side = (page.Size.Width - margin * 2 - pad) / 2
        Dim ip = New PointF(margin, rc.Bottom + pad)
        '' 镜像变换:
        Dim tr = Matrix3x2.CreateScale(-1, 1) * Matrix3x2.CreateTranslation(page.Size.Width, 0)
        '' 覆盖页面标题的文本格式:
        Dim clr = Color.DarkRed
        Dim tf = New TextFormat() With
        {
            .Font = StandardFonts.HelveticaBold,
            .FontSize = 16,
            .ForeColor = Color.FromArgb(128, clr)
        }
        '' 打开任意 PDF,将其加载到临时文档中并循环遍历其页面,
        '' 将每个绘制到当前文档中:
        Using fs = New FileStream(Path.Combine("Resources", "PDFs", "Wetlands.pdf"), FileMode.Open, FileAccess.Read)
            Dim doc1 = New GcPdfDocument()
            doc1.Load(fs)
            '' 为加载的 PDF 页面创建 FormXObject 列表:
            Dim fxos = New List(Of FormXObject)()
            doc1.Pages.ToList().ForEach(Sub(p_) fxos.Add(New FormXObject(doc, p_)))
            '' 将缩略图渲染到当前文档中:
            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)
                '' 直接绘制:
                g.DrawForm(fxos(i), rcfx, Nothing, ImageAlign.ScaleImage)
                g.DrawRectangle(rcfx, clr)
                g.DrawString($"Page {i + 1}", tf, rcfx, TextAlignment.Center, ParagraphAlignment.Center, False)
                '' 绘制反转:
                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
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class