PdfFont Class

A font that PdfContentBuilder shows text with. A font does not belong to a document: each document that uses it gets its own font objects.

public abstract class PdfFont

Remarks

The metrics are in glyph space units, 1/1000 of the font size. The class cannot be derived outside the library and its instances are immutable and thread-safe.

Properties

NameDescription
AscentGets the height above the baseline of the tallest glyphs, in glyph space units (1/1000 em).
CapHeightGets the height of the flat capital letters, in glyph space units.
DescentGets the depth below the baseline of the lowest glyphs, a negative number in glyph space units.
PostScriptNameGets the PostScript name of the font, the /BaseFont of its font dictionary.

Ascent

Gets the height above the baseline of the tallest glyphs, in glyph space units (1/1000 em).

public abstract double Ascent { get; }

Property value

double

CapHeight

Gets the height of the flat capital letters, in glyph space units.

public abstract double CapHeight { get; }

Property value

double

Descent

Gets the depth below the baseline of the lowest glyphs, a negative number in glyph space units.

public abstract double Descent { get; }

Property value

double

PostScriptName

Gets the PostScript name of the font, the /BaseFont of its font dictionary.

public abstract string PostScriptName { get; }

Property value

string

Methods

NameDescription
CanEncode(string)Indicates whether every character of a text has a glyph in the font.
FromFace(FontFace)Creates a font to embed in the documents that use it from a font face. See PdfFont.Load.
GetStandardFont(PdfStandardFont)Gets a standard font (ISO 32000-2, 9.6.2.2).
GetTextWidth(string, double)Gets the width of a text shown with the font, in user space units, without character or word spacing.
Load(ReadOnlySpan<byte>, int)Loads a TrueType or OpenType font (.ttf, .otf, .ttc, .woff, .woff2) to embed in the documents that use it.
Load(string, int)Loads a TrueType or OpenType font file to embed in the documents that use it. See PdfFont.Load.

CanEncode(string)

Indicates whether every character of a text has a glyph in the font.

public abstract bool CanEncode(string text)

Parameters

text string
The text.

Returns

bool true when the font can show the whole text.

FromFace(FontFace)

PdfFont.cs

Creates a font to embed in the documents that use it from a font face. See PdfFont.Load.

public static PdfFont FromFace(FontFace face)

Parameters

face FontFace
The face.

Returns

PdfFont The font.

Exceptions

UnsupportedPdfFeatureException
The outlines of the font cannot be embedded, or its license does not allow embedding.

GetStandardFont(PdfStandardFont)

PdfFont.cs

Gets a standard font (ISO 32000-2, 9.6.2.2).

public static PdfFont GetStandardFont(PdfStandardFont font)

Parameters

font PdfStandardFont
The font.

Returns

PdfFont The font.

GetTextWidth(string, double)

PdfFont.cs

Gets the width of a text shown with the font, in user space units, without character or word spacing.

public double GetTextWidth(string text, double fontSize)

Parameters

text string
The text.
fontSize double
The font size.

Returns

double The width.

Exceptions

ArgumentException
A character of the text has no glyph in the font.

Load overloads

Load(ReadOnlySpan<byte>, int)

PdfFont.cs

Loads a TrueType or OpenType font (.ttf, .otf, .ttc, .woff, .woff2) to embed in the documents that use it.

public static PdfFont Load(ReadOnlySpan<byte> data, int faceIndex = 0)
Parameters
data ReadOnlySpan<byte>
The bytes of the font file.
faceIndex int
The index of the font in a collection (.ttc); 0 for a file of one font.
Returns

PdfFont The font.

Exceptions
InvalidFontException
The file is not a valid font.
ArgumentOutOfRangeException
faceIndex is not the index of a font of the file.
UnsupportedPdfFeatureException
The outlines of the font cannot be embedded (CFF2 or bitmap fonts), or its license does not allow embedding.
Remarks

Each document embeds the subset of the glyphs it shows, in a composite font whose codes are the glyph identifiers (ISO 32000-2, 9.7), with a ToUnicode CMap so that the text can be extracted. The text is shaped with the OpenType layout tables of the font (ligatures, kerning, marks, complex scripts) and ordered by the Unicode Bidirectional Algorithm.

Load(string, int)

PdfFont.cs

Loads a TrueType or OpenType font file to embed in the documents that use it. See PdfFont.Load.

public static PdfFont Load(string path, int faceIndex = 0)
Parameters
path string
The path of the font file.
faceIndex int
The index of the font in a collection (.ttc); 0 for a file of one font.
Returns

PdfFont The font.

Exceptions
InvalidFontException
The file is not a valid font.
ArgumentOutOfRangeException
faceIndex is not the index of a font of the file.
UnsupportedPdfFeatureException
The outlines of the font cannot be embedded, or its license does not allow embedding.