|
iTextSharp
Tutorial
|
|
iTextSharp, a Free C#-PDF library
|
| [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.cs
Step 1:
Creates an instance of the iTextSharp.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
FileStream("Chap0801.rtf"), FileMode.Create);
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
-
Paragraphs with a right indentation
-
Lists with a right indentation
-
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.cs for an example. Check the result here:
Chap0802.rtf
-
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.cs for an example.
Check the result here: Chap0803.rtf
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.Header = 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.HasTitlePage
= 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
|
| [Top] |
[Previous] |
[TOC] |
[Next] |
[PDF] |
Page Updated: $Date: 2003/02/25
13:52:19 $
Copyright © 2000, 2001 by Bruno Lowagie
|
Adolf Baeyensstraat 121, 9040 Gent, BELGIUM,
tel +00 32 92 28 10 97 mailto:itext@lowagie.com
|