Fonts define the graphical representation of an individual character in the text, such as its size, display style, weight, color, etc. TTF (True Type Font) works fine on all digital platforms and gives excellent quality on-screen and in printed documents. On the other hand, the WOFF (Web Open Font Format) is mostly used on web pages. It contains format-specific compression and additional XML metadata. In certain cases, we may need to convert true-type fonts into Web fonts. In this article, we will learn how to convert TTF to WOFF in C#.
The following topics shall be covered in this article:
C# API to Convert TTF to WOFF
For converting TTF to WOFF, we will be using the Aspose.Font for .NET API. It allows loading, saving, and extracting information from supported font types.
Please either download the DLL of the API or install it using NuGet.
PM> Install-Package Aspose.Font
Convert TTF to WOFF using C#
We can easily convert True Type Fonts (TTF) to Web Open Fonts (WOFF) by following the steps given below:
- Firstly, load the font file using the FileSystemStreamSource class.
- Next, create an instance of the FontFileDefinition class with the source file object as an argument.
- Then, initialize the FontDefinition class object with Font type as TTF and FontFileDefinition object.
- After that, open the font using the Font.Open() method with the FontDefinition object as an argument.
- Finally, call the SaveToFormat() method to save it. It takes the output file path and FontSavingFormats to WOFF as arguments.
The following code sample demonstrates how to convert TTF to WOFF using C#.
Convert TTF to WOFF2 in C#
WOFF2 is an updated version of WOFF. It offers more compression due to which it is smaller in file size and provides better performance for modern browsers. We can also convert TTF to WOFF2 by following the steps mentioned earlier. However, we just need to set the FontSavingFormats as WOFF2 in the last step.
The following code sample demonstrates how to convert TTF to WOFF2 using C#.
Get a Free License
You can get a free temporary license to try the library without evaluation limitations.
TTF to WebFont - Learning Resources
Beides converting TTF to web fonts, explore various other features of Aspose.Font for .NET using the resources below:
Conclusion
In this article, we have learned how to convert True Type Fonts (TTF) to Web Open Fonts (WOFF) in C#. We have also seen how to save TTF as WOFF2 programmatically. In case of any ambiguity, please feel free to contact us on our free support forum.