๐Ÿ”ค Text Formatting

Fonts, sizes, colours, multi-line blocks, and fine-grained text control.

C# โ€” Text Formatting
using ObviousPDF;
using ObviousPDF.Accessibility;

var doc = new PdfDocument();
doc.Info.Title = "Text Formatting";
doc.Language = "en-US";
doc.DisplayDocTitle = true;
var root = doc.EnableTaggedPdf();
var page = doc.AddPage();

// Bold heading
var h1 = root.AddChild(StructureType.H1);
page.AddTaggedText(h1, "Text Formatting", 72, 740,
    new PdfTextOptions {
        Font = StandardFont.HelveticaBold,
        FontSize = 24
    });

// Different font families
var p1 = root.AddChild(StructureType.P);
page.AddTaggedText(p1, "Helvetica (Sans)", 72, 700,
    new PdfTextOptions {
        Font = StandardFont.Helvetica, FontSize = 14
    });

var p2 = root.AddChild(StructureType.P);
page.AddTaggedText(p2, "Times Roman (Serif)", 72, 680,
    new PdfTextOptions {
        Font = StandardFont.TimesRoman, FontSize = 14
    });

// Coloured text
var pRed = root.AddChild(StructureType.P);
page.AddTaggedText(pRed, "Red warning text", 72, 640,
    new PdfTextOptions {
        Color = new PdfColor(0.85, 0.1, 0.1),
        Font = StandardFont.HelveticaBold,
        FontSize = 14
    });

// Multi-line text block with custom leading
var pBlock = root.AddChild(StructureType.P);
page.AddTaggedTextBlock(pBlock, new[] {
    "This is a multi-line text block.",
    "Each line advances automatically.",
    "Leading controls vertical spacing."
}, 72, 580, new PdfTextOptions {
    FontSize = 12, Leading = 18
});

// Mixed fonts in one text block
var pMix = root.AddChild(StructureType.P);
int mcid = page.BeginTaggedContent(pMix);
page.BeginTextBlock()
    .SetFont(StandardFont.Helvetica, 14)
    .MoveTextTo(72, 500)
    .ShowText("Normal, ")
    .SetFont(StandardFont.HelveticaBold, 14)
    .ShowText("bold, ")
    .SetFont(StandardFont.HelveticaOblique, 14)
    .ShowText("italic")
    .EndTextBlock();
page.EndTaggedContent();

doc.Save("text_formatting.pdf");
Screenshot of the Text Formatting PDF showing a large bold heading, three lines in Helvetica, Times Roman, and Courier fonts, red and blue coloured text, a multi-line paragraph with 18pt leading, and a mixed-style line with normal, bold, and italic text

You should see a bold 24 pt heading, font-family samples in Helvetica and Times Roman, red warning text, a three-line paragraph with generous spacing, and a mixed-style line reading "Normal, bold, italic" at the bottom.
File: 02_text_formatting.pdf

โ™ฟ Accessibility Tip

Use AddTaggedText() for all visible text so screen readers can read it in the correct order. Use BeginTaggedContent()/EndTaggedContent() for fine-grained text blocks.

Available Fonts

FontStyleEmbedded Substitute
HelveticaSans-serifSora Regular (SIL OFL)
HelveticaBoldSans-serif boldSora Bold
TimesRomanSerifCMU Serif Roman
CourierMonospaceCMU Typewriter