Docs / Guides
Custom font embedding
Load a TrueType font to draw any Unicode text it covers. SoftZenPdf embeds only the glyphs you use and adds a map back to Unicode, so the text can be copied, searched and extracted.
Loading and using a font
TrueTypeFont.FromFile reads a font file; FromBytes takes the bytes, for example from an
embedded resource. A TrueTypeFont works anywhere a StandardFont does: DrawText,
SetFont, MeasureText and text watermarks.
using SoftZenPdf;
var font = TrueTypeFont.FromFile("NotoSans-Regular.ttf");
var document = new PdfDocument();
document.AddPage().Content
.DrawText("Ελληνικά, Кириллица, Latin: one embedded font", 72, 770, font, 16)
.DrawText($"{font.FamilyName}, subset to the glyphs this page uses", 72, 745, font, 11, PdfColor.Gray);
document.Save("truetype-font.pdf");
How embedding works
- Subsetting. When the document is saved, the font is cut down to the glyphs used anywhere in it, so a page of text typically adds kilobytes, not the whole font.
- Unicode encoding. The font is written as a Type0 font with a CIDFontType2 descendant and Identity-H encoding, which can address every glyph in the font.
- Copyable text. A ToUnicode CMap maps each glyph back to its characters (several, for a ligature), so viewers can copy and search the text.
- One embedding per instance. Each
TrueTypeFontobject is embedded separately. Load a font once and reuse the object across pages; loading the same file twice embeds it twice.
Checking which characters a font has
Drawing a character the font lacks throws ArgumentException. Check coverage first with
TryGetGlyphId, for example to fall back to another font:
using SoftZenPdf;
var font = TrueTypeFont.FromFile("NotoSans-Regular.ttf");
Console.WriteLine($"{font.FamilyName} {font.SubfamilyName}: {font.GlyphCount} glyphs");
foreach (var ch in "A€Ω中")
Console.WriteLine(font.TryGetGlyphId(ch, out var glyph) ? $"{ch}: glyph {glyph}" : $"{ch}: not in this font");
Noto Sans Regular: 3884 glyphs
A: glyph 36
€: glyph 538
Ω: glyph 373
中: not in this font
Supported font files
- Supported: fonts with TrueType (
glyf) outlines, in.ttffiles or.otffiles that contain TrueType outlines. - Not supported: CFF-flavoured OpenType fonts (most
.otffiles from Adobe and many foundries) and TrueType collections (.ttc). Both throwNotSupportedExceptionwith an explanation.
Many font families, including Noto, are published in both flavours. If an .otf file is rejected, look for the TrueType (.ttf) build of the same font.
Font licences
A font's licence decides whether it may be embedded. SoftZenPdf reads the embedding permissions stored in the font
(OS/2 fsType) and throws NotSupportedException for fonts marked restricted, bitmap-only or
no-subsetting. Fonts that allow embedding may still come with other licence terms, so check the licence of any font you
ship. Open-licence families such as Noto (SIL Open Font License) are a safe default.
Kerning and ligatures
Text is kerned with the font's pair adjustments (the OpenType GPOS table, or the older kern
table), and standard ligatures such as fi and ffi from its GSUB table replace the letters
they stand for. MeasureText measures the text as it is drawn, and a ligature still copies and extracts as its
separate letters. Turn either off per font with Kerning and Ligatures:
using SoftZenPdf;
var font = TrueTypeFont.FromFile("NotoSans-Regular.ttf");
var plain = TrueTypeFont.FromFile("NotoSans-Regular.ttf");
plain.Kerning = false;
plain.Ligatures = false;
var document = new PdfDocument();
document.AddPage().Content
.DrawText("AVATAR, Wave, office: kerned, with the ffi ligature", 72, 770, font, 20)
.DrawText("AVATAR, Wave, office: no kerning, no ligatures", 72, 740, plain, 20);
Console.WriteLine($"\"AVATAR\" is {font.MeasureText("AVATAR", 20):F1} pt kerned, {plain.MeasureText("AVATAR", 20):F1} pt plain");
document.Save("kerning-ligatures.pdf");
"AVATAR" is 69.5 pt kerned, 73.9 pt plain
The standard 14 fonts are kerned too, with the pairs in Adobe's metrics; they have no ligatures.
Right-to-left text
Hebrew and other right-to-left text is reordered for display with the Unicode Bidirectional Algorithm (Unicode 16.0,
checked against all of Unicode's conformance tests), so a line can mix right-to-left words with numbers and left-to-right
words. Brackets are mirrored, and the text is marked with its reading order so that it copies, searches and extracts
correctly. DrawText still starts at x and runs to the right, so right-align a line by subtracting
its width from the right margin:
using SoftZenPdf;
var hebrew = TrueTypeFont.FromFile("NotoSansHebrew-Regular.ttf");
const string text = "שלום עולם"; // "Hello, world"
var document = new PdfDocument();
var page = document.AddPage();
// Right-align: the line ends 72 pt from the right edge.
var width = hebrew.MeasureText(text, 24);
page.Content.DrawText(text, page.Width - 72 - width, 770, hebrew, 24);
document.Save("right-to-left.pdf");
// Copying or extracting the text gives it back in reading order.
var extracted = PdfReader.Open("right-to-left.pdf").ExtractText(0);
Console.WriteLine($"Extracted text matches: {extracted == text}");
Extracted text matches: True
A line's base direction comes from its first strong character. To set it yourself, for example for a right-to-left
line that starts with a Latin word, set page.Content.TextDirection to TextDirection.RightToLeft
(or LeftToRight); it applies to the text drawn after it.
Arabic
Arabic letters take their joined forms (initial, medial, final or isolated, decided by the Unicode joining rules), required ligatures such as lam-alef are formed, and dots and vowel marks are placed on their letters with the font's mark anchors. All of it comes from the font's own OpenType tables, so use a font made for Arabic, such as Noto Sans Arabic. The same joining applies to Syriac, N'Ko and other cursively joined scripts when the font supports them.
using SoftZenPdf;
var arabic = TrueTypeFont.FromFile("NotoSansArabic-Regular.ttf");
string[] lines =
[
"مرحبا بالعالم", // "Hello, world": letters join into words
"السلام عليكم", // lam-alef becomes a ligature
"بِسْمِ اللَّهِ", // vowel marks sit on their letters
"الطبعة ٢٠٢٦", // Arabic-Indic digits stay left to right
];
var document = new PdfDocument();
var page = document.AddPage();
var y = 770.0;
foreach (var line in lines)
{
// Right-align each line 72 pt from the right edge.
page.Content.DrawText(line, page.Width - 72 - arabic.MeasureText(line, 24), y, arabic, 24);
y -= 40;
}
document.Save("arabic.pdf");
var extracted = PdfReader.Open("arabic.pdf").ExtractText(0);
Console.WriteLine($"Extracted text matches: {extracted == string.Join("\n", lines)}");
Extracted text matches: True
Some viewers drop small marks, such as the dots under letters, from hinted fonts at certain screen sizes; the marks are in the file and appear when zoomed or printed. It is a property of the font's hinting and the viewer's rasteriser, not of the PDF.
Scripts that need more shaping
Indic scripts such as Devanagari, Bengali and Tamil reorder letters within a syllable and form conjuncts, which
SoftZenPdf does not do yet, so they do not render correctly. Fonts that join letters with cursive attachment
(curs, common in Urdu Nastaliq fonts) are not connected, and language-specific forms (for example Persian or
Urdu variants selected by locl) are not applied. See Known limitations.
API: TrueTypeFont, TextDirection, ContentStreamWriter.TextDirection.