Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

itext - How can I add titles of chapters in ColumnText?

Please, how i can add titles of the Chapters in ColumnText? I need make PDF like this:

    |    ColumnText column1   |    ColumnText column2   |
    | PdfPTable with content  |  PdfPTable with content |
    |                         |      Chapter 2 title    |
    |     Chapter 1 title     |                         |

And then add TOC to this document. I make document with ColumnText and table in it. But can't add Chapter in table. I can add Chapter only to the document body, but in this case title of Chapter not in ColumnText.

Image of one page of the result document here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your question isn't clear in the sense that you don't tell us if you want a TOC like this:

enter image description here

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:

enter image description here

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:

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...