Docs / Guides

Text and standard fonts

The 14 standard fonts are built into every PDF viewer, so they need no embedding and keep files tiny. This guide covers drawing text with them, aligning and wrapping it, and the characters they can show.

Drawing a line of text

DrawText(text, x, y, font, size, color) draws a single line with its baseline starting at (x, y). It does not wrap, so long text runs off the page. Pass a colour, or leave it out for black.

Program.cs
using SoftZenPdf;

var document = new PdfDocument();
var page = document.AddPage(PageSize.Letter);

const string title = "Quarterly report";
var width = StandardFont.HelveticaBold.MeasureText(title, 28);

page.Content
    .DrawText(title, (page.Width - width) / 2, 720, StandardFont.HelveticaBold, 28)
    .DrawText("Helvetica, Times and Courier need no embedding.", 72, 680, StandardFont.TimesRoman, 14)
    .DrawText("Accents and symbols such as é, ü, € and £ are in WinAnsi.", 72, 660, StandardFont.Courier, 11);

document.Save("standard-fonts.pdf");

The 14 fonts

FamilyStandardFont members
Helvetica (sans-serif)Helvetica, HelveticaBold, HelveticaOblique, HelveticaBoldOblique
Times (serif)TimesRoman, TimesBold, TimesItalic, TimesBoldItalic
Courier (monospaced)Courier, CourierBold, CourierOblique, CourierBoldOblique
SymbolsSymbol (Greek letters, mathematical symbols), ZapfDingbats (decorative symbols)

StandardFont.All lists all fourteen. Viewers may substitute a metrically compatible font, such as Arial for Helvetica.

Which characters work

The Latin fonts use WinAnsiEncoding (the Windows-1252 character set): ASCII, accented Western European letters, and symbols such as €, £, ©, ™, curly quotes, dashes, the ellipsis and the bullet. Any other character throws ArgumentException naming the character. For Greek, Cyrillic, CJK and everything else, use an embedded TrueType font.

Symbol and ZapfDingbats use their own single-byte encodings: you pass the byte's character, and the font draws its symbol at that position (in Symbol, "a" draws α).

Measuring and aligning

MeasureText(text, size) returns the width in points, using Adobe's metrics for the standard fonts, including the same kerning pairs DrawText applies. Subtract it from a right margin to right-align, or centre text by starting at (pageWidth - width) / 2:

Program.cs
using SoftZenPdf;

var document = new PdfDocument();
var page = document.AddPage();
var font = StandardFont.Helvetica;
const double size = 14, left = 72;
var right = page.Width - 72;

page.Content
    .DrawText("Left-aligned", left, 770, font, size)
    .DrawText("Centred", (page.Width - font.MeasureText("Centred", size)) / 2, 745, font, size)
    .DrawText("Right-aligned", right - font.MeasureText("Right-aligned", size), 720, font, size)
    .DrawText("Bold and red", left, 695, StandardFont.HelveticaBold, size, PdfColor.Red);

document.Save("aligned-text.pdf");

Wrapping paragraphs

SoftZenPdf has no text layout engine, so it places each line exactly where you ask. To fit a paragraph into a column, split it into lines yourself. Because MeasureText uses the same widths the viewer draws with, this loop wraps accurately:

Program.cs
using SoftZenPdf;

var font = StandardFont.TimesRoman;
const double size = 12, lineHeight = 16, maxWidth = 450;
var text = "SoftZenPdf draws each line exactly where you ask. To fit a paragraph into a column, " +
           "break it into lines yourself with MeasureText, as this loop does.";

var lines = new List<string>();
var line = "";
foreach (var word in text.Split(' '))
{
    var candidate = line.Length == 0 ? word : $"{line} {word}";
    if (line.Length > 0 && font.MeasureText(candidate, size) > maxWidth)
    {
        lines.Add(line);
        line = word;
    }
    else line = candidate;
}
lines.Add(line);

var document = new PdfDocument();
var content = document.AddPage().Content;
for (var i = 0; i < lines.Count; i++)
    content.DrawText(lines[i], 72, 770 - i * lineHeight, font, size);

document.Save("wrapped-text.pdf");

There is no hyphenation, and justified text needs your own word spacing. The loop breaks at spaces only, so a single word wider than the column overflows it.

Low-level text and rotation

For several lines in one text object, or rotated text, use the text operators directly. SetTextMatrix places and orients the text: for an angle θ, pass cos θ, sin θ, -sin θ, cos θ followed by the start point.

Program.cs
using SoftZenPdf;

var document = new PdfDocument();
var page = document.AddPage();
var angle = 90 * Math.PI / 180;

page.Content
    .BeginText()
    .SetFont(StandardFont.Helvetica, 12)
    .SetTextMatrix(Math.Cos(angle), Math.Sin(angle), -Math.Sin(angle), Math.Cos(angle), 40, 300)
    .ShowText("Rotated 90° along the left margin")
    .EndText();

document.Save("rotated-text.pdf");

See ContentStreamWriter for every text operator, and StandardFont for the font list.