anais1477
anais1477

Reputation: 496

iText 7 - Image in Cell of Table fill full height

I add images in my cells of a table, but it doesn't fit the whole height.

The height is not fixed and can change between 2 rows.

How can my images fit the full height of my cells ?

Here is my problem : enter image description here

Here is the code of creation of my table :

int nbColumns = 7 + planning.size() *4;
Table table = new Table(nbColumns);
table.setWidthPercent(100);
table.addCell(createCell(""));
table.addCell(createCell("Col1"));
table.addCell(createCell("Col2"));
DateFormat hourFormat = new SimpleDateFormat("HH", Locale.FRENCH);
for(Date hourDate : planning){
    table.addCell(new Cell(1,4).setTextAlignment(TextAlignment.CENTER).add(hourFormat.format(hourDate)).setFont(regular).setFontSize(10));
}

table.addCell(createCell("Long"));
table.addCell(createCell("A"));
table.addCell(createCell("B"));
table.addCell(createCell(""));

Here is how I add my images for each cell :

String IMG = // my img path
table.addCell(createImageCell(IMG));

public static Cell createImageCell(String path) throws MalformedURLException {
            Image img = new Image(ImageDataFactory.create(path));
            Cell cell = new Cell().add(img.setMargins(0,0,0,0).setAutoScaleHeight(true).setAutoScale(true)).setPadding(0);
            cell.setBorder(null);
            return cell;
        }

Upvotes: 0

Views: 1697

Answers (1)

Samuel Huylebroeck
Samuel Huylebroeck

Reputation: 1719

Posting this as an answer for the sake of visibility.

With regards to autoscaling:

  • setAutoScaleHeight() is currently bugged in iText7 on the develop branch (so the bug will be present in 7.0.5 and prior versions). It currently sets the AUTO_SCALE_WIDTH(probably due to a copy-paste oversight), unless the AUTO_SCALE_WIDTHproperty is already set, then it will put them both on false and set the AUTO_SCALE to true.

  • Fixing the typo does not result in expected behaviour, since we're now aware of the issue, a ticket for it has been added to our backlog.

  • Bi-directional autoscaling (via the AUTO_SCALE-property) works correctly, but will scale uniformly and thus only scale to width in these case where the cell is greater in height than width.

As for a temporary solutions or trick to bypass it, I don't have a generic one :( for now, apart from waiting for the fix.

I did a quick test with relative height declarations (which should be included in 7.0.5), but that scaled the image uniformly again. Some trial&error and Image.scaleAbsolute() can get a desired result but that's hardly automate-able. You could in theory hook into the layout-process by writing your own custom CellRenderer, extracting the height of the largest Cell in to row to use in conjuction with scaleAbsolute(), but you're kinda writing the auto-scaling logic yourself at that point.

Some remarks on the OP's posted code as well, in the interest of spreading good practice:

int nbColumns = 7 + planning.size() *4;
Table table = new Table(nbColumns);

The constructor Table(int) is deprecated (since 7.0.2) and can lead to unexpected behaviour in later versions (this was done when improving the table-layouting mechanisms). It's better to pass a UnitValue[], preferably created using UnitValue.createPercentArray(float[]) or UnitValue.createPercentArray(float[])

img.setMargins(0,0,0,0).setAutoScaleHeight(true).setAutoScale(true))

setAutoScale after setAutoScaleHeight makes the latter redundant.

Upvotes: 1

Related Questions