当您使用 Table 和 Cell 类在 iText 7 中创建表格时,表格单元格默认带有一些内置的填充。据我通过查看生成的文档可以看出,它似乎是大约 2 个 PDF 单位。
有什么方法可以检索此值以用于计算?另外,有什么方法可以更改此默认值,以便我可以设置自己的填充以在所有表格的所有单元格中使用,而不必在每个单元格上单独设置?
当您使用 Table 和 Cell 类在 iText 7 中创建表格时,表格单元格默认带有一些内置的填充。据我通过查看生成的文档可以看出,它似乎是大约 2 个 PDF 单位。
有什么方法可以检索此值以用于计算?另外,有什么方法可以更改此默认值,以便我可以设置自己的填充以在所有表格的所有单元格中使用,而不必在每个单元格上单独设置?
请查看iText 7: Building Blocks教程。
在开始之前部分中,我们看到每个构建块都派生自一个名为 的类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
类中可用的便捷方法,为什么会变得困难:
System.out.println(cell.getPaddingLeft());
System.out.println(cell.getPaddingRight());
System.out.println(cell.getPaddingTop());
System.out.println(cell.getPaddingBottom());
正如您在教程中看到的,Cell
该类是该类的子BlockElement
类。是该类BlockElement
的子AbstractElement
类。该类AbstractElement
是该类的子ElementPropertyContainer
类。
如果您想更改内边距(如果您愿意,也可以更改边距),请阅读该教程的第 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();
}
这是它的样子:
对不起,如果它有点伤害眼睛,但使用这些颜色似乎是向我解释边距和填充之间差异的最佳方式。
大多数属性是继承的。例如:如果您为 a 设置字体Div
,则该字体将是添加到该 的所有元素的默认字体Div
。不过也有一些例外。填充就是其中之一。这是定义特定于类的属性的默认值的方式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);
}
}
如您所见,整个单元格没有填充值;填充由四个值组成,默认情况下它们是相同的。
如果您不喜欢为 each 定义不同于默认值的填充Cell
,只需创建一个 的子类Cell
并调用它MyCustomCell
。从某种意义上说,它通过覆盖getDefaultProperty()
类来使用您选择的填充,使其自定义。
在本教程中,您将找到一个子类示例,该示例绘制带有圆角边框的单元格,这样我们就不必在每次想要引入圆角时都设置声明渲染器。
我是该文档的原作者。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)