Valeriane
Valeriane

Reputation: 954

Image in mode mosaic in PdfPCell

I am currently using itextPdf library to generate PDF file.

For to set an image I used this solution of itextpdf.com Now I want to set a small size image as a background in PdfPCell in mode mosaic : if cell have 3 x ImageSize, in PDF I will have my image repeated 3 times in cell

How I can do it ?

this is my example

public class ImageBackgroundEvent implements PdfPCellEvent {

    protected Image image;
    protected boolean mosaic;
    protected boolean full;

    public ImageBackgroundEvent(Image image, boolean mosaic, boolean full) {
        this.image = image;
        this.mosaic = mosaic;
        this.full = full;
    }

    public void cellLayout(PdfPCell cell, Rectangle position,
                           PdfContentByte[] canvases) {
        try {
            PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
            if(full){
                cell.setImage(image);
            }
            else if(mosaic){
                float imgWidth = image.getWidth();
                float imgHeight = image.getHeight();

                float cellWidth = cell.getWidth();
                float cellHeight = cell.getHeight();

                if(imgHeight < cellHeight && imgWidth < cellWidth){
                    PdfPatternPainter pattern = cb.createPattern(imgWidth, imgHeight);
                    pattern.addImage(image);
                    pattern.setPatternMatrix(-0.5f, 0f, 0f, 0.5f, 0f, 0f);
                    cb.setPatternFill(pattern);
                    //cb.ellipse(180, 408, 450, 534);
                    cb.fillStroke();

                } else{
                    image.scaleAbsolute(position);
                    image.setAbsolutePosition(position.getLeft(), position.getBottom());
                    cb.addImage(image);
                }
            } else{
                image.scaleAbsolute(position);
                image.setAbsolutePosition(position.getLeft(), position.getBottom());
                cb.addImage(image);
            }

        } catch (DocumentException e) {
            throw new ExceptionConverter(e);
        }
    }
}

Upvotes: 1

Views: 276

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Please take a look at the TiledBackgroundColor example. It takes an image of a light bulb and uses it to define a pattern color:

PdfContentByte canvas = writer.getDirectContent();
Image image = Image.getInstance(IMG);
PdfPatternPainter img_pattern = canvas.createPattern(
        image.getScaledWidth(), image.getScaledHeight());
image.setAbsolutePosition(0, 0);
img_pattern.addImage(image);
BaseColor color = new PatternColor(img_pattern);

Now you can use that color for the background of your cell:

PdfPCell cell = new PdfPCell();
cell.setFixedHeight(60);
cell.setBackgroundColor(color);
table.addCell(cell);

The result looks like this: tiled_patterncolor.pdf

enter image description here

Or you could add the image in a cell event as shown in the TiledBackground example. This example was written in answer to the question iTextSharp. Why cell background image is rotated 90 degrees clockwise?

I've written a variation on this example: TiledBackgroundColor2

The event looks like this:

class TiledImageBackground implements PdfPCellEvent {

    protected Image image;

    public TiledImageBackground(Image image) {
        this.image = image;
    }

    public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
        try {
            PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
            image.scaleToFit(10000000, position.getHeight());
            float x = position.getLeft();
            float y = position.getBottom();
            while (x + image.getScaledWidth() < position.getRight()) {
                image.setAbsolutePosition(x, y);
                cb.addImage(image);
                x += image.getScaledWidth();
            }
        } catch (DocumentException e) {
            throw new ExceptionConverter(e);
        }
    }

}

As you see, I don't care about the actual dimensions of the image. I scale the image in such a way that it fits the height of the cell. I don't use a pattern color either. I just add the image as many time as it fits the width of the cell.

This is how I declare the event to the cell:

PdfPCell cell = new PdfPCell();
Image image = Image.getInstance(IMG);
cell.setCellEvent(new TiledImageBackground(image));

The result looks like this:

enter image description here

Many variations are possible depending on your exact requirement.

Upvotes: 1

Related Questions