Loading Web‑Open‑Font (WOFF) files | .NET
Contents
[
Hide
Show
]Loading Web‑Open‑Font (WOFF) Files with Aspose.Font for .NET
What you need
| Item | Description | Where to get |
|---|---|---|
| Aspose.Font for .NET | Core library that reads, writes and converts font files (including WOFF/WOFF2). | dotnet add package Aspose.Font or via the NuGet UI (https://www.nuget.org/packages/Aspose.Font). |
| Target framework | .NET Framework 4.x / .NET Core 3.x / .NET 5/6/7 – any platform supported by Aspose.Font. | No extra runtime required. |
| Namespace imports | csharp\nusing Aspose.Font;\nusing Aspose.Font.Sources;\nusing System.IO;\n |
Tip: All API docs are at https://reference.aspose.com/font/net/.
Loading a WOFF file from the file system
Step‑by‑step
- Locate the font file (e.g.
C:\Fonts\MyWebFont.woff). - Create a
FileSystemStreamSourcethat gives the library a stream to that file. - Wrap the stream source in a
FontFileDefinition– specify the extension"woff"(the library uses this to pick the right parser). - Create a
FontDefinition– pass the appropriateFontType(Woff) and theFontFileDefinition. - Open the font via
Font.Open()– you obtain an object derived fromAspose.Font.Font(e.g.WoffFont).
1using System;
2using System.IO;
3using Aspose.Font;
4using Aspose.Font.Sources;
5
6class LoadWoffExample
7{
8 static void Main()
9 {
10 // Path to the .woff file
11 string woffPath = @"C:\Fonts\MyWebFont.woff";
12
13 // Stream source that reads directly from the file system
14 FileSystemStreamSource streamSrc = new FileSystemStreamSource(woffPath);
15
16 // FontFileDefinition – we tell Aspose it is a “woff” file
17 FontFileDefinition fileDef = new FontFileDefinition("woff", streamSrc);
18
19 // FontDefinition – specify the font type (Woff)
20 FontDefinition fontDef = new FontDefinition(FontType.WOFF, fileDef);
21
22 // Load the font
23 Font woffFont = Font.Open(fontDef);
24
25 // woffFont now gives you access to glyphs, metrics, encoding, etc.
26 Console.WriteLine($"Loaded font: {woffFont.GetType().Name}");
27 }
28}Result:
woffFontis an instance ofAspose.Font.Woff.WoffFont. You can now render text, inspect glyph metrics, or convert it to another format (TTF, SVG, …).
Alternative ways to load WOFF fonts
Loading from a byte array (e.g. when the font is embedded in a resource or downloaded over HTTP)
1byte[] woffBytes = File.ReadAllBytes(@"C:\Fonts\MyWebFont.woff");
2
3// ByteContentStreamSource wraps the byte[] as a readable stream
4ByteContentStreamSource byteSrc = new ByteContentStreamSource(woffBytes);
5
6// Create definition – note we still supply "woff" as extension
7FontDefinition fd = new FontDefinition(FontType.WOFF,
8 "woff",
9 byteSrc);
10
11Font woffFont = Font.Open(fd);Loading directly from a stream without an intermediate FileDefinition
If you already have an open FileStream:
1using (FileStream fs = File.OpenRead(@"C:\Fonts\MyWebFont.woff"))
2{
3 // Wrap the live FileStream in FileSystemStreamSource
4 var streamSrc = new FileSystemStreamSource(fs);
5
6 // Directly pass stream source + extension into FontDefinition
7 var fd = new FontDefinition(FontType.WOFF, "woff", streamSrc);
8 var woffFont = Font.Open(fd);
9}Both approaches use the same underlying classes, only the source of bytes changes (disk file vs memory vs custom stream).
After loading – what can you do?
- Render text – use
Aspose.Font.Renderingsubsystem or export glyph outlines. - Read font metrics – e.g.
((WoffFont)woffFont).Metrics. - Convert to another format – call
font.Save("output.ttf");after casting to the appropriate subclass or using the generic API. - Subset / merge – combine several fonts into one collection.
- Extract licensing info – if present in the WOFF header.
All of these are demonstrated in the official examples repository:
📂 https://github.com/aspose-font/Aspose.Font-Documentation/tree/master/net-examples
Quick checklist (for documentation pages)
- ✅ Add NuGet badge:
[](https://www.nuget.org/packages/Aspose.Font/) - ✅ Include sample code block (as shown above) with proper syntax highlighting.
- ✅ Mention supported extensions table (copy from section 2 above).
- ✅ Link to API reference for:
Aspose.Font.Font.OpenAspose.Font.Sources.FileSystemStreamSourceAspose.Font.Sources.ByteContentStreamSourceAspose.Font.FontFileDefinitionAspose.Font.FontDefinition
- ✅ Provide “Further reading” links:
- Getting started: https://docs.aspose.com/font/net/getting-started/
- Full API reference: https://reference.aspose.com/font/net/
- Free online demo: https://products.aspose.app/font/conversion
- ✅ Add troubleshooting tip: If you receive “Unsupported font format”, verify that you passed both
FontType.WOFFand a"woff"extension.