Load a font from TTF file | .NET

Here are described four differently fulfilled examples on how to load a font from the file Montserrat-Regular.ttf.


First learn the loading fundamentals at the How to load fonts? page.

Add the next namespaces at the head of the file:

using System;
using Aspose.Font;
using Aspose.Font.Sources;
using System.IO;

Loading from the file using FileSystemStreamSource object

Take the next steps to fulfil the operation:

  1. Construct path to the file.
  2. Initiate FontDefiniton object.
  3. Set fileExtension to ttf.
  4. Load the font.
    // Construct path to the file
    string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");

    // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
    FontFileDefinition fileDef = new FontFileDefinition("ttf", new FileSystemStreamSource(fontPath));

    // Based on FileSystemStreamSource object, set fileExtension to "ttf"
    FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);

    // Load the font
    Font font = Font.Open(fontDef);

Loading from the file using FileInfo object

To fulfil loading do the next:

  1. Construct path to the file.
  2. Initiate FontDefiniton object passing TTF as FontType value.
  3. Get automatically calculated value fileExtension.
  4. Load the font.
     // Construct path to the file
     string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");

     // Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition
     FontFileDefinition fileDef = new FontFileDefinition(new FileInfo(fontPath));

     // Based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
     FontDefinition fontDef = new FontDefinition(FontType.TTF, fileDef);

     // Load the font
     Font font = Font.Open(fontDef);

Loading from the file excluding FontFileDefinition object from initialization chain

The next actions have to be taken to load the font this way:

  1. Construct path to the file.
  2. Initiate FontDefiniton object passing TTF as FontType value, ttf as fileExtension value and FileSystemStreamSource object. Parameter fileExtension here is not a duplicate value for parameter FontType.
  3. Load the font.
     // Construct path to the file
     string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");

     // Initialize FontDefinition object passing TTF as FontType value, "ttf" as fileExtension value, 
     // and FileSystemStreamSource object. Parameter 'fileExtension' here is not duplicate value 
     // for parameter 'FontType' and it's needed for correct font format detection
     FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new FileSystemStreamSource(fontPath));

     // Load the font
     Font font = Font.Open(fontDef);     

Loading the font from the byte array

To load the font from the byte array:

  1. Construct path to the file.
  2. Load font binary data into the byte array.
  3. Initialize FontDefiniton object passing TTF as FontType value,ttf as fileExtension value, and ByteContentStreamSource object based on fontBytes array.
  4. Load the font.
     // Construct path to the file
     string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");

     // Load font binary data into byte array
     byte[] fontBytes = File.ReadAllBytes(fontPath);

     // Initialize FontDefinition object  passing TTF as FontType value, "ttf" as fileExtension value, 
     // and ByteContentStreamSource object based on fontBytes array
     FontDefinition fontDef = new FontDefinition(FontType.TTF, "ttf", new ByteContentStreamSource(fontBytes);

     // Load the font
     Font font = Font.Open(fontDef);