如何使用iText生成目录“ TOC”?

如何使用iText生成目录“ TOC”?,第1张

如何使用iText生成目录“ TOC”?

通过使用

PdfTemplate
s 可以实现。
PdfTemplate
s是一种可以稍后填充占位符。

来自Bruno的提示更新:

要在开始时在目录中生成,您需要为目录中的所有页码放置一些占位符。

PdfTemplate
你收集的那些
Map
。然后,将
Chapter
s
添加到文档中时,可以填充这些占位符。

此示例显示如何:

public class Main extends PdfPageEventHelper{    private final document      document;    private final PdfWriter     writer;    private final baseFont      baseFont       = baseFont.createFont();    private final Font          chapterFont    = FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL);    // table to store placeholder for all chapters and sections    private final Map<String, PdfTemplate> tocPlaceholder = new HashMap<>();    // store the chapters and sections with their title here.    private final Map<String, Integer>     pageByTitle    = new HashMap<>();    public static void main(final String[] args) throws Exception    {        final Main main = new Main();        main.document.add(new Paragraph("This is an example to generate a TOC."));        main.createTOC(10);        main.createChapters(10);        main.document.close();    }    public Main() throws Exception    {        this.document = new document(PageSize.A6);        this.writer = PdfWriter.getInstance(this.document, new FileOutputStream("text.pdf"));        this.writer.setPageEvent(this);        this.document.open();    }    @Override    public void onChapter(final PdfWriter writer, final document document, final float paragraphPosition, final Paragraph title)    {        this.pageByTitle.put(title.getContent(), writer.getPageNumber());    }    @Override    public void onSection(final PdfWriter writer, final document document, final float paragraphPosition, final int depth, final Paragraph title)    {        this.pageByTitle.put(title.getContent(), writer.getPageNumber());    }    private void createTOC(final int count) throws documentException    {        // add a small introduction chapter the shouldn't be counted.        final Chapter intro = new Chapter(new Paragraph("This is TOC ", this.chapterFont), 0);        intro.setNumberDepth(0);        this.document.add(intro);        for (int i = 1; i < count + 1; i++)        { // Write "Chapter i" final String title = "Chapter " + i; final Chunk chunk = new Chunk(title).setLocalGoto(title); this.document.add(new Paragraph(chunk)); // Add a placeholder for the page reference this.document.add(new VerticalPositionMark() {     @Override     public void draw(final PdfContentByte canvas, final float llx, final float lly, final float urx, final float ury, final float y)     {         final PdfTemplate createTemplate = canvas.createTemplate(50, 50);         Main.this.tocPlaceholder.put(title, createTemplate);         canvas.addTemplate(createTemplate, urx - 50, y);     } });        }    }    private void createChapters(final int count) throws documentException    {        for (int i = 1; i < count + 1; i++)        { // append the chapter final String title = "Chapter " + i; final Chunk chunk = new Chunk(title, this.chapterFont).setLocalDestination(title); final Chapter chapter = new Chapter(new Paragraph(chunk), i); chapter.setNumberDepth(0); chapter.addSection("Foobar1"); chapter.addSection("Foobar2"); this.document.add(chapter); // When we wrote the chapter, we now the pagenumber final PdfTemplate template = this.tocPlaceholder.get(title); template.beginText(); template.setFontAndSize(this.baseFont, 12); template.setTextMatrix(50 - this.baseFont.getWidthPoint(String.valueOf(this.writer.getPageNumber()), 12), 0); template.showText(String.valueOf(this.writer.getPageNumber())); template.endText();        }    }}

生成的PDF如下所示:
TableOfContents.pdf



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561270.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存