Dennis
Dennis

Reputation: 77

iText divide cell horizontal

I've been trying to split a cell horizontal into two cells (1 column, 2 rows). Alternative it's also okay to add a horizontal separator in a cell. It should look like in the picture below.

example of table with divided cells

How can I implement this with iText 7 in Java?

Upvotes: 1

Views: 1222

Answers (1)

mkl
mkl

Reputation: 95898

As already mentioned in a comment, the more appropriate way to construct such a table is to create the large cells by means of row spans and have the small cells naturally instead of trying to create the small cells by individually partitioning large cells.

This can be done like this:

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 test testCreateTableForDennis)

The result looks like this:

scrren shot


The test code has been tested with iText 7.1.4-SNAPSHOT.

Upvotes: 1

Related Questions