I'm using JDK 1.6.0_35
in a Java Project and I'm having a problem with java.util.ResourceBundle when recovering a properties file (encoded in ISO8859-1
).
We've been asked to show an invoice with english labels but an only one, which must be written in bulgarian. The issue is that it recovers and displays only the text which is not written in Bulgarian.
I've tried to insert the text in two ways:
\u0424\u0430\u043A\u0442\u0443\u0440\u0430\r\n\u041E\u0440\u0438\u0433\u0438\u043D\u0430\u043B
"Фактура\r\nОригинал"
Neither of them worked. I'm googling and it seems like it should work with the first way (though uncomfortable, it should work), but the text written like this does not display in my PDF (I'm using Jasperreports).
When doing JasperFillManager.fillReport()
my params variable contains "INVOICE=INVOICE/XXXФактураXXX", but when opening the PDF it only displays "INVOICE/XXX".
Java code:
Object obj = JRLoader.loadObject(new InputStream(someSource));
JasperReport reportMaster = (JasperReport) obj;
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(data);
JasperPrint jasperPrint = JasperFillManager.fillReport(reportMaster, params, ds);
String pathArialFont = "";
String pathArialBFont = "";
String pathArialIFont = "";
String pathArialBIFont = "";
try {
pathArialFont = Thread.currentThread().getContextClassLoader().getResource("arial.ttf").toURI()
.toString();
pathArialBFont = Thread.currentThread().getContextClassLoader().getResource("arialbd.ttf").toURI()
.toString();
pathArialIFont = Thread.currentThread().getContextClassLoader().getResource("ariali.ttf").toURI()
.toString();
pathArialBIFont = Thread.currentThread().getContextClassLoader().getResource("arialbi.ttf").toURI()
.toString();
LOGGER.debug("pathArialBIFont:".concat(pathArialBIFont));
} catch (final URISyntaxException e) {
LOGGER.error("", e);
}
String arial = "Arial";
HashMap<FontKey, PdfFont> fontMap = new HashMap<FontKey, PdfFont>();
FontKey key1 = new FontKey(arial, false, false);
PdfFont font1 = new PdfFont(pathArialFont, BaseFont.CP1252, true);
FontKey key2 = new FontKey(arial, true, false);
PdfFont font2 = new PdfFont(pathArialBFont, BaseFont.CP1252, true);
FontKey key3 = new FontKey(arial, false, true);
PdfFont font3 = new PdfFont(pathArialIFont, BaseFont.CP1252, true);
FontKey key4 = new FontKey(arial, true, true);
PdfFont font4 = new PdfFont(pathArialBIFont, BaseFont.CP1252, true);
fontMap.put(key1, font1);
fontMap.put(key2, font2);
fontMap.put(key3, font3);
fontMap.put(key4, font4);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, informe);
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);
reportMaster.setProperty("net.sf.jasperreports.awt.ignore.missing.font", "true");
exporter.exportReport();
Here you can see in one of my debuggings that the string arrives to Java code in cyrillic:
fontsfamily1365159936026.xml:
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="Arial">
<normal><![CDATA[fonts/arial.ttf]]></normal>
<bold><![CDATA[fonts/arialbd.ttf]]></bold>
<italic><![CDATA[fonts/ariali.ttf]]></italic>
<boldItalic><![CDATA[fonts/arialbi.ttf]]></boldItalic>
<pdfEncoding><![CDATA[Cp1252]]></pdfEncoding>
<pdfEmbedded><![CDATA[false]]></pdfEmbedded>
</fontFamily>
</fontFamilies>
jasperreports_extension.properties:
net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.ireportfamily1365159936026=fontsfamily1365159936026.xml
Any clue?
I finally achieved it. I needed to change these two lines in the xml:
<pdfEncoding><![CDATA[Identity-H]]></pdfEncoding>
<pdfEmbedded><![CDATA[true]]></pdfEmbedded>
I changed in the Java code the BaseFont constant BaseFont.CP1252 in these lines:
new PdfFont(pathArialFont, BaseFont.IDENTITY_H, true);
and entered the text in my ISO8859-1 properties file like this:
label=\u0424\u0430\u043A\u0442\u0443\u0440\u0430\r\n\u041E\u0440\u0438\u0433\u0438\u043D\u0430\u043B
And in the designer mode I edited the field to font "Arial Unicode MS" and checked "Pdf embedded" with Pdf Encoding "Identity-H".