【问题标题】:Image in mode mosaic in PdfPCellPdfPCell 中的模式马赛克图像
【发布时间】:2015-12-09 10:56:12
【问题描述】:

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

为了设置图像,我使用了 itextpdf.com 的 this solution 现在我想在模式镶嵌的 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);
        }
    }
}

【问题讨论】:

  • 您好布鲁诺,感谢您的回复。我编辑了我的问题。我不知道如何应用镶嵌工艺。
  • 我会使用图像作为平铺图案中的一个平铺来创建图案颜色。然后我会将该图案颜色应用为背景颜色。
  • 你有任何使用图案颜色和图像的例子吗?
  • 在官方网站上快速搜索,发现TilingPatternColor 示例。这不能回答你的问题吗?
  • 我添加了我的代码java,我评论了椭圆绘图,现在我有一个问题:我无法在单元格中设置添加马赛克图像

标签: itext itextpdf


【解决方案1】:

请查看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 示例所示。这个例子是为了回答问题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));

结果如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多