我一直在尝试将一个单元格水平拆分为两个单元格(1 列,2 行)。或者,也可以在单元格中添加水平分隔符。它应该如下图所示。
如何使用 Java 中的 iText 7 实现此功能?
最佳答案
正如评论中已经提到的,构建此类表格的更合适方法是通过行跨度创建大单元格并自然地拥有小单元格,而不是尝试通过单独分区大单元格来创建小单元格。
可以这样完成:
try ( PdfWriter writer = new PdfWriter(RESULT_STREAM_OR_FILE);
PdfDocument pdfDocument = new PdfDocument(writer);
Document doc = new Document(pdfDocument) )
{
Table table = new Table(new float[] {30, 30, 30, 30, 30, 30, 30, 30, 30});
for (int i = 0; i < 4; i++) {
table.addCell(new Cell(2, 1).add(new Paragraph("Text")));
table.addCell(new Cell(2, 1).add(new Paragraph("Text")));
table.addCell(new Cell().setHeight(15));
table.addCell(new Cell(2, 1).add(new Paragraph("Text")));
table.addCell(new Cell().setHeight(15));
table.addCell(new Cell(2, 1).add(new Paragraph("Text")));
table.addCell(new Cell().setHeight(15));
table.addCell(new Cell(2, 1).add(new Paragraph("Text")));
table.addCell(new Cell().setHeight(15));
table.addCell(new Cell().setHeight(15));
table.addCell(new Cell().setHeight(15));
table.addCell(new Cell().setHeight(15));
table.addCell(new Cell().setHeight(15));
}
doc.add(table);
doc.close();
}
( CreateTable 测试 testCreateTableForDennis
)
结果如下所示:
<小时/>测试代码已使用 iText 7.1.4-SNAPSHOT 进行测试。
关于java - iText 水平分割单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53449312/