Convert TTF to WOFF | .NET

Contents
[ ]

If you came across a font you like to use in your interface, and it is only available in TTF format, but you use WOFF in your project, you may need a solution that converts fonts. The following documentation will show you how to easily use the Aspose.Font for .NET to transform fonts from True type to WebFont.


Use the following namespaces at the head of the file:

 
using Aspose.Font.Sources;
using Aspose.Font.Ttf;
using System;
using System.IO;

The fundamentals for the conversion are placed in How to convert the font into the desired format? section.

To convert TTF to WOFF you will need to take the following steps:

  1. Open TTF font.

        // Open ttf font
        string fontPath = Path.Combine(DataDir, "Montserrat-Regular.ttf");
        FontDefinition fontDefinition = new FontDefinition(FontType.TTF, new FontFileDefinition(new FileSystemStreamSource(fontPath)));
        Font font = Font.Open(fontDefinition);
    

  2. Write the settings for the output stream.

        // Woff output settings
        string outPath = Path.Combine(OutputDir, "TtfToWoff_out1.woff");
        FileStream outStream = File.Create(outPath);
    

  3. Convert font to WOFF.

        // Convert ttf to woff
        font.SaveToFormat(outStream, FontSavingFormats.WOFF);
    

Here the SaveToFormat() method of the base abstract Font class instance was used. It takes two arguments: output stream and FontSavingFormats enumeration to choose output font format.