# 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?

25th November 2015

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:

``````Document document = new Document();
``````

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.

When you define your table like this:

``````PdfPTable table = new PdfPTable(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.setWidthPercentage(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:

``````PdfPTable table = new PdfPTable(2);
table.setTotalWidth(288);
table.setLockedWidth(true);
``````

If you forget the line `table.setLockedWidth(true);` then iText will assume that you want iText to calculate the width based on the (default) width percentage and the available width. Locking the width, switches iText to use the total width as an absolute width.

Width of columns:

Case 1: relative widths

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:

``````PdfPTable table = new PdfPTable(2);
table.setWidths(new float[] { 1, 3 });
``````

or, if you want to use only one line:

``````PdfPTable table = new PdfPTable(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:

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

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

Case 2: absolute widths

Suppose that you don't want to use relative widths and you want to create a table width two columns of which the first one is 1 inch wide and the second one is 3 inches wide, then you can once again use the principle of the locked width of a table. Take a look at:

``````PdfPTable table = new PdfPTable(2);
table.setTotalWidth(new float[]{ 72, 216 });
table.setLockedWidth(true);
``````

Now we pass an array to the `setTotalWidth()` method. Widths are no longer relative, and we tell iText to use these absolute widths by locking the width.

Now the first column (and all the cells in the first column) will be 72 user units (1 inch) wide and the second column will be 216 user units (3 inch) wide.

Width of the cells:

The width of the cells follow the widths of the columns.

Contact

## Still have questions?

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.