Tutorial: iText by Example

Extended header/footer support

Extended header/footer support:
Due to the limitations of the RTF format it is not possible to create more complex headers and footers using page events as can be done when generating PDF documents. To make it possible to create more complex headers and footers than with the basic HeaderFooter the RTF package provides three extensions: Of course these techniques can be combined to create different complex headers on different pages in different chapters :-).
Go to top of the page
Creating more complex headers and footers:
The first step is to create more complex headers and footers using the RtfHeaderFooter. The RtfHeaderFooter class provides two public constructors that take either a single Element or an array of Elements. Using these constructors you can create a header with more than one Paragraph
// Create the Paragraphs that will be used in the header.
Paragraph date = new Paragraph("01.01.2010");
date.setAlignment(Paragraph.ALIGN_RIGHT);
Paragraph address = new Paragraph("TheFirm\nTheRoad 24, TheCity\n" +
    "+00 99 11 22 33 44");

// Create the RtfHeaderFooter with an array containing the Paragraphs to add
RtfHeaderFooter header = new RtfHeaderFooter(new Element[]{date, address});
            
// Set the header
document.setHeader(header);
		  
another frequently used possibility is to use a Table to create a header or footer with multiple columns
// Create the table that will be used as the footer
Table footer = new Table(2);
footer.setBorder(0);
footer.setDefaultCellBorder(0);
footer.setWidth(100);
footer.addCell(new Cell("(c) Mark Hall"));
Paragraph pageNumber = new Paragraph("Page ");
            
// The RtfPageNumber is an RTF specific element that adds a page number field
pageNumber.add(new RtfPageNumber());
pageNumber.setAlignment(Paragraph.ALIGN_RIGHT);
footer.addCell(new Cell(pageNumber));
            
// Create the RtfHeaderFooter and set it as the footer to use
document.setFooter(new RtfHeaderFooter(footer));
		  
Example: java com.lowagie.examples.rtf.extensions.hf.ExtendedHeaderFooter
Generates an RTF document with more complex headers and footers: see ExtendedHeaderFooter.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page
Creating different headers and footers on differnet pages:
In addition to creating more complex headers and footers it is also possible to define multiple headers or footers that are displayed on different pages. This approach uses the RtfHeaderFooterGroup and allows for the following positions of headers or footers
  • RtfHeaderFooter.DISPLAY_ALL_PAGES
  • RtfHeaderFooter.DISPLAY_FIRST_PAGE
  • RtfHeaderFooter.DISPLAY_LEFT_PAGES
  • RtfHeaderFooter.DISPLAY_RIGHT_PAGES
DISPLAY_ALL_PAGES can only be used by itself, DISPLAY_FIRST_PAGE, DISPLAY_LEFT_PAGES and DISPLAY_RIGHT_PAGES can be combined to create different headers or footers on the respective pages.
// Create the RtfHeaderGroup for the footer and set the footers
// at the desired positions
RtfHeaderFooterGroup footer = new RtfHeaderFooterGroup();
footer.setHeaderFooter(new RtfHeaderFooter(titleFooter), RtfHeaderFooter.DISPLAY_FIRST_PAGE);
footer.setHeaderFooter(new RtfHeaderFooter(leftFooter), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
footer.setHeaderFooter(new RtfHeaderFooter(rightFooter), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
            
// Set the document footer
document.setFooter(footer);
		  
It is important to note that if you are creating a footer with different footers on different pages and you want to have the header on all pages except the first page, then you have to set this manually
// Create the RtfHeaderFooterGroup for the header.
// To display the same header on both pages, but not the
// title page set them to left and right pages explicitly.
RtfHeaderFooterGroup header = new RtfHeaderFooterGroup();
header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_LEFT_PAGES);
header.setHeaderFooter(new RtfHeaderFooter(date), RtfHeaderFooter.DISPLAY_RIGHT_PAGES);
          
This is not necessary if you want it to appear on all pages, then you can use DISPLAY_ALL_PAGES.

As the example shows you can combine the RtfHeaderFooterGroup with the RtfHeaderFooter to create complex headers or footers on different pages.
Example: java com.lowagie.examples.rtf.extensions.hf.MultipleHeaderFooter
Generates an RTF document with different headers/footers on different pages: see MultipleHeaderFooter.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page
Different headers per chapter:
The third method for displaying different headers or footers is to use Chapters and set different headers or footers per Chapter.

The headers and footers for the first Chapter have to be set before the document is opened.
// Create the header identifying the current chapter. The first
// chapter has to be set before the document is opened.
Paragraph header = new Paragraph("Chapter 1");
header.setAlignment(Element.ALIGN_CENTER);
document.setHeader(new RtfHeaderFooter(header));
            
// If the footer (or header) is to be the same for all Chapters
// then it has to be set before the document is opened and is
// then automatically set for all Chapters.
document.setFooter(new HeaderFooter(new Phrase("This is page "), new Phrase(".")));
		  
Then after adding each chapter optionally create and set the new headers or footers for the next Chapter.
// After adding the first chapter set the header for the second chapter.
header = new Paragraph("Chapter 2");
header.setAlignment(Element.ALIGN_CENTER);
document.setHeader(new RtfHeaderFooter(header));

Chapter chapter2 = new Chapter("Chapter 2", 2);
chapter2.add(new Paragraph("This is the content of chapter 2."));
document.add(chapter2);
		  
This process is then repeated for each additional Chapter to add. If you don't set a header or footer before adding a Chapter then the header and footer from the previous Chapter are used.
Example: java com.lowagie.examples.rtf.extensions.hf.ChapterHeaderFooter
Generates an RTF document with different headers/footers per chapter: see ChapterHeaderFooter.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page

iText, the #1 Java-PDF library




Amazon books:
amazon.co.uk-link