如何设置图像表达式来始终存储正确的图像路径 [英] How to set the image expression to always store the right image path

查看:150
本文介绍了如何设置图像表达式来始终存储正确的图像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JasperReports通过Java动态创建报表。我有图像表达式的问题(图像路径)。这是我现在通过的方式:

  JRDefaultStyleProvider JRDefaultStyleProvider = null; 
JRDesignImage image = new JRDesignImage(JRDefaultStyleProvider);
image.setX(0);
image.setY(0);
image.setWidth(200);
image.setHeight(200);
exp = new JRDesignExpression();
** exp.setText(\D:/MyProgram/src/myprogram/images/logo.png\); **
image.setExpression(exp);
image.setStyle(styles.imageStyle);
title_band.addElement(image);

它工作正常,但如果我更改MyProgram的位置,我也必须更改路径在表达中。
我尝试将表达式设置为: ../ images / logo.png 但是我收到一个错误数据未找到:../ images / logo.png 。任何帮助将不胜感激。

解决方案

有几件事你可以做。



在报表中手动添加名为ProjectRoot的参数,使用 $ P {ProjectRoot} +images / logo.png作为图像表达式,并传递一个值为ProjectRoot当您运行报表时,您可以使用它。



另一种方法是利用JasperReports也尝试将映像位置解析为类加载器资源的事实。因此,如果添加src / myprogram作为源文件夹,以便在运行时将image / logo.png作为项目类路径的一部分,您将可以使用images / logo.png作为图像表达式。



第三个解决方案是在 FileRepositoryService code> JasperReportsContext 您将用于填写报表的实例。文件存储库服务将使用当前项目根路径创建,您需要以某种方式从环境中确定。使用存储库服务还可以使用images / logo.png作为图像表达式。代码看起来像这样:

  SimpleJasperReportsContext context = new SimpleJasperReportsContext(); 
FileRepositoryService fileRepository = new FileRepositoryService(context,D:/ MyProgram / src / myprogram /,false);
context.setExtensions(RepositoryService.class,Collections.singletonList(fileRepository));
context.setExtensions(PersistenceServiceFactory.class,Collections.singletonList(FileRepositoryPersistenceServiceFactory.getInstance()));

JasperPrint jasperPrint = JasperFillManager.getInstance(context).fill(jasperReport,params);


I'm using JasperReports to create a report dynamically through Java. I have problem with the image expression (image path). This is the way I pass it now :

JRDefaultStyleProvider JRDefaultStyleProvider = null;
JRDesignImage image = new JRDesignImage(JRDefaultStyleProvider);
image.setX(0);
image.setY(0);
image.setWidth(200);
image.setHeight(200);
exp = new JRDesignExpression();
**exp.setText("\"D:/MyProgram/src/myprogram/images/logo.png\"");**
image.setExpression(exp);
image.setStyle(styles.imageStyle);
title_band.addElement(image);

It works fine, but if I change the location of MyProgram I'll have to also change the path in the expression. I tried setting the expression to : ../images/logo.png but I got an error of "Byte data not found at : ../images/logo.png". Any help would be appreciated.

解决方案

There are several things that you could do.

One is to manually add a parameter called ProjectRoot in the report, use $P{ProjectRoot} + "images/logo.png" as image expression, and pass a value for ProjectRoot (taken from the environment) when you run the report.

Another approach is to leverage the fact that JasperReports also attempts to resolve image locations as classloader resources. Therefore if you add src/myprogram as a source folder so that images/logo.png gets to be part of your project's classpath at runtime, you'll be able to use "images/logo.png" as image expression.

A third solution is to register a FileRepositoryService extension in a JasperReportsContext instance that you would use for filling the report. The file repository service would be created with the current project root path, that you need to somehow determine from the environment. Having a repository service would also allow you to use "images/logo.png" as image expression. The code would look something like this:

SimpleJasperReportsContext context = new SimpleJasperReportsContext();
FileRepositoryService fileRepository = new FileRepositoryService(context, "D:/MyProgram/src/myprogram/", false);
context.setExtensions(RepositoryService.class, Collections.singletonList(fileRepository));
context.setExtensions(PersistenceServiceFactory.class, Collections.singletonList(FileRepositoryPersistenceServiceFactory.getInstance()));

JasperPrint jasperPrint = JasperFillManager.getInstance(context).fill(jasperReport, params);

这篇关于如何设置图像表达式来始终存储正确的图像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆