可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to use external font in PDF document. I have no problem with using it in iReport after installing it via Settings -> Fonts -> Install new font.
The problem that when I export the new font as extension and adding that jar to java project's classpath - the PDF couldn't be generated and fails with
JRFontNotFoundException: Font 'Arial Custom' is not available to the JVM exception
What I am doing wrong? Thank you
UPD#1:
jasperreports_extensions.properties
net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory net.sf.jasperreports.extension.simple.font.families.ireportfamily1329192368547=fonts/fontsfamily1329192368547.xml
fontsfamily1329192368547.xml
fonts directory includes all ttf files. I just added that jar to classpath.
回答1:
Here is my working sample.
The font definition file (I dig it from the font's jar file):
The jar file is in the application's classpath.
And here is my java code:
String defaultPDFFont = "Arial"; JRProperties.setProperty("net.sf.jasperreports.awt.ignore.missing.font", "true"); JRProperties.setProperty("net.sf.jasperreports.default.font.name", defaultPDFFont); JasperReport jasperReport = JasperCompileManager.compileReport(reportSource); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params); JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
I set the net.sf.jasperreports.awt.ignore.missing.font property to prevent the error you have (JRFontNotFoundException: Font 'Arial Custom' is not available to the JVM exception) in case the font is missing. And I set the net.sf.jasperreports.default.font.name property for determine the font that will be used in the resulting pdf file.
You can read info about this properties: net.sf.jasperreports.awt.ignore.missing.font and net.sf.jasperreports.default.font.name.
The resulting pdf file is using this font (Arial in this sample).