Convert TTF to WOFF2 | .NET

[ ]

On this page you will find the example of using the Aspose solution for converting from TrueType font to Web Open Font Format version 2.

Use the following namespaces at the head of the file:

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

If you need to know the Aspose conversion fundamentals get to How to convert the font into the desired format? chapter.

Then take the next 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 output settings for the WOFF2 format.

        // Woff2 output settings
        string outPath = Path.Combine(OutputDir, "TtfToWoff2_out1.woff2");
        FileStream outStream = File.Create(outPath);

  3. Make conversion from TTF to WOFF2 and save the result using SaveToFormat() method of the base abstract Font class instance.

        // Convert ttf to woff2
        font.SaveToFormat(outStream, FontSavingFormats.WOFF2);