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:

1using System;
2using Aspose.Font
3using Aspose.Font.Sources;
4using 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.
 1    // Construct path to the file.
 2    string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");
 3
 4    // Initialize FontDefinition object passing CFF as FontType value and using FontFileDefinition
 5    FontFileDefinition fileDef = new FontFileDefinition(new FileInfo(fontPath));     
 6
 7    // Based on FileInfo object, fileExtension value is calculated automatically from FileInfo fields.
 8    FontDefinition fontDef = new FontDefinition(FontType.CFF, fileDef);
 9
10    // Load the font 
11    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.
 1    // Construct path to the file
 2    string fontPath = Path.Combine(DataDir, "CenturyGothic.cff");
 3
 4    // Load font binary data into byte array
 5    byte[] fontBytes = File.ReadAllBytes(fontPath);
 6
 7    // Initialize FontDefinition object  passing CFF as FontType value, "cff" as fileExtension value, 
 8    // and ByteContentStreamSource object based on fontBytes array
 9    FontDefinition fontDef = new FontDefinition(FontType.CFF, "ttf", new ByteContentStreamSource(fontBytes));
10
11    // Load the font
12    Font font = Font.Open(fontDef);

More examples on how to use Aspose.Font are in Aspose.Font.Examples.sln solution, in the net-examples of the Aspose.Font Documentation.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.