Simple tutorial for programmers — part 3

RTF – Working with Tables

Defining rows and cells, adding borders, controlling padding, and border style reference

This page builds on the first example. The original use case was a database report with three columns: a question column, a current-period comments column, and a last-period comments column, with rows for each question.

Twips reminder: RTF measures space in twips — 1/20th of a point, with 72 points per inch. That means 1440 twips = 1 inch. So \cellx2880 places a cell boundary at exactly 2 inches from the left margin.

Basic table structure

Each row starts with \trowd and ends with \row. Within a row, cell boundaries are declared with \cellxN (the right edge of each cell in twips), and cell content ends with \intbl\cell. The example below creates one row with three cells, ending at 1000, 2000, and 3000 twips:

RTF {\rtf1\ansi\deff0 \trowd \cellx1000 \cellx2000 \cellx3000 cell 1\intbl\cell cell 2\intbl\cell cell 3\intbl\cell \row }

Rendered output

cell 1 cell 2 cell 3

Auto-expanding row height

Unlike tabs, table rows automatically grow to fit their content. If one cell contains a long string, the entire row height expands to accommodate it:

RTF {\rtf1\ansi\deff0 \trowd \cellx1000 \cellx2000 \cellx3000 cell 1\intbl\cell lots of text in cell two\intbl\cell cell 3\intbl\cell \row }

Rendered output

cell 1 lots of text in cell two cell 3

Adding cell borders

Border commands are placed before each \cellxN declaration. The four sides are top (\clbrdrt), left (\clbrdrl), bottom (\clbrdrb), and right (\clbrdrr). Follow each with a border style — \brdrs gives a plain single line:

RTF {\rtf1\ansi\deff0 \trowd \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs \cellx1000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs \cellx2000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs \cellx3000 cell 1\intbl\cell lots of text in cell two\intbl\cell cell 3\intbl\cell \row }

Rendered output

cell 1 lots of text in cell two cell 3

Cell padding with \trgaph

Without padding, text sits flush against the cell border. \trgaphN on the \trowd line sets the half-gap between cell content and borders. A value of 144 twips gives a comfortable 1/10" of breathing room:

RTF {\rtf1\ansi\deff0 \trowd\trgaph144 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs \cellx1000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs \cellx2000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs \cellx3000 cell 1\intbl\cell lots of text in cell two\intbl\cell cell 3\intbl\cell \row }

Rendered output (with padding)

cell 1 lots of text in cell two cell 3

Adding a second row

Repeat the \trowd\row block for each additional row. Use \par and \pard between rows to reset paragraph state cleanly:

RTF {\rtf1\ansi\deff0 \trowd\trgaph144 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs\cellx1000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs\cellx2000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs\cellx3000 cell 1\intbl\cell lots of text in cell two\intbl\cell cell 3\intbl\cell \row \trowd\trgaph144 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs\cellx1000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs\cellx2000 \clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs\cellx3000 row 2 cell 1\intbl\cell lots of text in row 2 cell two\intbl\cell row 2 cell 3\intbl\cell \row }

Rendered output

cell 1 lots of text in cell two cell 3
row 2 cell 1 lots of text in row 2 cell two row 2 cell 3

Border style reference

Instead of \brdrs (single line), a range of other border styles are available. The RTF command is applied to all four sides of the cell — for example, replacing \brdrs with \brdrdot gives a dotted border. Download the example .rtf file to see all styles rendered in a word processor.

RTF command Description Preview
\brdrs Single line abc
\brdrdot Dotted abc
\brdrdb Double thickness abc
\brdrdash Dashed abc
\brdrdashsm Small dashes abc
\brdrdashd Dot-dash abc
\brdrdashdd Dot-dot-dash abc
\brdrtriple Triple line abc
\brdrtnthlg Thick-thin (large) abc
\brdrthtnlg Thin-thick (large) abc
\brdrtnthtnlg Thin-thick-thin (large) abc
\brdrwavy Wavy abc
\brdrdashdotstr Striped abc
\brdremboss Embossed abc
\brdrengrave Engraved abc
Note: Some border styles — wavy, triple, thick-thin — are only rendered by word processors that fully implement the RTF 1.5 specification. Plain text editors will ignore them.

Navigation

Part 2 – Fonts, color, and tab leaders Part 1 – RTF basics: color, fonts, layout, and formatting Download borders.rtf — all border styles in one file Rich Text Format (RTF) Version 1.5 Specification