0% found this document useful (0 votes)
26 views10 pages

Rich Text Format (Tutorial)

Uploaded by

Osvaldo Carvalho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Rich Text Format (Tutorial)

Uploaded by

Osvaldo Carvalho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf1.

html

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;}}

1 of 4 15/09/2024, 10:08
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf1.html

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

2 of 4 15/09/2024, 10:08
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf1.html

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

3 of 4 15/09/2024, 10:08
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf1.html

Click here to open a file with the above code.

Reference sites:

Rich Text Format (RTF) Version 1.5 Specification

Home Page

4 of 4 15/09/2024, 10:08
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf2.html

APPLICATION INTERCHANGE FILE FORMATS


Simple tutorial for programmers

RTF - Rich Text Format (.rtf extension) - working with fonts and tabs

This builds upon the basics in the first example. Fonts are straight forward, simply define them in the
beginning and reference them with as needed. In the example which follows I use the courier font and
Profile as the second font.

{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}{\f1 ProFontWindows;}}


This line is font 0 which is courier\line
\f1
This line is font 1\line
\f0
This line is font 0 again\line
}

There are a number of attributes which can be assigned to the text, such as text color and highlight. The
color table determines which color is applied. In the basics section I defined the red color. For this page I
will add yellow which I like to use to highlight text:

{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}{\f1 ProFontWindows;}}


{\colortbl;\red0\green0\blue0;\red255\green0\blue0;\red255\green255\blue0;}
This line is font 0 which is courier\line
\f1
This line is font 1\line
\f0
This line is font 0 again\line
This line has a \cf2 red \cf1 word\line
\highlight3 while this line has a \cf2 red \cf1 word and is highlighted in yellow\highlight0\line
Finally, back to the default color.\line
}

Tabs have lots of characteristics, such as justification and leading characters. In the following example I
put a tab stop at 3" and orient the text as noted in the code:

{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}


\tqr\tx4320\tab Right tab\par\pard
\tqc\tx4320\tab Center tab\par\pard
\tx4320\tab Left tab
}

What I did was make each line a new paragraph and define the tabs accordingly. A decimal tab is \tqdec.
Leading characters from the beginning of the line or preceeding tab use the following codes:

{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier;}}


\fs28
\tqr\tldot\tx7200\tab Right tab with leading dots\par\pard
\tqr\tlhyph\tx7200\tab Right tab with leading hyphens\par\pard
\tqr\tlul\tx7200\tab Right tab with leading underline\par\pard

1 of 2 15/09/2024, 10:12
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf2.html

\fs18 \tqr\tx1440\tab 1" tab\tldot\tx2880\tab with 1" of dots between\par\pard


\tqr\tlhyph\tx1440\tab 1" tab\tx2880\tab with 1" of space between and hypens outside\tqr\tlhyph\tx8640
\tab\par\pard
}

Reference sites:

Home Page

2 of 2 15/09/2024, 10:12
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf3.html

APPLICATION INTERCHANGE FILE FORMATS


Simple tutorial for programmers

RTF - Rich Text Format (.rtf extension) - working with tables

This builds upon the basics in the first example. The desired database output required three columns, the
first of which had the question, the second column had the current period comments and the third column
had last period comments. Both comments were free form text. The rows represented different questions
to which the comments applied.

As you will have observed from the prior examples RTF used a measurement system called twips which
are defined in 1/20 of a point, where 72 points are an inch; therefore 1440 twips are one inch (72 times
20). In the following examples I build a table by defining sequential rows. Each row begins with \trowd
and ends with \row. Within a row are cells which end with \cell.

The following will generate a table of one row and three columns. The right side of first cell is at 1,000
twips; the second cell at 2,000 twips and the third at 3,000 twips.

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

Of course, this could also be done with tabs. Unlike a tab, the application will increase the row height to
accomodate the text.

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

and you can add boarders to the top, bottom, left and right of the cells

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

1 of 4 15/09/2024, 10:07
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf3.html

\cellx3000
cell 1\intbl\cell
lots of text in cell two\intbl\cell
cell 3\intbl\cell
\row
}

Since I don't want the text right next to the border I move it in 144 twips (1/10")

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

adding a second row is straight forward

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

Instead of \brdrs for a single line border you can use the following border types (note that I used \par and
\pard to break the rows):

{\rtf1\ansi\deff0
Below are the border types\line\par

2 of 4 15/09/2024, 10:07
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf3.html

\trowd\trgaph144
\clbrdrt\brdrs\clbrdrl\brdrs\clbrdrb\brdrs\clbrdrr\brdrs
\cellx5000
Single border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrdot\clbrdrl\brdrdot\clbrdrb\brdrdot\clbrdrr\brdrdot
\cellx5000
Dotted border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrdb\clbrdrl\brdrdb\clbrdrb\brdrdb\clbrdrr\brdrdb
\cellx5000
Double thickness border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrdash\clbrdrl\brdrdash\clbrdrb\brdrdash\clbrdrr\brdrdash
\cellx5000
Dashed border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrdashsm\clbrdrl\brdrdashsm\clbrdrb\brdrdashsm\clbrdrr\brdrdashsm
\cellx5000
Small dashed border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrdashd\clbrdrl\brdrdashd\clbrdrb\brdrdashd\clbrdrr\brdrdashd
\cellx5000
Dot dash border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrdashdd\clbrdrl\brdrdashdd\clbrdrb\brdrdashdd\clbrdrr\brdrdashdd
\cellx5000
Dot dot dash border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrtriple\clbrdrl\brdrtriple\clbrdrb\brdrtriple\clbrdrr\brdrtriple
\cellx5000
Triple border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrtnthlg\clbrdrl\brdrtnthlg\clbrdrb\brdrthtnlg\clbrdrr\brdrthtnlg
\cellx5000
Thick thin border (large)\intbl\cell
\row\pard\par

3 of 4 15/09/2024, 10:07
RTF - Rich Text Format (tutorial) http://www.pindari.com/rtf3.html

\trowd\trgaph144
\clbrdrt\brdrtnthlg\clbrdrl\brdrtnthlg\clbrdrb\brdrthtnlg\clbrdrr\brdrthtnlg
\cellx5000
Thin thick border (large)\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrtnthtnlg\clbrdrl\brdrtnthtnlg\clbrdrb\brdrtnthtnlg\clbrdrr\brdrtnthtnlg
\cellx5000
Thin thick thin border (large)\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrwavy\clbrdrl\brdrwavy\clbrdrb\brdrwavy\clbrdrr\brdrwavy
\cellx5000
Wavy border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrdashdotstr\clbrdrl\brdrdashdotstr\clbrdrb\brdrdashdotstr\clbrdrr\brdrdashdotstr
\cellx5000
Striped border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdremboss\clbrdrl\brdremboss\clbrdrb\brdrengrave\clbrdrr\brdrengrave
\cellx5000
Emboss border\intbl\cell
\row\pard\par

\trowd\trgaph144
\clbrdrt\brdrengrave\clbrdrl\brdrengrave\clbrdrb\brdremboss\clbrdrr\brdremboss
\cellx5000
Engrave border\intbl\cell
\row\pard\par

End of border types.


}

Click here to open a file with the above code.

Reference sites:

Home Page

4 of 4 15/09/2024, 10:07

You might also like