iText Tutorial
|
|
iText, a Free Java-PDF library
by Bruno Lowagie
|
[Home] |
[Previous] |
[TOC] |
[Next] |
[PDF] |
Part I: Simple iText
Chapter 4: HeaderFooters, Chapters, Sections and the Graphic-object
A lot of 'Simple iText'-objects described in Chapters 3 to 5 can be used to avoid the more difficult 'Advanced' stuff (chapters 9 to 12).
Always keep in mind that the functionality of these 'simple' objects is limited. For really complex functionality, please read Part III of this tutorial.
|
HeaderFooter
The HeaderFooter-object is an object that can be used to add a simple header or footer
to each page of the document. Such a header or footer contains a standard phrase and (if needed) the current
page number. If you need a more complex header or footer (with a table, or with 'page X of Y'), please read
Chapter 12.
In example 1 you see that we first add
a footer containing a page number and without any borders:
HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
footer.setBorder(Rectangle.NO_BORDER);
document.setFooter(footer);
We could also have used this constructor:
HeaderFooter footer = new HeaderFooter(new Phrase("This is page "), new Phrase("."));
This constructor knows you want to add a pagenumber and puts it between the two phrases.
If you just set a HeaderFooter-object without changing the borders,
the header or footer has a line above and below the text:
HeaderFooter header = new HeaderFooter(new Phrase("This is a header without a page number"), false);
document.setHeader(header);
You can see this in Chap0401.pdf.
|
Chapters and Sections
Chapter 11 describes how you can construct an outline tree.
If you only need a simple tree with some chapters and (sub)sections, you can automatically
build such a tree using the Chapter and Section-object.
Paragraph cTitle = new Paragraph("This is chapter 1", chapterFont);
Chapter chapter = new Chapter(cTitle, 1);
Paragraph sTitle = new Paragraph("This is section 1 in chapter 1", sectionFont);
Section section = chapter.addSection(sTitle, 1);
In example 2, we add a series of
chapters and subsections and if you look at Chap0402.pdf,
you see a complete outline tree. This outline tree is open by default. If you want some parts of the outline closed,
you have to use the method setBookmarkOpen(boolean open) with false as parameter.
This is done in example 3
(Chap0403.pdf).
|
Graphic
If you want to add graphics, such as lines, circles, geometrical forms, you can read
Chapter 10, but if you only need very limited functionality,
you can also use the Graphic-object.
Graphic grx = new Graphic();
// add a rectangle
grx.rectangle(100, 700, 100, 100);
// add the diagonal
grx.moveTo(100, 700);
grx.lineTo(200, 800);
// stroke the lines
grx.stroke();
document.add(grx);
This is demonstrated in example 4
(Chap0404.pdf).
For a complete overview of all the methods, please consult the
API of class
PdfContentByte of which the Graphic-object is derived.
The Graphic-object can also be useful if you want to draw a border around
your page or if you want to insert a horizontal line at the current position in the text.
The following methods draw a border with a certain width, a certain spacing and (if needed) a certain color:
public void setBorder(float linewidth, float extraSpace);
public void setBorder(float linewidth, float extraSpace, Color color);
These methods draw a horizontal line with a certain width and (if needed) a certain color.
The length of the line is a certain percentage of the available space between the
margins. The line is always centered:
public void setHorizontalLine(float linewidth, float percentage)
public void setHorizontalLine(float linewidth, float percentage, Color color)
Example 5
(Chap0405.pdf)
has a border of 3 points, 5 points inside the margins. There are also two horizontal
lines, one in black of 5 points that takes 100% of the available space, another in
red of 3 points that takes only 80% of the available space.
|
[Top] |
[Previous] |
[TOC] |
[Next] |
[PDF] |
Page Updated: $Date: 2003/06/25 07:36:34 $
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
|
|