This sample shows how to use fonts in Docotic.Pdf library.

Docotic.Pdf library provides several ways to add fonts to PDF document. You can add a font from a file using PdfDocument.AddFontFromFile(..) method. This method supports TrueType fonts, TrueType font collections and Type1 fonts.

You can also add any font installed on your system, a one of 14 built-in fonts or a font created from a System.Drawing.Font object using PdfDocument.AddFont(..) method. The names of 14 built-in PDF fonts are in PdfBuiltInFont enumeration.

You can specify a font to use on canvas using PdfCanvas.Font property. PdfCanvas.FontSize property specifies the size of a font.

C#
using System.Diagnostics;
using System.Drawing;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class Fonts
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "Fonts.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                PdfCanvas canvas = pdf.Pages[0].Canvas;

                // NOTE: PdfDocument.AddFont() overloads, which load font from the collection of system fonts,
                // are not supported in version for .NET Standard
                PdfFont systemFont = pdf.AddFont("Arial", FontStyle.Italic | FontStyle.Strikeout);
                canvas.Font = systemFont;
                canvas.DrawString(10, 50, "Hello, world!");

                PdfFont builtInFont = pdf.AddFont(PdfBuiltInFont.TimesRoman);
                canvas.Font = builtInFont;
                canvas.DrawString(10, 70, "Hello, world!");

                PdfFont fontFromFile = pdf.AddFontFromFile("Sample data/HolidayPi_BT.ttf");
                canvas.Font = fontFromFile;
                canvas.DrawString(10, 90, "Hello, world!");

                // Remove unused glyphs from TrueType fonts to optimize size of result PDF file.
                systemFont.RemoveUnusedGlyphs();
                fontFromFile.RemoveUnusedGlyphs();

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Drawing

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class Fonts
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "Fonts.pdf"

            Using pdf As New PdfDocument()
                Dim canvas As PdfCanvas = pdf.Pages(0).Canvas

                ' NOTE: PdfDocument.AddFont() overloads, which load font from the collection of system fonts,
                ' are not supported in version for .NET Standard
                Dim systemFont As PdfFont = pdf.AddFont("Arial", FontStyle.Italic Or FontStyle.Strikeout)
                canvas.Font = systemFont
                canvas.DrawString(10, 50, "Hello, world!")

                Dim builtInFont As PdfFont = pdf.AddFont(PdfBuiltInFont.TimesRoman)
                canvas.Font = builtInFont
                canvas.DrawString(10, 70, "Hello, world!")

                Dim fontFromFile As PdfFont = pdf.AddFontFromFile("Sample data/HolidayPi_BT.ttf")
                canvas.Font = fontFromFile
                canvas.DrawString(10, 90, "Hello, world!")

                ' Remove unused glyphs from TrueType fonts to optimize size of result PDF file.
                systemFont.RemoveUnusedGlyphs()
                fontFromFile.RemoveUnusedGlyphs()

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace