ITEXT7表格无边框(无边框)

The*_*bie 0 java itext itext7

下面的代码不起作用。

 Table table = new Table(2); 
 table.setBorder(Border.NO_BORDER);
Run Code Online (Sandbox Code Playgroud)

我是itext7的新手,我想要做的就是让我的桌子无边界。喜欢怎么做?

Sam*_*eck 6

默认情况下,表格本身不负责iText7中的边框,而单元格则负责。如果要使用无边界表,则需要将每个单元格设置为无边界(或者,如果仍要在内部边界内,请将外部单元格的边缘设置为无边界)。

Cell cell = new Cell();
cell.add("contents go here");
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
Run Code Online (Sandbox Code Playgroud)

  • 根据 [iText 7.1.6](http://itextsupport.com/apidocs/itext7/7.1.6/com/itextpdf/layout/element/Cell.html),您不能向 `Cell` 添加 `String`不再,只有`IBlockElement` 或`Image`。 (3认同)

小智 6

您可以编写一个方法来运行表的所有子项并设置 NO_BORDER。

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

这为您提供了仍然可以使用的优势

table.add("whatever");
table.add("whatever");
RemoveBorder(table);
Run Code Online (Sandbox Code Playgroud)

而不是在所有单元格手册上更改它。

  • 很方便。我建议使用以下方法提高类型安全性:`table.GetChildren().OfType<Cell>()` (2认同)