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:
-
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);
-
Write the output settings for the
WOFF2
format.// Woff2 output settings string outPath = Path.Combine(OutputDir, "TtfToWoff2_out1.woff2"); FileStream outStream = File.Create(outPath);
-
Make conversion from
TTF
toWOFF2
and save the result using SaveToFormat() method of the base abstract Font class instance.// Convert ttf to woff2 font.SaveToFormat(outStream, FontSavingFormats.WOFF2);
For more examples check Aspose.Font.Examples.sln solution, in the net-examples folder of the Aspose.Font Documentation Github repository.
Also go to our free online Font Conversion application from Aspose to see the functionality.