zl程序教程

您现在的位置是:首页 >  其它

当前栏目

itext7获取table高度

获取 Table 高度
2023-09-27 14:21:58 时间

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()就可以获取高度