Load, Save and Extract Information from Fonts using C#

.NET Font Management API

In digital typography, fonts define particular styles to be used for the characters’ appearance. Most often, the fonts are utilized in the documents and the web pages to stylize the text. Each font is described in a file that contains information about characters’ size, weight, styles, as well as encoding. Since fonts are an important part of the various file formats, Aspose offers a dedicated API for dealing with the manipulation and rendering of popular font types including TrueType, CFF, OpenType, and Type1. In this article, you will learn how to load, save, and extract information from fonts using C# with Aspose.Font for .NET.

.NET Font Manipulation API – Free Download

Aspose.Font for .NET is an on-premise API that lets you implement the font management features using C# within your .NET applications. Along with loading, saving, and information extraction from the font, the API also allows you to render the glyph or text if required. You can either download the API or install it within your .NET applications using NuGet.

PM> Install-Package Aspose.Font

Load or Save Fonts From File using C#

You can load a font from the file stored on your digital storage for retrieving the font’s information. Aspose.Font for .NET exposes separate classes to deal with the particular types of fonts i.e. CFF, TrueType, etc. The following are the steps to load and save a font.

  • Load the font from the file (using file’s path or byte array) using FontDefinition class.
  • Access font’s information using CffFont, TtfFont, or Type1Font class.
  • Save the file (if required).

Load and Save CFF Font

The following code sample shows how to load and save CFF fonts using C#.

Load and Save TrueType Font

The following code sample shows how to load and save TrueType fonts using C#.

Load and Save Type1 Font

The following code sample shows how to load and save Type1 fonts using C#.

Extract Font Metrics using C#

Aspose.Font for .NET also allows you to retrieve the font metrics information such as Ascender, Descender, TypoAscender, TypoDescender, and UnitsPerEm. The following are the steps to do so.

  • Use FontDefinition class to load TrueType or Type1 font from file.
  • Open font using respective font type class i.e. Type1Font, TtfFont, etc.
  • Access the font’s metrics information.

The following code samples show how to extract font metrics from TrueType and Type1 fonts using C#.

Extract Metrics from TrueType Font

Extract Metrics from Type1 Font

Detect Latin Symbols in Fonts using C#

You can also detect Latin text in the fonts by decoding the glyph codes using Aspose.Font for .NET. The following are the steps to perform this operation.

  • Load the Font from the file using FontDefinition class.
  • Decode the GlyphId using the DecodeToGid() method of the respective font class.

The following code samples show how to decode Latin symbols in TrueType and Type1 fonts using C#.

Detect Latin Symbols in TrueType Font

Detect Latin Symbols in Type1 Font

Conclusion

In this article, you have seen how to load and save CFF, TrueType and Type1 fonts programmatically using C#. Furthermore, you have learned how to extract font metrics information from TrueType and Type1 fonts within your .NET applications. You can explore more interesting features offered by Aspose.Font for .NET in the documentation. For more updates, keep visiting Aspose.Font blog.