Load a font from CFF file | .NET
Contents
[
Hide
]
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:
- Construct path to the file.
- Initiate FontDefiniton object passing
CFF
as FontType value. - Get automatically calculated value fileExtension.
- 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:
- Construct path to the file.
- Initialize FontDefiniton object passing
CFF
as FontType value,cff
as fileExtension value, and ByteContentStreamSource object based on fontBytes array. - 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);
More examples on how to use Aspose.Font are in Aspose.Font.Examples.sln solution, in the net-examples of the Aspose.Font Documentation.