如何将带有页码的目录添加到RTF

时间:2019-01-03 12:35:38

标签: java itext rtf tableofcontents

我已经成功生成了目录,但是如何添加相应的页码。我正在使用iText的RtfWriter2创建文档。

我尝试下面的代码添加目录,但没有运气。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document doc = new Document();
        RtfWriter2 writer2 = RtfWriter2.getInstance(doc, baos);
        try {
        writer2.setAutogenerateTOCEntries(true);
        Table headerTable = new Table(3);
        headerTable.addCell("Test Cell 1");
        headerTable.addCell("Test Cell 2");
        headerTable.addCell("Test Cell 3");
        HeaderFooter header = new RtfHeaderFooter(headerTable);
        RtfHeaderFooterGroup footer = new RtfHeaderFooterGroup();
        doc.open();

        Paragraph p = new Paragraph();
        p.add(new RtfTableOfContents("UPDATE ME!"));
        doc.add(p);

        p = new Paragraph("", new RtfFont("Staccato222 BT"));
        p.add(new Chunk("Hello! "));
        p.add(new Chunk("How do you do?"));
        doc.add(p);
        p.setAlignment(Element.ALIGN_RIGHT);
        doc.add(p);

        Chunk c = new Chunk("");
        c.setNewPage();
        doc.add(c);

        for (int i = 0; i < 300; i++) {
            doc.add(new Paragraph( "Dummy line to get multi-page document. Line " + (i + 1)));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
        doc.close();
        return baos;

下面是我想要的输出:

目录

History 1
Intro  2

0 个答案:

没有答案