How can I add titles of chapters in ColumnText?

Category: 
Tags: page eventsColumnTextPdfOutlineBookmarksiText 5

I need to make a PDF like this:

Example

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
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
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.

Just like you, I create a ColumnText object with titles and tables:

ColumnText ct = new ColumnText(writer.getDirectContent());
int start;
int end;
for (int i = 0; i <= 20; ) {
    start = (i * 10) + 1;
    i++;
    end = i * 10;
    String title = String.format("Numbers from %s to %s", start, end);
    Chunk c = new Chunk(title);
    c.setGenericTag(title);
    ct.addElement(c);
    ct.addElement(createTable(start, end));
}
int column = 0;
do {
    if (column == 3) {
        document.newPage();
        column = 0;
    }
    ct.setSimpleColumn(COLUMNS[column++]);
} while (ColumnText.hasMoreText(ct.go()));

The result looks like this:

Columns with titles and tables
Columns with titles and tables

In spite of the rules for posting a question on StackOverflow, you didn't post a code sample, but there is at least one difference between your code and mine:

c.setGenericTag(title);

In this line, we declare a generic tag. This tag is used by the TOCEntry class that looks like this:

public class TOCCreation extends PdfPageEventHelper {

    protected PdfOutline root;
    protected List<TOCEntry> toc = new ArrayList<TOCEntry>();

    public TOCCreation() {
    }

    public void setRoot(PdfOutline root) {
        this.root = root;
    }

    public List<TOCEntry> getToc() {
        return toc;
    }

    @Override
    public void onGenericTag(
        PdfWriter writer, Document document, Rectangle rect, String text) {
        PdfDestination dest = new PdfDestination(
            PdfDestination.XYZ, rect.getLeft(), rect.getTop(), 0);
        new PdfOutline(root, dest, text);
        TOCEntry entry = new TOCEntry();
        entry.action = PdfAction.gotoLocalPage(
            writer.getPageNumber(), dest, writer);
        entry.title = text;
        toc.add(entry);
    }
}

As you can see, we create a PdfDestination based on the position of the title:

PdfDestination dest =
    new PdfDestination(PdfDestination.XYZ, rect.getLeft(), rect.getTop(), 0);

If you want bookmarks, you can create a PdfOutline like this:

new PdfOutline(root, dest, text);

If you want a TOC, you can store a String and a PdfAction in a List:

TOCEntry entry = new TOCEntry();
entry.action = PdfAction.gotoLocalPage(writer.getPageNumber(), dest, writer);
entry.title = text;
toc.add(entry);

Now that we understand the TOCCreation class, we take a look at how to use it:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
TOCCreation event = new TOCCreation();
writer.setPageEvent(event);
document.open();
event.setRoot(writer.getRootOutline())

We create an event object, pass it to the writer and after we've opened the document, we pass the root of the outline tree to the event. The bookmarks will be created automatically, the TOC won't. If you want to add the TOC, you need something like this:

document.newPage();
for (TOCEntry entry : event.getToc()) {
    Chunk c = new Chunk(entry.title);
    c.setAction(entry.action);
    document.add(new Paragraph(c));
}

You now have a list of titles which you can click to jump to the corresponding table.