''
'' 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.Numerics
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Graphics
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
Public Class SplitA3toA4x2
Public Function CreatePDF(ByVal stream As Stream) As Integer
' A3 and A4 page sizes:
' A3 is 11.69" x 16.54":
Dim sizeA3 = New SizeF(11.69F * 72, 16.54F * 72)
' A4 is 8.27" x 11.60":
Dim sizeA4 = New SizeF(8.27F * 72, 11.60F * 72)
' Create a sample source PDF with A3 landscape oriented pages:
Dim docA3 = New GcPdfDocument() With {
.PageSize = sizeA3,
.Landscape = True
}
' First page is a horizontal image filling the whole page:
Dim p0 = docA3.Pages.Add()
Dim imgA3 = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "aurora.jpg"))
p0.Graphics.DrawImage(imgA3, New RectangleF(PointF.Empty, docA3.PageSize), Nothing, GCDRAW.ImageAlign.StretchImage)
' Add a few more A3 pages as two columns of random 'lorem ipsum' text:
Dim p1 = docA3.Pages.Add()
Dim g = p1.Graphics
Dim tl = g.CreateTextLayout()
tl.DefaultFormat.Font = StandardFonts.Times
tl.DefaultFormat.FontSize = 12
tl.TextAlignment = GCTEXT.TextAlignment.Justified
tl.FirstLineIndent = 72 / 2
tl.ParagraphSpacing = 72 / 8
' Add random text:
tl.Append(Util.LoremIpsum(37))
' Specify 1/4" margins all around:
Const margin As Single = 72 / 4
tl.MarginAll = margin
Dim colWidth = p1.Size.Width / 2
tl.MaxWidth = colWidth
tl.MaxHeight = p1.Size.Height
tl.PerformLayout(True)
' In a loop, split and render the text in a 2 column layout, adding pages as needed:
Dim col As Integer = 0
Do
' 'rest' will accept the text that did not fit:
Dim rest As GCTEXT.TextLayout = Nothing
Dim splitResult = tl.Split(Nothing, rest)
g.DrawTextLayout(tl, New PointF(col * colWidth, 0))
If splitResult <> GCTEXT.SplitResult.Split Then
Exit Do
End If
tl = rest
col += 1
If col = 2 Then
docA3.Pages.Add()
g = docA3.Pages.Last.Graphics
col = 0
End If
Loop
' At this point, 'docA3' is a PDF with landscape oriented A3 pages,
' the first page contains an image filling the whole page,
' other pages contain random text in two vertical column layout.
' Create the resulting (target) PDF with A4 page size:
Dim doc = New GcPdfDocument() With {
.PageSize = sizeA4,
.Landscape = False
}
' FormXObjects are rendered on graphics like images, but without losing fidelity.
' We create two image alignments for the left and the right halves of the source A3 page:
Dim iaLeft = New GCDRAW.ImageAlign(GCDRAW.ImageAlignHorz.Left, GCDRAW.ImageAlignVert.Top, False, False, True, False, False)
Dim iaRight = New GCDRAW.ImageAlign(GCDRAW.ImageAlignHorz.Right, GCDRAW.ImageAlignVert.Top, False, False, True, False, False)
For Each pA3 In docA3.Pages
' Create FormXObject representing the source page:
Dim fxoA3 = New FormXObject(doc, pA3)
' Draw its left and right halves on separate pages of the target PDF:
Dim pLeft = doc.Pages.Add()
pLeft.Graphics.DrawForm(fxoA3, pLeft.Bounds, pLeft.Bounds, iaLeft)
Dim pRight = doc.Pages.Add()
pRight.Graphics.DrawForm(fxoA3, pLeft.Bounds, pLeft.Bounds, iaRight)
Next
' Done:
doc.Save(stream)
Return doc.Pages.Count
End Function
End Class