InlineImages.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
'' 此示例展示了如何插入任意对象(此示例中的图像)
'' 到文本块中,以便这些对象保持其相对位置
'' 到周围的文本,并且布局与其他文本运行完全相同,
'' 并可以参与文本流。
Public Class InlineImages
Function CreatePDF(ByVal stream As Stream) As Integer
'' 获取用作内联对象的图像:
Using imgPuffins As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "puffins-small.jpg")),
imgFerns As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "ferns-small.jpg"))
'' 要使用的图像对齐:
Dim ia = New ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Bottom, True, True, True, False, False)
'' 创建并设置文档:
Dim doc = New GcPdfDocument()
Dim page = doc.NewPage()
Dim g = page.Graphics
'' 创建并设置一个 TextLayout 对象来打印文本:
Dim tl = g.CreateTextLayout()
tl.MaxWidth = page.Size.Width
tl.MaxHeight = page.Size.Height
tl.MarginLeft = 36
tl.MarginRight = 36
tl.MarginTop = 36
tl.MarginBottom = 36
tl.DefaultFormat.Font = StandardFonts.Times
tl.DefaultFormat.FontSize = 12
tl.DefaultFormat.BackColor = Color.LightGoldenrodYellow
tl.TextAlignment = TextAlignment.Justified
'' 使用图像和任意大小创建内联对象:
Dim ioPuffins = New InlineObject(imgPuffins, 36, 24)
Dim ioFerns = New InlineObject(imgFerns, 36, 24)
'' 构建文本:
tl.Append("TextLayout 类的“内联对象”功能允许插入任意对象" +
"")
tl.Append("这里有一些海雀:")
tl.Append(ioPuffins)
tl.Append("这里有一些蕨类植物:")
tl.Append(ioFerns)
tl.Append("结束。")
''
Debug.Assert(tl.InlineObjects.Count = 0, "InlineObjects is filled by RecalculateGlyphs")
'' 此方法获取并测量渲染文本所需的字形,
'' 因为我们用不同的布局多次绘制相同的文本,
'' 我们在下面的循环之前调用一次:
tl.RecalculateGlyphs()
''
Debug.Assert(tl.InlineObjects.Count = 2, "InlineObjects is filled by RecalculateGlyphs")
'' 在循环中,在 3 个不同的位置绘制文本和内嵌图像
'' 和页面上的边界:
For i = 0 To 2
tl.MarginTop = tl.ContentRectangle.Bottom + 36
tl.MarginLeft = 36 + 72 * i
tl.MarginRight = 36 + 72 * i
'' 注意这里传递“false”,我们不需要重新计算字形,因为
'' 文本没有改变:
tl.PerformLayout(False)
'' 绘制文本和图像:
g.DrawTextLayout(tl, PointF.Empty)
For Each inlineObj In tl.InlineObjects
g.DrawImage(CType(inlineObj.Object, GCDRAW.Image), inlineObj.ObjectRect.ToRectangleF(), Nothing, ia)
Next
Next
''
'' 完毕:
doc.Save(stream)
Return doc.Pages.Count
End Using
End Function
End Class