JasperReports:如何添加字体不在应用程序类路径中 [英] JasperReports: How to add font not in the application classpath

查看:177
本文介绍了JasperReports:如何添加字体不在应用程序类路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一种字体,它没有安装在我的本地操作系统上,并带有 JasperReports 。 jasper报告以这种方式使用:

< textField>
< reportElement x =0y =0width =137height =20/>
< textElement>
< / textElement>
< textFieldExpression class =java.lang.String><![CDATA [$ F {something}]]>< / textFieldExpression>
< / textField>

字体名称​​ Corbel 作为字体扩展名(使用iReport)导出,并包含在我的系统中的文件夹(Corbel.jar)中。
我将这个扩展添加到应用程序的类路径,使用如下代码:

  ClassLoader cl = new URLClassLoader(new URL [] {new URL(file:/// D:/ path / to / Corbel_jar_folder /)}); 
param = new HashMap();
param.put(JRParameter.REPORT_CLASS_LOADER,cl);
jasperReport = JasperCompileManager.compileReport(d:/path/to/Report_with_Corbel_font.jrxml);
jasperPrint = JasperFillManager.fillReport(jasperReport,param,new JREmptyDataSource());

完成报表后,我使用 JRPdfExporter 。但是,在结果pdf文件中,该元素没有应用 Corbel 字体。我没有包括PDF输出,因为我认为这个问题在填充的地方。
我已经在 JasperReports 中搜索并阅读了许多与使用/包括字体(即字体扩展)相关的帖子和​​问题。我仍然不知道错误或问题在哪里。
任何帮助将不胜感激。



预先感谢您! (对不起的代码缩进,我希望我已经包含足够的细节)
解决方案

问题是加载的字体扩展jars是从线程上下文类加载器和 JRParameter.REPORT_CLASS_LOADER ,也不是 JRExporterParameter.CLASS_LOADER 完成的。因此,在我的情况下,当前(初始)线程类加载器必须被保存,我们不得不做一些像 Thread.currentThread( ).setContextClassLoader(cl),其中 cl 是基于 JasperReports 的应用程序的上下文然后线程上下文类加载器被恢复到原来的状态。



这个问题已经被回答了,详情可以这里



我希望这个答案能够帮助其他类似(字体)问题的人。


I am trying to use a font, that is not installed on my local OS, with a JasperReports. The jasper report uses in this way:

<textField>  
  <reportElement x="0" y="0" width="137" height="20"/>  
        <textElement>  
    <font fontName="Corbel" size="12"/>  
    </textElement>  
    <textFieldExpression class="java.lang.String"><![CDATA[$F{something}]]></textFieldExpression>  
</textField>

The font named Corbel was exported as a font extension (using iReport) and is contained in file (Corbel.jar), in folder, on my system. I add this extension to the classpath of the application, using a code as follows:

ClassLoader cl = new URLClassLoader(new URL[] {new URL("file:///D:/path/to/Corbel_jar_folder/")});  
param = new HashMap();    
param.put(JRParameter.REPORT_CLASS_LOADER, cl);  
jasperReport = JasperCompileManager.compileReport("d:/path/to/Report_with_Corbel_font.jrxml");  
jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());

After the report is filled, I export it using a JRPdfExporter. However, in the result pdf file, the element does not have the Corbel font applied. I have not included the pdf exporting, because I think that the problem is somewhere with the filling. I have searched and read numerous posts and questions related to using/including fonts (i.e.font extensions) in JasperReports; still I am not aware of where the mistake or the problem resides. Any help would be greatly appreciated.

Thank you in advance! (sorry for the bad code indentation and I hope I have included enough details)

解决方案

The problem was that the loading of font extensions jars is done from the thread context classloader and from the JRParameter.REPORT_CLASS_LOADER, nor the JRExporterParameter.CLASS_LOADER.

Therefore, in my case, the current (initial) thread classloader had to be saved, the we had to do something like Thread.currentThread().setContextClassLoader(cl), where cl was the context of the JasperReports based application and then the thread context classloader was reverted to the original one.

The question has been answered and details are available here.

I hope this answer will help others facing similar (font) issues.

这篇关于JasperReports:如何添加字体不在应用程序类路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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