我目前正在使用 iText 生成 PDF 报告。我想将中等大小的图像设置为背景,PdfPCell
而不是使用背景颜色。这可能吗?
问问题
3129 次
1 回答
3
您可以在此处找到有关如何使用 iText 5.5.1 执行此操作的示例。您需要创建自己的 PdfPCellEvent
接口实现,例如:
class ImageBackgroundEvent implements PdfPCellEvent {
protected Image image;
public ImageBackgroundEvent(Image image) {
this.image = image;
}
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
try {
PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
image.scaleAbsolute(position);
image.setAbsolutePosition(position.getLeft(), position.getBottom());
cb.addImage(image);
} catch (DocumentException e) {
throw new ExceptionConverter(e);
}
}
然后你需要创建这个事件的一个实例,并将它声明给需要这个背景的单元格:
Image image = Image.getInstance(IMG1);
cell.setCellEvent(new ImageBackgroundEvent(image));
此代码已使用最新版本的 iText 进行了测试,结果如下所示。您在包名称 (com.lowagie) 中使用带有我的名字 (Lowagie) 的 iText 版本。这意味着此示例可能有效,也可能无效。我们不知道也不会测试,因为您使用的版本已在多年前宣布 EOL。它不再受支持。
于 2014-06-12T08:08:29.470 回答