iText Tutorial SourceForge.net Logo iText, a Free Java-PDF library
by
Bruno Lowagie
[Home] [Previous] [TOC] [Next] [PDF]

Part II: Other document formats

Chapter 8: RTF
The RTF package
The RTF package is an extension to the base iText package and allows iText to output Rich Text files in addition to PDF files. This package was written by Mark Hall.

Most of the PDF features are available but there are some features which are not supported in the RTF package.


Generating a RTF document
You generate RTF documents the same way you generate PDF documents. The 5 basic steps are the same. The only change is that you use a RtfWriter instead of the usual PdfWriter in Step 2.
Download the source code for the Hello World example: Chap0801.java

Step 1:
Creates an instance of the com.lowagie.text.Document-object:
Document document = new Document();

Step 2:
Creates a RtfWriter that listens to this document and writes the document to the OutputStream of your choice:
RtfWriter.getInstance(document, new FileOutputStream("Chap0801.rtf"));

Step 3:
Opens the document:
document.open();

Step 4:
Adds content to the document:
document.add(new Paragraph("Hello World"));

Step 5:
Closes the document:
document.close();

Check the result here: Chap0801.rtf.

For help on how to create different objects and adding them to the document please look at the other sections of this tutorial.


Unsupported Features
These features are not supported by the RTF package. Everything else should work.
  • Watermarks
  • Viewer preferences
  • Encryption
  • Embedded fonts
  • Phrases with a leading
  • Lists with non-bullet symbols
  • Nested tables
  • Images other than JPEG and PNG
  • Rotated images


Extended Headers and Footers in RTF
With the RTF write it is not possible to change the header or footer for the next page just by calling setHeader before calling newPage(). There are two ways for getting around this problem:
  • Using Chapters. You can use different headers or footers with each chapter, by calling setHeader or setFooter before adding the chapter to the document. See Chap0802.java for an example.
  • Using the RtfHeaderFooters class. This class allows you to set 4 headers or footers and to specify on which pages they appear. You can of course combine this class with Chapters to create up to 4 different headers or footers per chapter. See Chap0803.java for an example.

Using the RtfHeaderFooters class

Step 1:
Create a new RtfHeaderFooters object
RtfHeaderFooters headers = new RtfHeaderFooters();

Step 2:
Add HeaderFooter objects
headers.add(RtfHeaderFooters.LEFT_PAGES, new HeaderFooter(new Phrase("This header is only on left hand pages")));
headers.add(RtfHeaderFooters.RIGHT_PAGES, new HeaderFooter(new Phrase("This header is only on right hand pages")));

Step 3:
Use the RtfHeaderFooters object as your header or footer
document.setHeader(headers);

Constants to use with the method RtfHeaderFooters.add(...)
  • FIRST_PAGE: Use this to have a special header or footer on the first page of you document. You will have to call rtfWriter.setHasTitlePage(true) for this to work.
  • LEFT_PAGES: Use this to have a header or footer on all left side pages.
  • RIGHT_PAGES: Use this to have a header or footer on all right side pages.
  • ALL_PAGES: Use this to have a header or footer an all pages. This only makes sense together with FIRST_PAGE

One thing that is important is that if you use LEFT_PAGES or RIGHT_PAGES with either the header or footer then ALL_PAGES will not work for both header and footer.

Examples of using the RtfWriter
  • Example 1 the Hello World example
  • Example 2 has an example of using multiple different headers.
  • Example 3 has an example of using the RtfHeaderFooters class.
  • Example 4 has a simple table

[Top] [Previous] [TOC] [Next] [PDF]
Page Updated: $Date: 2003/06/25 07:36:35 $
Copyright © 2000, 2001 by Bruno Lowagie
Adolf Baeyensstraat 121, 9040 Gent, BELGIUM,
tel +00 32 92 28 10 97 mailto:itext-questions@lists.sourceforge.net