Chapter 3. Example Programs
So far, this book has approached RTF as a format that means something in and of itself. But RTF, like any document format, makes sense only in terms of how it is used—namely, how it is formatted and read. The issue of parsing RTF is larger than can be dealt with in this brief guide, but these simple RTF-generating programs demonstrate the basic issues involved in having programs that write RTF documents. The examples are of simple utilities that each use some particular features of RTF.
The programs are all in relatively simple Perl, with an explanation before each, and comments in the code. If you want to generate your own RTF documents from Perl, have a look at the helpful RTF-writing modules in CPAN (located at http://search.cpan.org), notably RTF::Writer and RTF::Generator. I don’t actually use these modules in the following examples, because the examples simply demonstrate RTF without detouring through the APIs of the RTF-writing modules.
A Datebook Generator
This program starts a new RTF file, writes an RTF prolog, writes a series of paragraphs (one at a time, in each pass through the while
loop), and then closes the document with a single } character. Each paragraph that it writes is a heading for each day’s worth of space in a datebook (which can be printed out and bound, so you have someplace to pencil in notes about upcoming appointments, deadlines, brunches, court dates, box socials, and the like). You can get a blank space between the paragraphs ...
Get RTF Pocket Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.