Load font documents from a byte array

.NET API Solution to download TTF, WOFF, EOT, Type 1, and CFF fonts from a MemoryStream.

 

Aspose.Font API Solution has a rich functionality to work with fonts. Conversion, manipulations with glyphs, detection of Latin symbols, and many more. The important step in font manipulations is loading font files, so you could use them afterward.

This page describes the option on how to load font files from streams but the whole functionality is detailly described in the How to load files? article. There you will find much more C# code examples, and find out the objects and parameters required for font loading.

The solution supports the next font formats: TrueType (single font), TrueType (font collection), Web Open Font Format, Web Open Font Format version 2.0, Embedded OpenType, Adobe Type 1 font (pfa, pfb, afm, pfm), and Compact Font Format. The attached code snippets show loading a Type 1 and CFF fonts from a byte array.

To load fonts we need:

  • Aspose.Font for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for C# platform.

  • Open the NuGet package manager, and search for Aspose.Font and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Font

Steps to load Compact Font Format(CFF) from byte array/MemoryStream using C#:

  1. Set the byte array to load the font using FontFileDefinition Class.
  2. Use the static method Open() of object Aspose.Font.Sources.FontDefinition to create the desired FontDefinition object.
  3. Use the object of type ByteContentStreamSource to specify CFF font format.
  4. Pass the appropriate FontDefinition to the Aspose.Font.Font.Open() Method.

C# Code for loading fonts from the stream

    using Aspose.Font;
    using Aspose.Font.Sources;
    using Aspose.Font.Cff;
    //byte array to load Font from
    byte[] fontMemoryData;

    FontDefinition fd = FontDefinition.Open(new ByteContentStreamSource(fontMemoryData), 
                FontType.CFF);
    CffFont cffFont = Aspose.Font.Font.Open(fd) as CffFont;

Steps to load Type 1 font from byte array/MemoryStream using C#:

  1. Set the byte array to load the Type 1 font using FontFileDefinition Class.
  2. Use the static method Open() of object Aspose.Font.Sources.FontDefinition to create the desired FontDefinition object.
  3. Use the object of type ByteContentStreamSource to specify Type 1 font format.
  4. Pass the appropriate FontDefinition to the Aspose.Font.Font.Open() Method.

C# Code for loading fonts from the stream

    using Aspose.Font;
    using Aspose.Font.Sources;
    using Aspose.Font.Type1;
    //byte array to load Font from
    byte[] fontMemoryData;

    FontDefinition fd = FontDefinition.Open(new ByteContentStreamSource(fontMemoryData), 
                FontType.Type1);
    Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;



FAQ

1. What are the parameters to load fonts programmatically?

For correct font loading, you need to know two key parameters. They are font format and storage.

2. Where are fonts located on my computer?

To see the fonts available on your computer go to the C:>Windows>Fonts directory. There you will be able to manage your fonts.

3. How to load a font from a stream?

Set the path array using FontFileDefinition and create the object by means of Open() Method. Specify the font format. Finish loading using Aspose.Font.Font.Open() Method.