Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the width of a cell?

I need your help in setting the length of a cell's bottom border. Currently, it is showing the bottom border of a cell too long and I need to shorten it. I tried to change the width of the table, but it is not coming properly.

Below is the code:

Paragraph tableParagraph = new Paragraph();
tableParagraph.setAlignment(Element.ALIGN_LEFT);

PdfPTable table55 = new PdfPTable(2);
table55 = new PdfPTable(new float[] { 6, 6 });
table55.setWidthPercentage(90f);
table55.getDefaultCell().setBorder(PdfPCell.NO_BORDER);

PdfPCell cel2a = new PdfPCell(new Paragraph("Total of Net Profit ", font));

PdfPCell cel2b = new PdfPCell(new Paragraph("100.000" + " USD  ", font));

cel2a.setBorder(Rectangle.NO_BORDER);
cel2b.setBorder(Rectangle.BOTTOM);
cel2a.setLeading(1f, 1.5f);
cel2b.setLeading(1f, 1.5f);

table55.addCell(cel2a);
table55.addCell(cel2b);
like image 528
99maas Avatar asked Dec 05 '22 20:12

99maas


1 Answers

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 }.

You did something strange:

PdfPTable table55 = new PdfPTable(2);
table55 = new PdfPTable(new float[] { 6, 6 });

In the first line you create a table with two columns of identical width new PdfPTable(2), but you never use that PdfPTable(2), because in the second row you redefine the table55 variable as new PdfPTable(new float[]{ 6, 6 } ).

As { 6, 6 } are relative values, this is equivalent to { 50, 50 } or { 1, 1 }... You're creating a table with two columns with the same width.

Case 2: absolute widths

Suppose that you don't want to use relative widths (for instance because the Math of calculating 1/4 of 288 and 3/4 of 288 is too hard) 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.

All of this has, of course, been explained in large detail in the documentation that is available online. See for instance examples such as ColumnWidths or the entire chapter that is dedicated to tables in the free ebook The Best iText Questions on StackOverflow. One wonders why no one ever reads that documentation. Does it make sense to keep on answering questions if no one is interested in the answers? (Sorry for this philosophical note, it has been a long day.)

like image 129
Bruno Lowagie Avatar answered Dec 19 '22 13:12

Bruno Lowagie