Jasper Report中文設置粗體樣式之后在列印時并沒有顯示粗體效果,網上查了該問題的原因是 Jasper Report對中文字體的參數并沒有接受,一種方法就是改變源代碼,另外一種方式就在調用的時候用JAVA注入樣式效果;方法如下:
首先在Ireport需要顯示 正楷體 “我們”分別用粗體,正常體顯示,因此我們將需要設置粗體的我們TEXT FIELD字體設置一個另外的字體,這里我們選擇了ARIAL NARROW字體接著用Ireport編譯即可,接著貼一段我的源代碼:
public void onFont() {
WebApp app = this.getDesktop().getWebApp();
InputStream inputStream = app.getResourceAsStream("/Rpt/dtd.jasper");
JasperPrint jasperPrint;
try {
jasperPrint = JasperFillManager.fillReport(inputStream,new HashMap(), new JREmptyDataSource());
JRPdfExporter exporter = new JRPdfExporter();
Map fontsMap = new HashMap();
fontsMap.put(new FontKey("Arial Narrow", true, false), new PdfFont(
"c:/kaiu.ttf", "Identity-H", false, true, false));
fontsMap.put(new FontKey("標楷體", true, false), new PdfFont(
"c:/kaiu.ttf", "Identity-H", false, false, false));//設置字體樣式
exporter.setParameter(JRExporterParameter.FONT_MAP, fontsMap);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
FileOutputStream fos = new FileOutputStream("c:/test.pdf");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
exporter.exportReport();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
首先在Ireport需要顯示 正楷體 “我們”分別用粗體,正常體顯示,因此我們將需要設置粗體的我們TEXT FIELD字體設置一個另外的字體,這里我們選擇了ARIAL NARROW字體接著用Ireport編譯即可,接著貼一段我的源代碼:
public void onFont() {
WebApp app = this.getDesktop().getWebApp();
InputStream inputStream = app.getResourceAsStream("/Rpt/dtd.jasper");
JasperPrint jasperPrint;
try {
jasperPrint = JasperFillManager.fillReport(inputStream,new HashMap(), new JREmptyDataSource());
JRPdfExporter exporter = new JRPdfExporter();
Map fontsMap = new HashMap();
fontsMap.put(new FontKey("Arial Narrow", true, false), new PdfFont(
"c:/kaiu.ttf", "Identity-H", false, true, false));
fontsMap.put(new FontKey("標楷體", true, false), new PdfFont(
"c:/kaiu.ttf", "Identity-H", false, false, false));//設置字體樣式
exporter.setParameter(JRExporterParameter.FONT_MAP, fontsMap);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
FileOutputStream fos = new FileOutputStream("c:/test.pdf");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
exporter.exportReport();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}