TextAroundImages.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
'' 此示例展示了如何在矩形区域周围流动大块文本,
'' 在本例中是图像。它还演示了如何获取实际边界
'' 使用特定 ImageAlign 在页面上呈现的图像的图像。
Public Class TextAroundImages
Function CreatePDF(ByVal stream As Stream) As Integer
Dim doc = New GcPdfDocument()
Dim g = doc.NewPage().Graphics
'' 我们想在第一页的某些任意位置绘制 3 个图像,
'' 然后打印需要 2-3 页的文本,并让它四处流动
'' 第一页上的图像。
''
'' 获取图像及其矩形。请注意,我们指定了一个正方形
'' 所有图像的区域 - 但它们将在该区域内对齐
'' 保留它们原来的长宽比,所以我们稍后会检索
'' 绘制图像的实际矩形:
Using imgPuffins As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "puffins.jpg")),
imgReds As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg")),
imgLavender As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "lavender.jpg"))
Dim rectPuffins = New RectangleF(100, 70, 180, 180)
Dim rectReds = New RectangleF(300, 280, 180, 180)
Dim rectLavender = New RectangleF(190, 510, 180, 180)
'' 设置 ImageAlign 使图像适合并居中于指定区域内,
'' 保留图像的原始长宽比:
Dim ia = New ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, True, True, True, False, False)
'' 绘制每个图像,为每个 DrawImage 调用提供一个矩形数组作为输出参数,
'' 这样我们就得到了图像所拍摄的实际矩形(需要一个数组来处理平铺图像):
Dim rectsPuffins As RectangleF() = Nothing
g.DrawImage(imgPuffins, rectPuffins, Nothing, ia, rectsPuffins)
Dim rectsReds As RectangleF() = Nothing
g.DrawImage(imgReds, rectReds, Nothing, ia, rectsReds)
Dim rectsLavender As RectangleF() = Nothing
g.DrawImage(imgLavender, rectLavender, Nothing, ia, rectsLavender)
'' 创建并设置一个 TextLayout 对象来打印文本:
Dim tl = g.CreateTextLayout()
tl.DefaultFormat.Font = StandardFonts.Times
tl.DefaultFormat.FontSize = 9
tl.TextAlignment = TextAlignment.Justified
tl.ParagraphSpacing = 72 / 8
tl.MaxWidth = doc.PageSize.Width
tl.MaxHeight = doc.PageSize.Height
'' 周围 1/2" 边距
tl.MarginAll = 72 / 2
'' ObjectRect 是用于指定要流向 TextLayout 的区域的类型。
'' 我们设置一个本地函数来根据图像矩形创建一个 ObjecRect,
'' 添加一些填充以使结果看起来更好:
Dim makeObjectRect As Func(Of RectangleF, ObjectRect) =
Function(ByVal rect_)
Return New ObjectRect(rect_.X - 6, rect_.Y - 2, rect_.Width + 12, rect_.Height + 4)
End Function
'' 指定 TextLayout 上的 ObjectRect 数组:
tl.ObjectRects = New List(Of ObjectRect)() From {
makeObjectRect(rectsPuffins(0)),
makeObjectRect(rectsReds(0)),
makeObjectRect(rectsLavender(0))
}
'' 添加几段文字:
tl.Append(Util.LoremIpsum(7, 5, 6, 28, 32))
'' 计算字形并布置文本:
tl.PerformLayout(True)
'' 拆分选项可控制页面之间的文本拆分。
'' 我们可以使用默认的 ctor 并设置 MaxWidth 等值,
'' 或者基于TextLayout创建一个TextSplitOptions,并清除RestObjectRects:
Dim tso = New TextSplitOptions(tl) With {
.RestObjectRects = Nothing,
.MinLinesInFirstParagraph = 2,
.MinLinesInLastParagraph = 2
}
'' 在循环中,分割并渲染文本:
While True
'' 'rest' 将接受不适合的文本:
Dim rest As TextLayout = Nothing
Dim splitResult = tl.Split(tso, rest)
doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty)
If splitResult <> SplitResult.Split Then
Exit While
End If
tl = rest
'' 我们只在第一页上绘制图像:
tl.ObjectRects = Nothing
doc.Pages.Add()
End While
''
'' 完毕:
doc.Save(stream)
Return doc.Pages.Count
End Using
End Function
End Class