PdfPCell 中的模式马赛克图像

Image in mode mosaic in PdfPCell

我目前正在使用 itextPdf 库生成 PDF 文件。

为了设置图像,我使用了 this solution of itextpdf.com 现在我想在 PdfPCell 模式马赛克中设置一个小尺寸图像作为背景:如果单元格有 3 x ImageSize,在 PDF 中我将在单元格

中重复我的图像 3 次

我该怎么做?

这是我的例子

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);
        }
    }
}

请查看 TiledBackgroundColor 示例。它获取灯泡图像并使用它来定义图案颜色:

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);

现在您可以使用该颜色作为单元格的背景:

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

结果如下所示:tiled_patterncolor.pdf

或者您可以在单元格事件中添加图像,如 TiledBackground example. This example was written in answer to the question iTextSharp. Why cell background image is rotated 90 degrees clockwise?

中所示

我写了一个关于这个例子的变体:TiledBackgroundColor2

事件看起来像这样:

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);
        }
    }

}

如您所见,我不关心图像的实际尺寸。我以适合单元格高度的方式缩放图像。我也不使用图案颜色。我只是根据单元格的宽度多次添加图像。

这是我向单元格声明事件的方式:

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

结果如下所示:

根据您的具体要求,可能会有许多变化。