Remove any spacing, padding, and margin so a cell is filled completely with iText 7

Viewed 817

How do I make a table in iText 7 with cells that don't have any spacing, padding, or margins on the inside. When I put an image in the cell it needs to fill the cell to 100%, touching the borders without any space of any kind at all.

I tried adding this to my Cell:

Cell qrImageCell = new Cell();
qrImageCell.setMargin(0f);
qrImageCell.setPadding(0f);
qrImageCell.setHeight(44f);
qrImageCell.add(barcodeImage.setMargins(0f,0f,0f,0f).setPadding(0f));

I also tried setting it in the table itself:

table.setPadding(0f);
table.setMargin(0f);

But whatever I try there is always a white rectangular area between the QR Code and the border (FYI: I would of course remove the border once it works.

enter image description here

2 Answers

Test with iText 7.1.15

With this image:

enter image description here

And this code:

Table table = new Table(1);
Image barcodeImage = new Image(ImageDataFactory.create("image.png"));
Cell qrImageCell = new Cell();
qrImageCell.setBorder(new SolidBorder(ColorConstants.RED, 2));
qrImageCell.setPadding(0f);
qrImageCell.add(barcodeImage);
table.addCell(qrImageCell);
doc.add(table);

The resulting output is:

enter image description here

Potential causes

Incorrect height

You have qrImageCell.setHeight(44f) in your code. Maybe that height does not correspond correctly with the height of your image. It's unlikely though that this is the problem, because it would not increase the width of the cell.

Impact of other cells

Making the table of the test above a 2-column table and adding a few cells shows that the size of the cells in the same row and column will have an impact on the size of the cell.

table.addCell(new Cell().add(new Paragraph("cell (1,2)")).setHeight(300));
table.addCell(new Cell().add(new Paragraph("cell (2,1)")).setWidth(300));
table.addCell(new Cell().add(new Paragraph("cell (2,2)")));

enter image description here

It's also unlikely that this is the problem, because in your output the image is centered in the cell.

There is no white space

Maybe the white "margins" are simply part of your barcode image. In that case, there isn't any spacing, margin or padding, but it would visually seem so.

Verify your input image or set a background color for the cell, so you can verify whether the white area is part of the image or part of the cell: qrImageCell.setBackgroundColor(ColorConstants.GREEN)

In the end the backgroundcolor trick helped me a lot. In the end the QR Image which is also generated by iText7 for some reason has a white border around itself. It's generated by this code:

BarcodeQRCode qrCode = new BarcodeQRCode(text);
PdfFormXObject barcodeObject = qrCode.createFormXObject(ColorConstants.BLACK, pdfDoc);
Image barcodeImage = new Image(barcodeObject).setPadding(0f).scaleAbsolute(44f, 44f)

But indeed setting the padding to "0f" removes any padding in the cell. Just have to find out if there's a possibility to remove that border around the QR code.

Related