Java iText PdfPTable larger than 6 columns

I want to modify a table that is generated by iText to have one more column.

But when I change the constructor argument from:

PdfPTable table = new PdfPTable(6);

to:

PdfPTable table = new PdfPTable(7);

and add content to it like I do to other columns:

cell1 = new PdfPCell(new Phrase(name));
    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell1);

The table doesn't exist in the generated PDF. Why is that? I'm guessing it does not fit the document width?

Upvotes: 0

Views: 2791

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Suppose that this works (it does work, I know):

PdfPTable table = new PdfPTable(6);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);

In this case, a table with a single row and 6 columns will be added.

If you change this snippet like this, no table will be added:

PdfPTable table = new PdfPTable(7);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);

This table won't be added, because there is only a single row in it, and incomplete rows aren't rendered.

You have two options, either you add:

table.completeRow();

Or you add:

table.addCell("7");

right before adding the table to the document.

There is no reason why you could add a table with 6 columns but not a table with 7 columns. Width isn't an issue: if you don't define an absolute width for the columns, then iText will calculate the exact width of each column automatically.

I don't know if I have a document with a table that has 7 columns, but I do have an example of a table that has 8: see the SimpleTable example and simple_table.pdf.

Upvotes: 1

Related Questions