编程语言
首页 > 编程语言> > java-JasperReports:使用背景带创建水印

java-JasperReports:使用背景带创建水印

作者:互联网

我试图弄清楚我们如何使用纯JavaJava JasperReport设置水印(以编程方式自行构建JasperDesign).

假设我有一个水印图像文件,如下所示:

String imageFileUri = "/some/url/MyWatermark.jpg";

现在,我想在JasperDesign中添加背景带,该背景带将MyWatermark.jpg图像覆盖在报告的每一页上.我有几行“开始代码”,但此后却空白.我已经在代码示例中搜索了很多内容,但是找不到.

JRDesignBand backgroundBand = new JRDesignBand();
backgroundBand.setHeight(842);
backgroundBand.setWidth(595);

// ...???

在此先感谢您的澄清.

解决方法:

为了解决这个问题,我们可以使用位于背景带中的JRDesignImage元素.

样品:

JasperDesign jasperDesign = new JasperDesign();

//Some code to filling bands .....

JRDesignBand band = new JRDesignBand();
band.setHeight(200);

//The expression must contain the double quotes!!! 
//This is a analogue of <imageExpression><![CDATA["watermark.png"]]></imageExpression>
String imgPath = "\"watermark.png\"";

JRDesignExpression expression = new JRDesignExpression();
expression.setText(imgPath);

JRDesignImage image = new JRDesignImage(jasperDesign);
image.setX(45);
image.setY(55);
image.setWidth(165);
image.setHeight(40);
//We can use another scaling mode
image.setScaleImage(ScaleImageEnum.FILL_FRAME);
image.setExpression(expression);

//Adds image to the band
band.addElement(image);

//Adds band as background 
jasperDesign.setBackground(band);

标签:jasper-reports,java
来源: https://codeday.me/bug/20191101/1987546.html