sg552
sg552

Reputation: 1543

Generate table with double column beneath a row

I want to create a table such as this with itext: enter image description here

I tried to followed the solution here: Create a table in a generated PDF but unfortunately M and F column will not go beneath the Gender column. Supposedly it should work like in the answer but it is not working. I'm not sure what is wrong. This is my code:

document.open();
PdfPTable table = new PdfPTable(5);
table.setWidths(new float[]{ 1f, 3f, 1f, 1f, 1f});
PdfPCell cell;
cell = new PdfPCell(new Phrase("Chapter"));
cell.setRowspan(2);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Description"));
cell.setRowspan(2);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Gender"));
cell.setColspan(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Total"));
cell.setRowspan(2);
table.addCell(cell);

table.addCell("M");
table.addCell("F");

document.add(table);
document.close();

and this is my current output: enter image description here

Can somebody help me with this?? Thanks in advance.

Upvotes: 0

Views: 50

Answers (1)

Roman Pustylnikov
Roman Pustylnikov

Reputation: 1932

I understand it should be:

cell = new PdfPCell(new Phrase("Gender"));
cell.setColspan(2);
table.addCell(cell);

since you want it to be two columns wide.

Upvotes: 2

Related Questions