飞碟iTextPDF中文字体 [英] Flying Saucer iTextPDF Chinese Fonts

查看:182
本文介绍了飞碟iTextPDF中文字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FlyingSaucer(R8pre2版本)将XHTML转换为PDF。


$ b HTML有中文字符,正在使用MS Mincho字体。



文档已被转换,但所有中文字符在转换后的PDF文件中都没有。

任何想法如何解决这个问题
$ b 代码:
$ b $ pre $ OutputStream os = new FileOutputStream(output .PDF);
String inputFile =input.html;
String url = new File(inputFile).toURI()。toURL()。toString();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();


解决方案

中文字体需要在中文字体之前加载显示。例如:

$ p $ renderer.getFontResolver()。addFont(fonts / TIMES.TTF,BaseFont.IDENTITY_H,BaseFont.EMBEDDED );
renderer.getFontResolver()。addFont(fonts / TIMESBD.TTF,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
renderer.getFontResolver()。addFont(fonts / TIMESBI.TTF,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
renderer.getFontResolver()。addFont(fonts / TIMESI.TTF,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);


I'm using FlyingSaucer (R8pre2 version) to convert an XHTML into PDF.

The HTML has Chinese characters and are using MS Mincho fonts.

The document was converted, but all the Chinese characters are missing on the converted PDF.

Any idea how to resolve this problem?

Code:

OutputStream os = new FileOutputStream("output.pdf");
String inputFile = "input.html";
String url = new File(inputFile).toURI().toURL().toString();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();

解决方案

Chinese font is required to be loaded before Chinese font is displayed. For example:

renderer.getFontResolver().addFont("fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESBD.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESBI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

这篇关于飞碟iTextPDF中文字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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