I'm trying to tile an image or background image in a table cell in iTextSharp. The closest I have gotten is to attach an image directly to the cell using a PdfPTable and PdfPCell.
tempCell = new PdfPCell();
tempCell.Image = iTextSharp.text.Image.GetInstance(Path.Combine(GetImageDirectory(), "my_image.gif"));
table.AddCell(tempCell);
This has its issues, though. The cells are never the same size in my tables, so the images are scaled automatically by width. This can make some of the cells extra tall comparatively to other cells, or only half full of the image if they are narrow.
Any suggestions on how to get the images tiling, if it is even possible with iTextSharp? Thanks!
If all else fails, you can create a pattern fill and set that in the cell event (or fill in behind the entire table).
You'll draw your image into a PdfPatternPainter
template with the bounds you want for the repeating pattern. You then draw your image into this template at whatever scale you want.
PdfPatternPainter patternPainter = someContent.createPattern(width, height);
patternPainter.addImage( image, desiredImgSizeX, 0, 0, desiredImgSizeY, 0, 0 );
someContent.saveState();
someContent.setPatternFill( patternPainter );
someContent.rectangle( llx, lly, wid, hei );
someContent.fill();
someContent.restoreState();
Your PdfPCellEvent
will be given everything you need in the cellLayout override. You'll probably want to use canvases[PdfPTable.BASECANVAS]
in place of someContent
above.
You'll want to stash and reuse your patternPainter so you don't get a duplicate for each cell you use it in. That could cause some pretty horrific file bloat. No beuno.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With