JasperReports:如何添加不在应用程序类路径中的字体 [英] JasperReports: How to add font not in the application classpath
问题描述
我正在尝试使用带有 JasperReports
的字体,该字体未安装在我的本地操作系统上.jasper 报表是这样使用的:
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>
名为 Corbel
的字体被导出为字体扩展(使用 iReport),并包含在我系统上的文件夹中的文件 (Corbel.jar) 中.我将此扩展添加到应用程序的类路径中,使用如下代码:
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());
报告填写后,我使用 JRpdfExporter
将其导出.但是,在结果 pdf 文件中,该元素没有应用 Corbel
字体.我没有包括pdf导出,因为我认为问题出在填充的某个地方.我在 JasperReports
中搜索并阅读了许多与使用/包括字体(即字体扩展)相关的帖子和问题;我仍然不知道错误或问题出在哪里.任何帮助将不胜感激.
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)
推荐答案
问题是字体扩展 jar 的加载是从线程上下文类加载器和 JRParameter.REPORT_CLASS_LOADER
完成的,也不是JRExporterParameter.CLASS_LOADER
.
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
.
因此,就我而言,必须保存当前(初始)线程类加载器,我们必须执行类似 Thread.currentThread().setContextClassLoader(cl)
之类的操作,其中 cl
是基于 JasperReports
的应用程序的上下文,然后线程上下文类加载器恢复到原来的.
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屋!