[英]How to add image as parameter from projects classpath in jasper reports
I designed a jasper report using ireport designer in which I added logo image in the title of the report.我使用 ireport 设计器设计了一个 jasper 报告,其中我在报告的标题中添加了徽标图像。 This image is added from the hard coded path on the local machine.该图像是从本地机器上的硬编码路径添加的。 I need to add the logo image from my projects classpath.我需要从我的项目类路径中添加徽标图像。 To do that I created a parameter for the image in the report which is supplied from the program.为此,我为程序提供的报告中的图像创建了一个参数。
InputStream imgInputStream = this.getClass().getResourceAsStream("header.png");
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("dateFrom", datum1);
parameters.put("dateTo", datum2);
parameters.put("logo", imgInputStream);
strQuery = "Select calldate,src,dst,duration,disposition,cdrcost from cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'";
rs = conexiondb.Consulta(strQuery);
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
//JasperPrint jasperPrint = JasperFillManager.fillReport(reportStream, parameters);
JasperRunManager.runReportToPdfStream(reportStream, fos, parameters, resultSetDataSource);
And below is the image snippet from the report:以下是报告中的图片片段:
<image>
<reportElement x="0" y="1" width="555" height="61"/>
<imageExpression><![CDATA[$P{logo}]]>
</imageExpression>
</image>
We always pass in the image instead of the InputStream.我们总是传入图像而不是 InputStream。 First load up the image and set it in the parameter map:首先加载图片并在参数图中设置:
BufferedImage image = ImageIO.read(getClass().getResource("/images/IMAGE.png"));
parameters.put("logo", image );
Then the parameter is just defined like:然后参数定义如下:
<parameter name="logo" class="Object" isForPrompting="false">
<parameterDescription><![CDATA[The letterhead image]]></parameterDescription>
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
And when placed in the report it looks like:当放在报告中时,它看起来像:
<image>
<reportElement x="324" y="16" width="154" height="38"/>
<imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>
You can easily get the URL form the classpath/classloader.您可以轻松地从类路径/类加载器中获取 URL。 This is a valid input for <imageExpression> and therefore you can use it to embed an image in your pdf.这是 <imageExpression> 的有效输入,因此您可以使用它在 pdf 中嵌入图像。 The following worked for me:以下对我有用:
Setting the parameter:设置参数:
URL url = this.getClass().getClassLoader().getResource("pdf/my_image.tif");
parameters.put("logo", url);
Declaration in the report:报告中的声明:
<parameter name="logo" class="java.net.URL">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
Usage in the report.报告中的用法。
<image>
<reportElement x="100" y="30" width="135" height="30"/>
<imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>
Some additional observations一些额外的观察
I did not manage to get it working with any of those methods, I was having the error :我没有设法让它与任何这些方法一起工作,我遇到了错误:
Error evaluating expression for source text
at compiling the report in java.在java中编译报告。
In java you have to get your image to an inputstream so either在 Java 中,您必须将图像发送到输入流,因此要么
byte[] image = imageRepository.getLogo();
InputStream logo= new ByteArrayInputStream(image);
parameters.put("logo",logo);
because I am getting the image as a byteArray from a database, but if you have it somewhere in your JAR:因为我从数据库中将图像作为 byteArray 获取,但是如果您在 JAR 中的某个地方有它:
ResourceLoader resourceLoader;
InputStream logo= resourceLoader.getResource("classpath:/image/logo.jpg").getInputStream();
parameters.put("logo",logo);
Then in the in jrxml it simply gives :然后在 jrxml 中它简单地给出:
<parameter name="logo" class="java.io.InputStream"/>
<image scaleImage="RealSize">
<imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.