How can I add titles of chapters in ColumnText?

前端 未结 1 814
不知归路
不知归路 2020-12-11 14:09

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

    |    ColumnText column1   |    ColumnText column2   |
    | PdfP         


        
相关标签:
1条回答
  • 2020-12-11 14:50

    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.

    0 讨论(0)
提交回复
热议问题