gpt4 book ai didi

itext7 - 如何在 iText 7 中设置和/或检索默认单元格填充

转载 作者:行者123 更新时间:2023-12-01 12:26:35 25 4
gpt4 key购买 nike

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

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

最佳答案

请看 iText 7: Building Blocks教程。

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

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

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类是 BlockElement 的子类类(class)。 BlockElementAbstractElement 的子类类(class)。 AbstractElement类是 ElementPropertyContainer 的子类类(class)。

如果您想更改内边距(或者如果您愿意,可以更改边距),请阅读 chapter 5那个教程。它有一个示例,名为 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();
}

这是它的样子:

enter image description here

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

大多数属性都是继承的。例如:如果您为 Div 设置字体,该字体将是添加到该 Div 的所有元素的默认字体。 .不过也有一些异常(exception)。填充就是其中之一。这是特定于 Cell 的属性的默认值的方式。类被定义:
@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 的子类并称之为 MyCustomCell .通过覆盖 getDefaultProperty() 使用您选择的填充,使其自定义。类(class)。

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

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

关于itext7 - 如何在 iText 7 中设置和/或检索默认单元格填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com