itext7获取table高度

itext7与itext5有一定的区别

i7是通过虚拟地布局元素,来了解它占用了多少空间。

Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();

        for (int i = 0; i < 10; i++) {
            Cell cell;

            if (i == 9) {
                cell = new Cell().add(new Paragraph("Two\nLines"));
            } else {
                cell = new Cell().add(new Paragraph(Integer.toString(i)));
            }

            table.addCell(cell);
        }

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        // Fictitiously layout the element to find out, how much space it takes
        IRenderer tableRenderer = table.createRendererSubTree().setParent(doc.getRenderer());
        LayoutResult tableLayoutResult = tableRenderer.layout(new LayoutContext(
                new LayoutArea(0, new Rectangle(550 + 72, 1000))));

        pdfDoc.setDefaultPageSize(new PageSize(550 + 72,
                tableLayoutResult.getOccupiedArea().getBBox().getHeight() + 72));

        doc.add(table);

        doc.close();
tableLayoutResult.getOccupiedArea().getBBox().getHeight()就可以获取高度
posted @ 2022-09-03 17:51  jiduoduo  阅读(820)  评论(0编辑  收藏  举报