当前位置: 技术问答>java相关
iText的中文问题,FAQ上的帖子没有了,麻烦……
来源: 互联网 发布时间:2017-05-01
本文导语: 用iText怎样才能显示中文? 多谢先! | //设置字体-返回中文字体 protected Font getChineseFont(int nfontsize, boolean isBold) throws IOException, DocumentException { BaseFont bfChinese; if(isBold){ bfChinese =...
用iText怎样才能显示中文?
多谢先!
多谢先!
|
//设置字体-返回中文字体
protected Font getChineseFont(int nfontsize, boolean isBold) throws IOException, DocumentException {
BaseFont bfChinese;
if(isBold){
bfChinese = BaseFont.createFont("STSong-Light,Bold", "UniGB-UCS2-H", BaseFont.EMBEDDED);
}else{
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
}
Font FontChinese = new Font(bfChinese, nfontsize, Font.BOLD);
return FontChinese;
}
//转化中文
protected Cell ChangeCell(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException {
Phrase ph = ChangeChinese(str,nfontsize,isBold);
Cell cell = new Cell(ph);
//cell.setBorderWidth(3);
return cell;
}
//转化中文
protected Chunk ChangeChunk(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize,isBold);
Chunk chunk = new Chunk(str, FontChinese);
return chunk;
}
//转化中文
protected Phrase ChangeChinese(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize,isBold);
Phrase ph = new Phrase(str, FontChinese);
return ph;
}
返回各种类型,用于文本和表格足够了。
别忘了加入iTextAsian.jar
protected Font getChineseFont(int nfontsize, boolean isBold) throws IOException, DocumentException {
BaseFont bfChinese;
if(isBold){
bfChinese = BaseFont.createFont("STSong-Light,Bold", "UniGB-UCS2-H", BaseFont.EMBEDDED);
}else{
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
}
Font FontChinese = new Font(bfChinese, nfontsize, Font.BOLD);
return FontChinese;
}
//转化中文
protected Cell ChangeCell(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException {
Phrase ph = ChangeChinese(str,nfontsize,isBold);
Cell cell = new Cell(ph);
//cell.setBorderWidth(3);
return cell;
}
//转化中文
protected Chunk ChangeChunk(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize,isBold);
Chunk chunk = new Chunk(str, FontChinese);
return chunk;
}
//转化中文
protected Phrase ChangeChinese(String str, int nfontsize, boolean isBold) throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize,isBold);
Phrase ph = new Phrase(str, FontChinese);
return ph;
}
返回各种类型,用于文本和表格足够了。
别忘了加入iTextAsian.jar