有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

itext如何在ColumnText中添加章节标题?

请问,我如何才能在专栏文本中添加章节标题? 我需要这样制作PDF:

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

然后将TOC添加到此文档中。 我用列文本和表格制作文档。但无法在表中添加章节。 我只能将章节添加到文档正文中,但在本例中,章节标题不在ColumnText中

Image of one page of the result document here


共 (1) 个答案

  1. # 1 楼答案

    你的问题并不清楚,因为你没有告诉我们你是否想要这样的TOC:

    enter image description here

    如果是这种情况,则说明您使用了错误的术语,因为您在“书签”面板中看到的内容可能被称为大纲或书签

    如果你说你想要TOC,你想要这样的东西:

    enter image description here

    我提到这两个,因为您谈到Chapter(一个您不应该再使用的类),该类创建书签/大纲,而不是TOC

    我已经创建了一个PDF文件,其中既有书签,也有TOC:columns_with_toc.pdf。请看一下CreateTOCinColumn示例,了解它是如何完成的

    与您一样,我创建了一个带有标题和表格的ColumnText对象:

    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()));
    

    结果如下所示:

    enter image description here

    尽管有在StackOverflow上发布问题的规则,但您没有发布代码示例,但您的代码和我的代码至少有一个区别:

    c.setGenericTag(title);
    

    在这一行中,我们声明一个泛型标记。该标记由TOCEntry类使用,如下所示:

    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);
        }
    }
    

    如您所见,我们根据标题的位置创建一个PdfDestination

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

    如果需要书签,可以创建如下PdfOutline

    new PdfOutline(root, dest, text);
    

    如果需要TOC,可以将StringPdfAction存储在List中:

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

    现在我们已经了解了TOCCreation类,我们来看看如何使用它:

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

    我们创建一个event对象,将其传递给writer,打开文档后,将大纲树的根传递给事件。书签将自动创建,TOC不会。如果要添加TOC,您需要以下内容:

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

    现在您有了一个标题列表,可以单击该列表跳转到相应的表