作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当您使用 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)。
BlockElement
是
AbstractElement
的子类类(class)。
AbstractElement
类是
ElementPropertyContainer
的子类类(class)。
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
的所有元素的默认字体。 .不过也有一些异常(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/
我是一名优秀的程序员,十分优秀!