iText 中写Word RTF 文档 中文字体设置 [转]
2013-04-22 13:27
549 查看
http://www.kaifajie.cn/kecheng/java/8421.html
传统使用iTextAsian.jar中定义的字体
Java代码
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);
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格式不支持,显示的是英文名称的字体
Java代码
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)
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
Java代码
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
// 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
// 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
传统使用iTextAsian.jar中定义的字体
Java代码
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);
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格式不支持,显示的是英文名称的字体
Java代码
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)
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
Java代码
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
// 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
// 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
相关文章推荐
- iText 中写Word RTF 文档 中文字体设置
- java-pdf-itext 生成pdf 文档(支持中文字体)
- Java导出Word文档(可设置字体)
- IText 中文字体解决方案 生成doc文档
- java-pdf-itext7、itextpdf 生成pdf 文档,使用pdf模板生成(支持中文字体)
- Matlab中文字体乱码设置-----help文档
- word文档分别批量修改中文与英文字体大小字号等格式
- Itext设置导出word文件的字体
- latex学习笔记1 latex中文、英文、数学字体设置
- 在powerdesigner9中建了pdm模型,想把它用word,RTF,HTM文档导出
- css设置各种中文字体如雅黑、黑体、宋体、楷体等等
- iText编辑PDF动态生成文档,支持中文
- lucene 索引非txt文档 (pdf word rtf html xml)
- Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
- word文档页码设置-118
- 给同一篇Word文档设置多个不同的页眉页脚
- Opera中文字体的设置
- ITEXT字体设置
- word巧妙设置页码(做标书文档设置页码必备方法)
- Gentoo本地化设置--时区、时钟、字体、中文环境