【问题标题】:iText divide cell horizontaliText 水平分割单元格
【发布时间】:2019-04-26 05:18:19
【问题描述】:

我一直在尝试将一个单元格水平拆分为两个单元格(1 列,2 行)。或者,也可以在单元格中添加水平分隔符。它应该如下图所示。

如何在 Java 中使用 iText 7 实现这一点?

【问题讨论】:

  • 我删除了“你们”,因为 Stack Overflow 上不仅有男生。
  • 对于那些较小的单元格不做任何特别的事情,而是对较大的单元格使用行跨度怎么样?
  • @mkl 我试过这个,但它没有按预期工作。你有我的例子吗?

标签: java itext itext7


【解决方案1】:

正如评论中已经提到的,构建这样一个表的更合适的方法是通过行跨度创建大单元格并自然地拥有小单元格,而不是尝试通过单独划分大单元格来创建小单元格。

这可以这样完成:

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 测试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多