APPLICATION INTERCHANGE FILE FORMATS
Simple tutorial for programmers

RTF - Rich Text Format (.rtf extension)

My starting point was a rather detailed site, but my requirement was to create an output file with some lines highligted in a different color, such as:

This line is the default color
This line is red
This line is the default color

With the default setting my Word application created an RTF file which was 2,460 bytes and contained coding which isn't relevant to the primary task of producing this output. After some experimenting I was able to produce the same output with the following RTF code when the text was saved in a file with an .rtf extension:

{\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 that any space before the \ symbol is included in the text of the document, so exclude them. To include the \ symbol in your document you use \\

To specify the default font add {\fonttbl {\f0 Courier;}} to the first line, where Courier is the font name. When printing program listing I prefer to use the ProFont typeface since it differentiates between a zero and O. I also like the Andale Mono font since it uses a dot in the middle of a zero rather than a line.

A couple of commands I found useful: \tab for the tab character (more on tabs in the next section) and \page for a page break

The code will now read as follows:

{\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
}

I then defined each tab stops with with \tx? (where ? is a value in which 1440 is 1 inch). I prefer setting my own tabs and for my application I needed them at 0.5" 1" 2" and 4". Our code now reads as follows:

{\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
}

To set the page orinetation define the page size with \paperw15840\paperh12240 for a 11" wide and 8.5" high page and set landscape mode with \landscape
I then set the page margins (say 0.5" all around) with \margl720\margr720\margt720\margb720.

{\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
}

As it turns out I needed different tab sections for the second and third parts of my report. To accomplish this I simply started a new paragraph and redefined the tab stops with \par\pard\tx1440\tx2880 which gave me a tab stop at 1" and 2".

{\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
}

Other useful formatting commands include:
•	\fs? for font size (where ? is twice the font size such that \fs24 results in a 12 point font) 
•	\i to turn italics on and \i0 to turn italics off 
•	\b to turn bold on and \b0 to turn bold off 
•	\scaps to turn small caps on and \scaps to turn small caps off 
•	\strike to turn strike through on and \strike0 to turn strike through off 
•	\caps to turn all capitals on and \caps0 to turn all capitals off 
•	\outl to turn outline on and \outl0 to turn outline off 
There are numerous underline options (all of which are turned off with \u0). Rather than list them I have made them self explanatory in the code below

{\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 test\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 Stike through 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 Double Underline Off\line
\ulth Thick Underline on \ul0 Thick Underline Off\line
\ulw Underline words only on \ul0 Underline words only off\line
\ulwave Wave Underline on \ul0 Wave underline off\line
\uld Dotted Underline on \ul0 Dotted underline off\line
\uldash Dash Underline on \ul0 Dash underline off\line
\uldashd Dot Dash Underline on \ul0 Dot Dash underline off\line
}

Click here to open a file with the above code.

Rich Text Format (RTF) Version 1.5 Specification