iText 7无边框表格(无边框)

9

以下代码不起作用。

Table table = new Table(2); 
table.setBorder(Border.NO_BORDER);

我刚接触iText 7,我想让我的表格没有边框,应该怎么做呢?

3个回答

18

在iText7中,表格本身默认不负责边框,而是由单元格来控制。如果您想要一个无边框的表格,您需要将每个单元格都设置为无边框(或者在仍然希望有内部边框的情况下,将外部单元格设置为无边框)。

Cell cell = new Cell();
cell.add("contents go here");
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);

我尝试了这个,但是在 "cell.setBorder(Border.NO_BORDER)" 中出现了错误,提示找不到符号 "NO_BORDER"。 - The Newbie
啊,抱歉我的问题。导入问题。谢谢。 - The Newbie
3
根据iText 7.1.6的描述,现在不能再将一个String添加到Cell中,只能添加IBlockElement或者Image - Evgenij Reznik
1
使用C#的iText 7.1.10,您可以这样做:.table.AddCell(new Cell().Add(new Paragraph("内容").SetBorder(Border.NO_BORDER))); - alex

11

你可以编写一个方法,遍历表格的所有子元素并设置 NO_BORDER。

private static void RemoveBorder(Table table)
{
    for (IElement iElement : table.getChildren()) {
        ((Cell)iElement).setBorder(Border.NO_BORDER);
    }
}

这样做有一个好处,您仍然可以使用

table.add("whatever");
table.add("whatever");
RemoveBorder(table);

而不是手动更改所有单元格。


1
非常方便。我建议加强类型安全,改为: table.GetChildren().OfType<Cell>() - Metro Smurf

0
我在我的Java代码库中定义的一个实用函数如下:
private static void setInnerCellBorder(Table table, Border border) {
    for (IElement child : table.getChildren()) {
        if (child instanceof Cell) {
            ((Cell) child).setBorder(border);
        }
    }
}

如果你不想要边框,可以按照以下方式调用该函数:
setInnerCellBorder(table, Border.NO_BORDER);
// or if you want a different color size you can do 
setInnerCellBorder(Table, new SolidBorder(ColorConstants.RED, 2));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接