Loading Web‑Open‑Font (WOFF) files | .NET

Loading Web‑Open‑Font (WOFF) Files with Aspose.Font for .NET

What you need

ItemDescriptionWhere to get
Aspose.Font for .NETCore 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 importscsharp\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

  1. Locate the font file (e.g. C:\Fonts\MyWebFont.woff).
  2. Create a FileSystemStreamSource that gives the library a stream to that file.
  3. Wrap the stream source in a FontFileDefinition – specify the extension "woff" (the library uses this to pick the right parser).
  4. Create a FontDefinition – pass the appropriate FontType (Woff) and the FontFileDefinition.
  5. Open the font via Font.Open() – you obtain an object derived from Aspose.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: woffFont is an instance of Aspose.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?

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)

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.