How can I add titles of chapters in ColumnText?
Why can I only add a Chapter to the document body and not add a Chapter to create a TOC?
I need to make a PDF like this:
And then add TOC to this document. I create the document using ColumnText
and PdfPTable
objects, but I can't add a Chapter
to create a TOC. I can only add a Chapter
to the document body, but in that case, the title of the Chapter
isn't shown in the ColumnText
.
Posted on StackOverflow on Nov 23, 2012 by Asator
Your question isn't clear in the sense that you don't tell us if you want a TOC like this:
Option 1
If this is the case, you are using the wrong terminology, as what you see in the Bookmarks panel can be referred to as Outlines or bookmarks.
If you say you want a TOC, you want something like this:
Option 2
I mention both, because you talk about the Chapter
(a class you should no longer use) and that class creates bookmarks/outlines, not a TOC.
I have create a PDF file that has both, bookmarks and a TOC: columns_with_toc.pdf. Please take a look at the CreateTOCinColumn example to find out how it's done.
The result looks like this:
Columns with titles and tables
Take a look at these lines:
String title = String.format("Numbers from %s to %s", start, end);
Text c = new Text(title);
TOCTextRenderer renderer = new TOCTextRenderer(c);
renderer.setRoot(root);
c.setNextRenderer(renderer);
We declare a text renderer here. TOCTextRenderer
class looks like this:
protected class TOCTextRenderer extends TextRenderer {
protected PdfOutline root;
public TOCTextRenderer(Text modelElement) {
super(modelElement);
}
public void setRoot(PdfOutline root) {
this.root = root;
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
Rectangle rect = getOccupiedAreaBBox();
PdfDestination dest = PdfExplicitDestination.createXYZ(drawContext.getDocument().getLastPage(),
rect.getLeft(), rect.getTop(), 0);
list.add(new TOCEntry(((Text) modelElement).getText(), dest));
PdfOutline curOutline = root.addOutline(((Text) modelElement).getText());
curOutline.addDestination(dest);
}
}
As you can see, we create a PdfDestination
based on the position of the title:
PdfDestination dest = PdfExplicitDestination.createXYZ(drawContext.getDocument().getLastPage(), rect.getLeft(), rect.getTop(), 0);
If you want bookmarks, you can create a PdfOutline
like this:
PdfOutline curOutline = root.addOutline(((Text) modelElement).getText());
curOutline.addDestination(dest);
If you want a TOC, you can store a TOCEntry
in a List
:
list.add(new TOCEntry(((Text) modelElement).getText(), dest));
So we create a renderer object, pass the root of the outline tree to it and assign it to every title. The bookmarks will be created automatically, the TOC won't. If you want to add the TOC, you need something like this:
for (TOCEntry entry : list) {
Link c = new Link(entry.title, entry.dest);
doc.add(new Paragraph(c));
}
You now have a list of titles which you can click to jump to the corresponding table.
Click How can I add titles of chapters in ColumnText? if you want to see how to answer this question in iText 5.