user7393258
user7393258

Reputation: 41

Set Table Column width problems in iText7

When I want to create table in PDF, I can use the following two ways: But the first one failed.

method 1:

float[] columnWidths = {20, 30, 50};
Table table = new Table(columnWidths);

It's failed to control columnWidths method 2:

UnitValue[] unitValue = new UnitValue[]{
UnitValue.createPercentValue((float) 20),
UnitValue.createPercentValue((float) 30),
UnitValue.createPercentValue((float) 50)};
Table table = new Table(columnWidths);

It's success! Why does this happen?

Upvotes: 3

Views: 3686

Answers (2)

Pavel Alay
Pavel Alay

Reputation: 111

Yes, there is no way to scale column widths in iText 7.0.2, as Vernon said.

If you want to use directly 20, 30, and 50 points for columns despite min widths you shall use this construction:

Table table = new Table(new float[] {20, 30, 50}) // in points
        .setWidth(100) //100 pt
        .setFixedLayout();

If you set fixed layout you must set width as well, it is required for fixed layout.

Upvotes: 5

Vernon
Vernon

Reputation: 65

The Release Notes for iText Core 7.0.2 include the bullet point "improved auto layout and fixed layout for tables, scaled column widths have been removed". If you need the old functionality, you'll need to go back to 7.0.1!

The online API docn for the Table constructor says Note, since 7.0.2 in case auto layout column width values less than min width will be ignored. It's not great English, but I believe it's trying to say that the supplied values will not be scaled up so the columns fill the table, but instead will be used as-is, in which case they're almost certain to be too small, so will be ignored. Overall, this change makes sense, since treating a handful of floats as a ratio is a bit arbitrary for a good API. Using values with units is much more sensible, and your example using proper percentages is a good one.

Upvotes: 2

Related Questions