Skip to content

PdfFontCollection class

View as Markdown

Namespace: PdfNetStack.Fonts
Assembly: PdfNetStack.dll
Package: PdfNetStack

Fonts found by family name, weight, width and style, the way CSS matches them. Add font files or directories, or use System for the fonts installed on the machine. Adding a file reads only its names and style; the font itself loads when first found. Thread safe.

public sealed class PdfFontCollection

Inheritance

object ← PdfFontCollection

PdfFont body = PdfFontCollection.System.Find("Segoe UI") ?? PdfStandardFont.Helvetica;
var fonts = new PdfFontCollection();
fonts.AddDirectory("fonts");
PdfFont? heading = fonts.Find("Inter", weight: 700);

A variable font counts for every weight, width and slant its axes cover: asking for weight 650 of a family whose font has a wght axis from 100 to 900 gives the instance at 650. Its named instances are faces of the family too, and like any face can also be found by full name, such as “Inter SemiBold”.

Family names, sorted.

public IReadOnlyList<string> Families { get; }

IReadOnlyList<string>

The fonts installed on this machine (Windows, macOS and Linux font directories), scanned on first use.

public static PdfFontCollection System { get; }

PdfFontCollection

Adds a loaded font under its family name. A variable font as loaded adds its whole design space and its named instances; an instance from GetInstance adds that one location.

public void Add(PdfOpenTypeFont font)

font PdfOpenTypeFont

Adds the fonts in a directory with a font extension (.ttf, .otf, .ttc, .otc, .woff, .woff2). Files that are not readable fonts are skipped, and so are directories that are missing or cannot be listed. When recursive, directories below are searched to 16 levels, without following links to directories. Returns how many fonts were added.

public int AddDirectory(string path, bool recursive = true)

path string

recursive bool

int

Adds every font of a file: one, or all fonts of a collection. Returns how many were added.

public int AddFile(string path)

path string

int

FontFormatException

The file is not a supported font.

The font of family closest to the requested weight and style, chosen as CSS does (CSS Fonts 4, 5.2): for weights 400 to 500, heavier up to 500 first, then lighter, then heavier; below 400, lighter first; above 500, heavier first. An italic is preferred when asked for, an upright face otherwise. A variable font gives its instance at the weight asked when its wght axis covers it. Null when the family is unknown.

public PdfOpenTypeFont? Find(string family, int weight = 400, bool italic = false)

family string

Family name, case insensitive, such as “Inter” or “Times New Roman”, or a face’s full name.

weight int

CSS weight, 1 to 1000: 400 regular, 700 bold.

italic bool

True to prefer an italic or oblique face.

PdfOpenTypeFont?

The font of family that best matches query, chosen as CSS does (CSS Fonts 4, 5.2): the closest width first, then style, then weight. A variable font gives its instance at the values found, clamped to its axes, with the query’s optical size and variations applied over them (CSS Fonts 4, 7.2). Each location is made once, so finding it again returns the same font. Null when the family is unknown.

public PdfOpenTypeFont? Find(string family, PdfFontQuery query)

family string

Family name, case insensitive, such as “Inter”, or a face’s full or PostScript name, such as “Inter SemiBold”.

query PdfFontQuery

The weight, width, style and variations wanted.

PdfOpenTypeFont?