Loading and Extracting Data | .NET

Load CFF Font from Disc

Aspose.Font for .NET API lets you read CFF Font types from files stored in your digital storage. CFF font files stored on disc can be loaded by using the following steps.

Both examples use CFF/29645.cff under DataDir, which points to the tests-data/Aspose.Font directory in the examples project. When using the snippets in your application, set DataDir to your font data directory. Include the System, System.IO, Aspose.Font, Aspose.Font.Cff, and Aspose.Font.Sources namespaces.

1// Load a CFF font from disk
2
3string fileName = Path.Combine(DataDir, "CFF", "29645.cff");
4FontDefinition fontDefinition = new FontDefinition(FontType.CFF,
5    new FontFileDefinition("cff", new FileSystemStreamSource(fileName)));
6CffFont cffFont = Font.Open(fontDefinition) as CffFont
7    ?? throw new InvalidOperationException("Expected a CFF font.");

Load CFF Font from Byte Array

The following example reads the CFF file into a byte array with File.ReadAllBytes, then loads the font using ByteContentStreamSource.

1// Load a CFF font from a byte array
2
3byte[] fontMemoryData = File.ReadAllBytes(Path.Combine(DataDir, "CFF", "29645.cff"));
4FontDefinition fontDefinition = new FontDefinition(FontType.CFF,
5    new FontFileDefinition("cff", new ByteContentStreamSource(fontMemoryData)));
6CffFont cffFont = Font.Open(fontDefinition) as CffFont
7    ?? throw new InvalidOperationException("Expected a CFF font.");