Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert TTF to WOFF using C#
// This code example demonstrates how to convert TTF to WOFF.
// TTF file path
string fontPath = @"D:\Files\font\Montserrat-Regular.ttf";
// Load the Font file
FileSystemStreamSource source = new FileSystemStreamSource(fontPath);
// Create font file definition
FontFileDefinition fileDefinition = new FontFileDefinition(source);
// Create font definition
FontDefinition fontDefinition = new FontDefinition(FontType.TTF, fileDefinition);
// Open font
Font font = Font.Open(fontDefinition);
// WOFF output path
string outPath = @"D:\Files\font\TtfToWoff_out1.woff";
FileStream outStream = File.Create(outPath);
// Convert TTF to WOFF
font.SaveToFormat(outStream, FontSavingFormats.WOFF);
// This code example demonstrates how to convert TTF to WOFF2.
// TTF file path
string fontPath = @"D:\Files\font\Montserrat-Regular.ttf";
// Load the Font file
FileSystemStreamSource source = new FileSystemStreamSource(fontPath);
// Create font file definition
FontFileDefinition fileDefinition = new FontFileDefinition(source);
// Create font definition
FontDefinition fontDefinition = new FontDefinition(FontType.TTF, fileDefinition);
// Open font
Font font = Font.Open(fontDefinition);
// WOFF output path
string outPath = @"D:\Files\font\TtfToWoff_out1.woff";
FileStream outStream = File.Create(outPath);
// Convert TTF to WOFF
font.SaveToFormat(outStream, FontSavingFormats.WOFF2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment