com.itextpdf.layout.element.Table

IText 7 使用Table

  • 使用表格来呈现数据。

Demo1

// 创建对象指定固定比例的列
Table table = new Table(UnitValue.createPercentArray(10)).useAllAvailableWidth()

// 设置边框
table.setBorder(Border.NO_BORDER)

// 可以设置表头表尾巴,当表呈现在两个页面的时候,第二页会自动呈现出表头。
table.addHeaderCell(...)
table.addFooterCell(...)

// 添加Cell 以及 合并参数:rowspan,colspan
table.addCell(new Cell(1, 4)
        .setBorder(Border.NO_BORDER)
        .add(new Paragraph("")))
table.addCell(new Cell(1, 6)

Demo2

  • 通常我们创建一个Style 对象来控制所有的 Cell 的样式。
  • .setTextAlignment(TextAlignment.RIGHT) 设置容器内文本的对其方式
  • .setVerticalAlignment(VerticalAlignment.MIDDLE) 设置容器内容器的垂直位置
  • cellStyle.setKeepTogether(true) 当一个单元格处在两个页面中间的时候,默认会被拆开,这里让他保持在一起。
Style cellStyle = new Style()
cellStyle.setVerticalAlignment(VerticalAlignment.MIDDLE)
cellStyle.setTextAlignment(TextAlignment.CENTER)
cellStyle.setKeepTogether(true)

Demo3

  • 如何有效的调整 Table中 Cell 的宽度?
float[] width = new float[]{0.5,1.5,1,1,1,1,1,1,1,1}
Table table = new Table(width).useAllAvailableWidth()
原文地址:https://www.cnblogs.com/duchaoqun/p/13994336.html