如何设置 and/or 检索 iText 7 中的默认单元格填充

How to set and/or retrieve default cell padding in iText 7

当您使用 Table 和单元格 类 在 iText 7 中创建 table 时,table 单元格默认内置了一些填充。据我通过查看生成的文档可以看出,它似乎是大约 2 个 PDF 单元。

有什么方法可以检索此值以用于计算?另外,有什么方法可以更改此默认值,以便我可以设置自己的填充以用于所有 table 中的所有单元格,而不必在每个单元格上单独设置它?

请查看 iText 7: Building Blocks 教程。

Before we start 部分,我们看到每个构建块都派生自名为 ElementPropertyContainer 的 class。这个 class 是属性的容器。

Cell class 的情况下,有一组定义填充的属性。您可以通过通用方式(使用 AbstractElement class 的方法)获得这些属性,如下所示:

System.out.println(cell.getProperty(Property.PADDING_LEFT));
System.out.println(cell.getProperty(Property.PADDING_RIGHT));
System.out.println(cell.getProperty(Property.PADDING_TOP));
System.out.println(cell.getProperty(Property.PADDING_BOTTOM));

但是,如果您也可以简单地使用 BlockElement class:

System.out.println(cell.getPaddingLeft());
System.out.println(cell.getPaddingRight());
System.out.println(cell.getPaddingTop());
System.out.println(cell.getPaddingBottom());

如您在教程中所见,Cell class 是 BlockElement class 的子 class。 BlockElementAbstractElement class 的子 class。 AbstractElement class 是 ElementPropertyContainer class 的子 class。

如果您想更改内边距(或边距,如果您愿意的话),请阅读 chapter 5 of that tutorial. It has an example, named CellMarginPadding:

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document document = new Document(pdf);
    Table table = new Table(new float[]{2, 1, 1});
    table.setBackgroundColor(Color.ORANGE);
    table.setWidthPercent(80);
    table.setHorizontalAlignment(HorizontalAlignment.CENTER);
    table.addCell(
        new Cell(1, 3).add("Cell with colspan 3")
            .setPadding(10).setMargin(5).setBackgroundColor(Color.GREEN));
    table.addCell(new Cell(2, 1).add("Cell with rowspan 2")
        .setMarginTop(5).setMarginBottom(5).setPaddingLeft(30)
        .setFontColor(Color.WHITE).setBackgroundColor(Color.BLUE));
    table.addCell(new Cell().add("row 1; cell 1")
        .setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
    table.addCell(new Cell().add("row 1; cell 2"));
    table.addCell(new Cell().add("row 2; cell 1").setMargin(10)
        .setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
    table.addCell(new Cell().add("row 2; cell 2").setPadding(10)
        .setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
    document.add(table);
    document.close();
}

这是它的样子:

对不起,如果它有点伤害眼睛,但使用这些颜色似乎是向我解释边距和填充之间差异的最佳方式。

大部分属性都是继承而来的。例如:如果您为 Div 设置字体,该字体将成为添加到 Div 的所有元素的默认字体。不过也有一些例外。填充是其中之一。 Cell class 属性的默认值是这样定义的:

@Override
public <T1> T1 getDefaultProperty(int property) {
    switch (property) {
        case Property.BORDER:
            return (T1) (Object) DEFAULT_BORDER;
        case Property.PADDING_BOTTOM:
        case Property.PADDING_LEFT:
        case Property.PADDING_RIGHT:
        case Property.PADDING_TOP:
            return (T1) (Object) 2f;
        default:
            return super.<T1>getDefaultProperty(property);
    }
}

如您所见,整个单元格没有填充值;填充由四个值组成,默认情况下它们是相同的。

如果您不想为每个 Cell 定义不同于默认值的填充,只需创建 Cell 的子 class 并将其命名为 MyCustomCell .通过覆盖 getDefaultProperty() class.

来使用您选择的填充,使其自定义

在本教程中,您会找到一个子示例class,它绘制的单元格带有圆角边框,这样我们就不必在每次要介绍时都设置声明渲染器圆角。

我是该文档的原作者。我希望您觉得回答有关 Cell 和 iText 7 中其他对象的这些问题和其他问题很有用。

我在 C# 中根据@Bruno Lowagie 重写路由将默认值设置为无填充和无边框:

public class BorderlessCell : Cell
    {
        public BorderlessCell(int rowSpan, int colSpan) : base(rowSpan, colSpan) { }
        public BorderlessCell() : base() { }
        public override T1 GetDefaultProperty<T1>(int property) 
        {
            switch (property) 
            {
                case Property.BORDER:
                    return (T1)(Object)(Border.NO_BORDER);
                case Property.PADDING_BOTTOM:
                case Property.PADDING_LEFT:
                case Property.PADDING_RIGHT:
                case Property.PADDING_TOP:
                   return (T1)(Object)(0);
                default:
                    return base.GetDefaultProperty<T1>(property);
            }
        }
    }

对我有用的是编辑 WidthPercentage,例如:

table.setWidthPercentage(100)