MultiFormattedText.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

'' Shows how to use different text formats (fonts, colors) in a single paragraph.
Public Class MultiFormattedText
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' Function to generate sample text quoting its formatting options:
        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

        '' Font names:
        Const times = "times new roman"
        Const arial = "arial"
        '' Create document and text layout:
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics
        Dim tl = g.CreateTextLayout()
        '' Use TextLayout to layout the whole page and maintain margins:
        tl.MaxHeight = page.Size.Height
        tl.MaxWidth = page.Size.Width
        tl.MarginAll = 72
        '' Get some fonts:
        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)
        '' Add text to TextLayout using different fonts and font sizes:
        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)
        '' Add text with different foreground and background colors:
        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)
        '' Finish with plain black on transparent again:
        tl.Append("The end.", New TextFormat() With {.Font = fTimes, .FontSize = 14})
        '' Layout and draw text:
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, PointF.Empty)
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class