Skip to main content
Skip table of contents

How to define the width of a cell?

I'm trying to change the width of a cell, but I don't succeed. Can someone explain how to define the width of a cell?

Posted on StackOverflow on Aug 13, 2015 by 99maas

There are different ways to define the width of a cell. To explain the different options, we have to talk about defining the width of the table (all columns) first, and then talk about defining the width of the separate columns.

Width of a table:

Option 1: You don't define an absolute width.

Instead you ask iText to calculate the width based on the available space. The available space is the width of the page minus the size of the left and the right margin.

If you create a document like this:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdfDoc);

Then the width of the page is 595 user units (this is the width of an A4 page) and the width of the margins is 36 user units (these are default values if you don't define the margins explicitly). Hence the available width is 523 user units.

In iText 7 you define your table like this:

Table table = new Table(2);

then the table will take up 80% of the available width when you add this table to a page. So if there's an available width of 523, the width of your table will be 418.4 user units.

You can change this by changing the width percentage. For instance if you add:

table.setWidthPercent(100);

then 100% of the available width will be used and your table will be 523 user units wide.

Option 2: You define an absolute width.

Suppose that you are asked to create a table with a width of 4 inches. By default 1 inch is 72 user units, so you need a table of 288 user units.

This can be achieved like this:

Table table = new Table(2);
table.setWidth(288);

Width of columns:

When you don't define any widths, each column will have the same width. This width will be calculated by dividing the width of the table by the number of columns. E.g. if the width of the table is 288 user units, and if the table has two columns, each column will be 144 user units.

Suppose that you want the second column to be three times as wide as the first column, then you can change the relative widths of the columns like this:

Table table = new Table(new float[] { 1, 3 });

If the width of table is 288, the width of the first column will now be 72 and the width of the second column will be 216.

You'll get the same result if you do this:

Table table = new Table(new float[] { 25, 75 });

The widths are relative values. { 1, 3 } is the equivalent of { 25, 75 }.

Width of the cells:

The width of the cells follows the widths of the columns, but Cell object also has setWidth(), setWidthPercent() and setStrokeWidth() methods. If you define the absolute width of the columns and then create a cell with a new width value, then the new width will be applied. See, for instance, ColumnWidthExample.

Click How to define the width of a cell? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.