FontFromFile.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text

'' This short sample demonstrates how a Font can be loaded from a file
'' And used in your code to render text.
'' The sample relies on font files Gabriola.ttf And timesbi.ttf to exist in the
'' Resources/Fonts folder.
'' 
'' NOTE 1: When Font.FromFile() Is used, the actual data Is loaded on demand,
'' so that usually a Font instance will Not take too much space.
'' The situation Is different for fonts created using Font.FromArray()
'' And Font.FromStream() methods - in those cases the whole font Is
'' immediately loaded into memory. The font will still be parsed
'' only on demand, but memory consumption Is slightly higher,
'' so using Font.FromFile() should generally be preferred.
''
'' NOTE 2: When different Font instances (created using any of the static ctors
'' mentioned above) are used to render text in a PDF, each instance will result
'' in embedding a separate subset of glyphs even if the glyphs are the same,
'' because GcPdf has no way of knowing that two different Font instances 
'' represent the same physical font. So either make sure that only one Font instance
'' Is created for each physical font, Or better yet use the FontCollection class
'' to add the fonts you need, And specify them via TextFormat.FontName.
Public Class FontFromFile
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim gabriola = Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"))
        If gabriola Is Nothing Then
            Throw New Exception("Could not load font Gabriola")
        End If

        '' Now that we have our font, use it to render some text:
        Dim tf = New TextFormat() With {.Font = gabriola, .FontSize = 16}
        Dim doc = New GcPdfDocument()
        Dim g = doc.NewPage().Graphics
        g.DrawString($"Sample text drawn with font {gabriola.FontFamilyName}.", tf, New PointF(72, 72))
        '' We can change the font size:
        tf.FontSize += 4
        g.DrawString("The quick brown fox jumps over the lazy dog.", tf, New PointF(72, 72 * 2))
        '' We can force GcPdf to emulate bold or italic style with a non-bold (non-italic) font, e.g.:
        tf.FontStyle = FontStyle.Bold
        g.DrawString("This line prints with the same font, using emulated bold style.", tf, New PointF(72, 72 * 3))
        '' But of course rather than emulated, it is much better to use real bold/italic fonts.
        '' So finally, get a real bold italic font and print a line with it:
        Dim timesbi = Font.FromFile(Path.Combine("Resources", "Fonts", "timesbi.ttf"))
        If timesbi Is Nothing Then
            Throw New Exception("Could not load font timesbi")
        End If
        tf.Font = timesbi
        tf.FontStyle = FontStyle.Regular
        g.DrawString($"This line prints with {timesbi.FullFontName}.", tf, New PointF(72, 72 * 4))
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class