【发布时间】: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,我评论了椭圆绘图,现在我有一个问题:我无法在单元格中设置添加马赛克图像