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

本文探讨了在iTextAsian中使用不同字体的方法,包括传统方式、引用Windows字体及通过RtfWriter2实现在Word文档中正确显示中文字体的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

传统使用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中表现如下

word

写字板中表现如下:

写字板

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值