【问题标题】:How to set the image expression to always store the right image path如何设置图像表达式始终存储正确的图像路径
【发布时间】:2016-11-28 19:50:06
【问题描述】:

我正在使用 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,但出现“Byte data not found at :../images/logo.png”错误。任何帮助,将不胜感激。

【问题讨论】:

    标签: java image jasper-reports expression


    【解决方案1】:

    您可以做几件事。

    一种是在报表中手动添加一个名为ProjectRoot的参数,使用$P{ProjectRoot} + "images/logo.png"作为图像表达式,并在运行报表时为ProjectRoot(取自环境)传递一个值。

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

    第三种解决方案是在JasperReportsContext 实例中注册一个FileRepositoryService 扩展,您将使用它来填写报告。文件存储库服务将使用当前项目根路径创建,您需要以某种方式从环境中确定。拥有存储库服务还允许您使用"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);
    

    【讨论】:

      猜你喜欢
      • 2015-03-12
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 2023-03-09
      • 2014-01-09
      • 2010-11-01
      • 2017-04-05
      相关资源
      最近更新 更多