Lay out paragraphs, lists, and tab-aligned columns in a PDF

PDF Formatted Text Drawing Write

This sample shows how to lay out a page of formatted text directly in a PDF using BoundedText's XML-based markup: regular paragraphs with first-line indentation and paragraph spacing, a bulleted list with custom line leading, and a two-column schedule where each session's title flows from the left and its time is pushed to a right-aligned tab stop by a dot leader, the classic layout of a printed program or table of contents.

Сode Snippet

const float dpi = 150f;

var pageWidth = UnitConverter.ConvertUnitsToPixels(dpi, 8.5f, Unit.Inch);
var pageHeight = UnitConverter.ConvertUnitsToPixels(dpi, 11f, Unit.Inch);
var margin = UnitConverter.ConvertUnitsToPixels(dpi, 0.5f, Unit.Inch);

var markup =
    "<p style='space-after:14pt;'><span style='font-size:20pt;'>Workshop Schedule</span></p>" +

    "<p style='first-line-indent:18pt;space-after:16pt;'>" +
    "Below is the full-day schedule, followed by a short list of what to bring. " +
    "Session times are tab-aligned to the right margin with a dot leader, the layout classically " +
    "used in printed programs and tables of contents." +
    "</p>" +

    // Each row is a paragraph declaring a single right-aligned tab stop at 540pt (the box's
    // own width) with a dot leader (character code 46). The row's <span> text starts with
    // the session title flowing naturally from the left; a literal tab character then jumps
    // to that stop, where the time is printed, right-aligned against the dots.
    "<p style='space-after:6pt;'><tab style='alignment:right;leader:#46;'>540pt</tab><span>Opening Keynote\t9:00 AM</span></p>" +
    "<p style='space-after:6pt;'><tab style='alignment:right;leader:#46;'>540pt</tab><span>Color Management Deep Dive\t10:30 AM</span></p>" +
    "<p style='space-after:6pt;'><tab style='alignment:right;leader:#46;'>540pt</tab><span>Working with Vector PDFs\t1:00 PM</span></p>" +
    "<p style='space-after:20pt;'><tab style='alignment:right;leader:#46;'>540pt</tab><span>Closing Panel\t3:30 PM</span></p>" +

    "<p style='space-after:6pt;'><span style='font-size:13pt;'>What to bring:</span></p>" +
    "<ul style='type:#9679;'>" +
    "<li><span style='leading:16pt;'>A laptop with the sample project installed</span></li>" +
    "<li><span style='leading:16pt;'>A printed copy of the schedule above</span></li>" +
    "<li><span style='leading:16pt;'>Questions for the closing panel</span></li>" +
    "</ul>";

using (var writer = new PdfWriter("AdvancedTypography.pdf"))
using (var gr = writer.GetGraphics())
{
    writer.AddPage(pageWidth, pageHeight, dpi, dpi);

    var boundedText = new BoundedText(markup, gr.CreateFont("Arial", 11f), new SolidBrush(RgbColor.Black))
    {
        Rectangle = new System.Drawing.RectangleF(margin, margin, pageWidth - (2 * margin), pageHeight - (2 * margin)),
    };

    gr.DrawText(boundedText);
}

Output

AdvancedTypography.pdf

Download

AdvancedTypography.png

For AI-assisted development: Download Graphics Mill Code Samples XML Catalog