Tutorial: iText by Example

Rich Text Format

The RTF Package:
The RTF package is an extension of the iText library and allows iText to output Rich Text Files in additon to PDF files. These files can then be viewed and edited with RTF viewers such as Microsoft Word or OpenOffice.org Writer.
The main class for generating RTF documents is the RtfWriter2. It supports most of iText's features with the exception of those features listed under the unsupported features. In addition to this there are a number of extensions to the core iText classes that provide access to RTF specific functionality:

Go to top of the page
Introduction to RTF:
RTF is the Rich Text Format developed by Microsoft with the aim of providing a cross-platform text-based document format. The main advantage that it has over PDF is that it can easily be edited with most word processing software. Its biggest disadvantage is that it is rendered by the word processing software when the RTF document is opened and not when the RTF document is generated. This leads to the situation that the document can look slightly different on different platforms.

It is important to remember that RTF is a document description format and when iText creates a RTF document, only the description of the document is generated. NO RENDERING TAKES PLACE. It is thus impossible to provide access to such things as page numbers or page events.

Apart from these considerations creating an RTF document works just like creating any other document with iText.
Go to top of the page
Creating a basic RTF document:
Creating a basic RTF document works just like creating a basic PDF document:
Document document = new Document();
RtfWriter2.getInstance(document,
	new FileOutputStream("testRTFdocument.rtf"));
document.open();
document.add(new Paragraph("Hello World!"));
document.close();
          
Example: java com.lowagie.examples.rtf.HelloWorld
Generates a simple 'Hello World!' RTF file: see HelloWorld.rtf
You can then use the objects described in Using High Level Objects to create more complex RTF documents.
Go to top of the page
Unsupported iText features:
A number of iText features are not supported in the RtfWriter2 due to the fact that they either have not been implemented or are not supported by the RTF format itself. The following list may not be complete, since the development of the RtfWriter2 sometimes lags behind the development of the core library.
Images other than JPEG, PNG, BMP, GIF and WMF
Only these image types are supported natively in the RTF format.
Rotated images
Nested tables
Embedded fonts
Lists with non-bullet symbols
Support may be added on demand.
Table.setSpaceInsideCell and Table.setSpaceBetweenCells
Table.setSpaceBefore
Will be supported in the future.
Table.padding
Watermarks
Encryption
Viewer preferences
These only apply to the Adobe Acrobat Viewer.
Image borders
Forms
Go to top of the page
Transitioning from the RtfWriter to the RtfWriter2:
IF YOU HAVEN'T TRANSITIONED YET, NOW IS THE TIME. THE RTFWRITER WILL BE REMOVED END OF 2007

The RtfWriter is the old RTF generating class. It has been deprecated and superceded by the RtfWriter2 which is now the only class that should be used for RTF generation. The RtfWriter is now only provided for backward compatability and will be removed from the library by the end of 2007. The RtfWriter receives NO UPDATES.

Transitioning from the old RtfWriter to the new RtfWriter2 should by no problem if you do not use any of the extensions provided for the old RtfWriter. If you did use extensions for the old RtfWriter the following table shows which classes provide the equivalent functionality in the new RtfWriter2:
Old extensions New extensions
com.lowagie.text.rtf.RtfFont com.lowagie.text.rtf.style.RtfFont
com.lowgaie.text.rtf.HeaderFooter com.lowagie.text.rtf.headerfooter.RtfHeaderFooter
com.lowagie.text.rtf.HeaderFooters com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup
com.lowagie.text.rtf.RtfPageNumber com.lowagie.text.rtf.field.RtfPageNumber
com.lowagie.text.rtf.RtfTOC com.lowagie.text.rtf.field.RtfTableOfContents
com.lowagie.text.rtf.RtfTOCEntry com.lowagie.text.rtf.field.RtfTOCEntry
com.lowagie.text.rtf.RtfTableCell com.lowagie.text.rtf.table.RtfCell
Go to top of the page

iText, the #1 Java-PDF library




Amazon books:
amazon.co.uk-link