Load a font from CFF file | .NET

On this page we will have a look at examples of loading font CenturyGothic placed in the file CenturyGothic.cff.


If you did not read the Aspose.Font loading fundamentals, go to How to load fonts? page.

First you need to notify 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 FileInfo object

Follow the algorithm to fulfill the font loading:

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

    // Initialize FontDefinition object passing CFF 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.CFF, fileDef);

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

Font loading with the byte[] type variable and with using ByteContentStreamSource type object

To load font this way, you need to take the following steps:

  1. Construct path to the file.
  2. Initialize FontDefiniton object passing CFF as FontType value, cff as fileExtension value, and ByteContentStreamSource object based on fontBytes array.
  3. Load the font.
    // Construct path to the file
    string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");

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

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

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