user4140835
user4140835

Reputation:

Introducing a cell that spans two rows in a PDF table

I am using iText (Java version) to create a PDF document as shown in the figure below:

enter image description here

I want to create content as shown in the highlighted part. I finished development of all the other parts of the PDF except, except for the highlighted part.

See:

enter image description here

Upvotes: 1

Views: 3296

Answers (1)

Yash
Yash

Reputation: 13804

You can achieve this by properly using Colspan and rowspan in iText. An example can be found below:

https://developers.itextpdf.com/examples/tables/colspan-and-rowspan

I have added a small code block for the part I assume you'd be having trouble with:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(3);
    table.setWidths(new int[]{ 1, 1, 1});
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("8"));
    cell.setColspan(2);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("10"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("15"));
    cell.setColspan(1);
    cell.setRowspan(2);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("16"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("17"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("24"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("25"));
    cell.setColspan(1);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("mm"));
    cell.setColspan(2);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("mm"));
    cell.setColspan(1);
    table.addCell(cell);
    document.add(table);
    document.close();
}

The resulting pdf looks like following: enter image description here

I have used iText version 5.0.6

Upvotes: 2

Related Questions