您的位置:首页 > 其它

iText 中写Word RTF 文档 中文字体设置

2009-07-23 13:50 337 查看
传统使用iTextAsian.jar中定义的字体

BaseFont   bfChinese   =   BaseFont.createFont("STSong-Light",   "UniGB-UCS2-H",   BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font   FontChinese   =   new   com.lowagie.text.Font(bfChinese,   12,   com.lowagie.text.Font.NORMAL);


但是这种办法只能时是在亚洲语言包中定义的

2、网上查到的引用windows字体的方式,但度rtf格式不支持,显示的是英文名称的字体

BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL)


3 经过反复测试,下面这种办法支持word

// step 1: 定义
Document document = new Document();
try {
// step 2:
// 建立一个rtf文档
RtfWriter2.getInstance(document, new FileOutputStream(filePath + file));

// step 3: we open the document
document.open();
//设置字体  字体名称是中文的,在中间的中文字符前后加空格,
//这种写法是实验多次后的结果,直接写在word中体现为 "华?行?楷",这种写法感觉很怪异。
//在写字板中打开和word中打开不一样,见图
RtfFont font = new RtfFont("华 文 行 楷", 36, Font.BOLD, Color.BLACK);

String text = "这是中文字体测试!this is a test";
document.add(new Paragraph(text, font));

System.out.println(font.getFamilyname());
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}finally{

document.close();
}
// step 5: we close the document




word中表现如下



写字板中表现如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: