Simple tutorial for programmers
RTF – Rich Text Format
Working with .rtf files: color, fonts, tabs, layout, and formatting commands
The starting point for this tutorial was a detailed RTF 1.5 specification, but the immediate requirement was to produce an output file with certain lines highlighted in a different color, like so:
This line is the default color
This line is red
This line is the default color
A Word-generated .rtf file for the above output was 2,460 bytes and contained much irrelevant overhead. After experimenting, the same result can be achieved with this minimal RTF:
RTF
{\rtf1\ansi\deff0
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line
\cf2
This line is red\line
\cf1
This line is the default color
}
Note: Any space before a \ symbol is included in the document text — omit leading spaces. To include a literal backslash in the document, write \\.
Adding a font and basic commands
To specify the default font, add {\fonttbl {\f0 Courier;}} to the first line, replacing Courier with your preferred font name. Two useful commands: \tab inserts a tab character and \page inserts a page break.
RTF
{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line
\cf2
\tab This line is red and has a tab before it\line
\cf1
\page
This line is the default color and the first line on page 2
}
Defining tab stops
Tab stops are set with \txN where N is in twips (1440 twips = 1 inch). The example below sets stops at 0.5", 1", 2", and 4":
RTF
{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
\tx720\tx1440\tx2880\tx5760
This line is the default color\line
\tab this line has 1 tab\line
\tab\tab this line has 2 tabs\line
\tab\tab\tab this line has 3 tabs\line
\tab\tab\tab\tab this line has 4 tabs\line
\cf2
\tab This line is red and has a tab before it\line
\cf1
\page
This line is the default color and the first line on page 2
}
Page orientation and margins
Set landscape orientation with \landscape. Define page dimensions in twips — for an 11" × 8.5" landscape page use \paperw15840\paperh12240. Set 0.5" margins all around with \margl720\margr720\margt720\margb720.
RTF
{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
\landscape
\paperw15840\paperh12240\margl720\margr720\margt720\margb720
\tx720\tx1440\tx2880\tx5760
This line is the default color\line
\tab this line has 1 tab\line
\tab\tab this line has 2 tabs\line
\tab\tab\tab this line has 3 tabs\line
\tab\tab\tab\tab this line has 4 tabs\line
\cf2
\tab This line is red and has a tab before it\line
\cf1
\page
This line is the default color and the first line on page 2
}
Resetting tab stops mid-document
To use different tab stops in a later section, start a new paragraph and redefine them with \par\pard\txN:
RTF
{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
\landscape
\paperw15840\paperh12240\margl720\margr720\margt720\margb720
\tx720\tx1440\tx2880\tx5760
This line is the default color\line
\tab this line has 1 tab\line
\tab\tab this line has 2 tabs\line
\tab\tab\tab this line has 3 tabs\line
\tab\tab\tab\tab this line has 4 tabs\line
\cf2
\tab This line is red and has a tab before it\line
\cf1
\page
\par\pard\tx1440\tx2880
This line is the default color and the first line on page 2\line
\tab\tab This is the second tab on the second line on the second page\line
}
Text formatting commands
- \fsNFont size — N is twice the point size, so
\fs24 = 12 pt
- \i / \i0Italics on / off
- \b / \b0Bold on / off
- \scaps / \scaps0Small caps on / off
- \strike / \strike0Strikethrough on / off
- \caps / \caps0All capitals on / off
- \outl / \outl0Outline on / off
Underline variants
All underline styles are turned off with \ul0.
| Command |
Style |
| \ul | Standard underline |
| \uldb | Double underline |
| \ulth | Thick underline |
| \ulw | Words only (skips spaces) |
| \ulwave | Wave underline |
| \uld | Dotted underline |
| \uldash | Dash underline |
| \uldashd | Dot-dash underline |
Complete formatting example
The following RTF file demonstrates all the formatting commands above across three pages:
RTF — full example
{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}
{\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
\landscape
\paperw15840\paperh12240\margl720\margr720\margt720\margb720
\tx720\tx1440\tx2880\tx5760
This line is the default color\line
\tab this line has 1 tab\line
\tab\tab this line has 2 tabs\line
\tab\tab\tab this line has 3 tabs\line
\tab\tab\tab\tab this line has 4 tabs\line
\cf2
\tab This line is red and has a tab before it\line
\cf1
\page
\par\pard\tx1440\tx2880
This line is the default color and the first line on page 2\line
\tab\tab This is the second tab on the second line on the second page\line
\page
\par\pard
This is the third page with formatting examples\line
\fs30 First line with 15 point text\line
\fs20 Second line with 10 point text\line
\i Italics on \i0 Italics off\line
\b Bold on \b0 Bold off\line
\scaps Small Caps On \scaps0 Small Caps Off\line
\strike Strikethrough on \strike0 Strike off\line
\caps All Capitals On \caps0 All Capitals Off\line
\outl Outline on \outl0 Outline off\line
\ul Underline on \ul0 Underline off\line
\uldb Double underline on \ul0 off\line
\ulth Thick underline on \ul0 off\line
\ulw Words-only underline on \ul0 off\line
\ulwave Wave underline on \ul0 off\line
\uld Dotted underline on \ul0 off\line
\uldash Dash underline on \ul0 off\line
\uldashd Dot-dash underline on \ul0 off\line
}
Download the example .rtf file to open it directly in a word processor.
Reference & next steps