MultiFormattedText.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

'' 该示例展示了如何使用不同的文本格式
'' (字体、颜色)在一个段落中。
Public Class MultiFormattedText
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' 生成引用其格式选项的示例文本的函数:
        Dim makeSampleText As Func(Of TextFormat, String) =
            Function(ByVal tf_)
                Dim boldItalic = String.Empty
                If tf_.Font.FontBold Then
                    boldItalic = "bold "
                End If
                If tf_.Font.FontItalic Then
                    boldItalic += "italic "
                End If
                If boldItalic = String.Empty Then
                    boldItalic = "normal "
                End If
                Return $"This is {boldItalic}text drawn using font '{tf_.Font.FullFontName}', font size {tf_.FontSize} points, " +
                    $"text color {tf_.ForeColor}, background color {tf_.BackColor}. "
            End Function

        '' 字体名称:
        Const times = "times new roman"
        Const arial = "arial"
        '' 创建文档和文本布局:
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics
        Dim tl = g.CreateTextLayout()
        '' 使用 TextLayout 布局整个页面并保持边距:
        tl.MaxHeight = page.Size.Height
        tl.MaxWidth = page.Size.Width
        tl.MarginAll = 72
        '' 获取一些字体:
        Dim fc = New FontCollection()
        fc.RegisterDirectory(Path.Combine("Resources", "Fonts"))
        Dim fTimes = fc.FindFamilyName(times, False, False)
        Dim fTimesBold = fc.FindFamilyName(times, True, False)
        Dim fTimesItalic = fc.FindFamilyName(times, False, True)
        Dim fTimesBoldItalic = fc.FindFamilyName(times, True, True)
        Dim fArial = fc.FindFamilyName(arial, False, False)
        '' 使用不同的字体和字体大小将文本添加到 TextLayout:
        Dim tf = New TextFormat() With {.Font = fTimes, .FontSize = 12}
        tl.Append(makeSampleText(tf), tf)
        tf.Font = fTimesBold
        tf.FontSize += 2
        tl.Append(makeSampleText(tf), tf)
        tf.Font = fTimesItalic
        tf.FontSize += 2
        tl.Append(makeSampleText(tf), tf)
        tf.Font = fTimesBoldItalic
        tf.FontSize += 2
        tl.Append(makeSampleText(tf), tf)
        tf.Font = fArial
        tf.FontSize += 2
        tl.Append(makeSampleText(tf), tf)
        '' 添加具有不同前景色和背景色的文本:
        tf.Font = fTimesBold
        tf.ForeColor = Color.Tomato
        tl.Append(makeSampleText(tf), tf)
        tf.Font = fTimesBoldItalic
        tf.FontSize = 16
        tf.ForeColor = Color.SlateBlue
        tf.BackColor = Color.Orange
        tl.Append(makeSampleText(tf), tf)
        '' 再次在透明上涂上纯黑色:
        tl.Append("The end.", New TextFormat() With {.Font = fTimes, .FontSize = 14})
        '' 布局和绘制文本:
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, PointF.Empty)
        ''
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class