Load font documents from disc

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

 

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 the disc but the whole functionality is carefully 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 font collection (TTC) and TTf font from a disc.

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 TTC fonts from a disc using C#:

  1. Set the path to the documents directory.
  2. Initialize FontDefinition object passing TTF as FontType value and using FontFileDefinition Class.
  3. Pass the appropriate FontDefinition to the Aspose.Font.Font.Open() Method.

C# Code for loading fonts from the disc

    using System;
    using Aspose.Font;
    using Aspose.Font.Sources;
    using Aspose.Font.Ttf;
    string fileName = @"C:\Windows\Fonts\cambria.ttc";
    TtcFontSource source = new TtcFontSource(fileName);
    FontDefinition[] fds = source.GetFontDefinitions();
    TtfFont cambriaFont = null;
    Console.WriteLine(string.Format("Collection contains {0} fonts", fds.Length));
    for (int index = 0; index < fds.Length; index++)
    {
        string fontName = fds[index].FontName;
        Console.WriteLine(string.Format("Font by index {0} has name \"{1}\"", index, fontName));
        if (fontName == "Cambria")
            cambriaFont = Aspose.Font.Font.Open(fds[index]) as TtfFont;
    }
    if (cambriaFont != null)
        Console.WriteLine(string.Format("Font \"Cambria\" was found in collection. This font contains {0} glyphs", cambriaFont.NumGlyphs));

Steps to load .ttf fonts from a disc using C#:

  1. Create an object Aspose.Font.Sources.FontDefinition to describe font location and format.
  2. To reference files on a disc, use the object Aspose.Font.Sources.FileSystemStreamSource .
  3. Use the Aspose.Font.FontType.TTF value to specify the TrueType format.
  4. To return the specified font use the Aspose.Font.Font.Open() Method.

C# Code for loading fonts from the disc

    //Font file name with full path
    string fileName; 

    FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
    TtfFont ttfFont = Aspose.Font.Font.Open(fd) as TtfFont;



FAQ

1. How to load a font from a disc?

Set the path to the documents directory. Define the font using objects of the FontFileDefinition Class and load the font by means of the Aspose.Font.Font.Open() Method.

2. How do I load fonts on a PC?

Download font files or archives to your PC. Unpack the archive. Right-click on the font file to choose the Open option. Choose the Install option and as soon as installed the font is ready to use.

3. How do I import and use fonts?

If using windows, go to the C:>Windows>Fonts directory. To use the loaded font you need to copy and paste the font file to this folder.