java itextpdf pdf 支持中文

public static boolean fillTemplate(String templatePath,OutputStream out, Map<String,Object> map,int index) { PdfReader reader;ByteArrayOutputStream bos;PdfStamper stam...

java  itextpdf 在填充给PDF文件的时候,虽然有itext-asian 对亚洲字体支持,单支持并不好,有写时候填充汉字会显示莫名其妙的乱码,或空白。

解决这个问题,可以使用系统自带的中文字体。linux和windows上加载中文字体如下:


将Windows字体拷贝到Linux服务器:

# 1.安装字体命令、安装字体索引指令

yum -y install fontconfig

yum install mkfontscale

# 2.查看已安装字体

# (1)查看linux已安装字体

fc-list

# (2)查看linux已安装中文字体

fc-list :lang=zh

# 3.安装中文字体

# (1)创建目录

mkdir -p /usr/share/fonts/my_fonts

# (2)将要安装的字体上传到该文件夹下这里我们安装楷体 常规,即simKai.ttf。

# 关于具体字体查找,我以windows为例。进入C:\Windows\Fonts,该文件夹下就存放相关字体,

# 将simhei.ttf拷贝到linux 目录/usr/share/fonts/my_fonts下即可,如:

cp simKai.ttf /usr/share/fonts/my_fonts

# (3)生成字体索引,进入目录 cd  /usr/share/fonts/my_fonts,执行索引字体生成

mkfontscale

# (4)刷新linux的字体缓存,使其立刻生效

fc-cache -fv

# 4.查看黑体常规字体是否安装成功

fc-list :lang=zh

代码中引用字体

Windows系统下测试:

BaseFont bfKai = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

Font myKai8B = new Font(bfKai, 8, Font.BOLD);


Linux系统字体路径引用:

BaseFont bfKai = BaseFont.createFont("/usr/share/fonts/my_fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);  //楷体

Font myKai8B = new Font(bfKai, 8, Font.BOLD);


当字体为字体集时需要指定第几个:


BaseFont bfSong = BaseFont.createFont("/usr/share/fonts/my_fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);  //宋体

Font mySong8B = new Font(bfSong, 8, Font.BOLD);


不同国家字体的选择

itext对不同国家的语言似乎没有提供一个统一的字体,不同国家的语言需要引用本国的字体,如中文需使用simkai.ttf、simsun.ttc等、韩语需使用malgun.ttf、泰语需要用LeelawUI.ttf,怎么找这些字体呢?其实Widows的字体就很全面,直接在C:\Windows\Fonts找对应国家字体即可:


JAVA代码如下:

public static boolean fillTemplate(String templatePath,OutputStream out, Map<String,Object> map,int index) {

PdfReader reader;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//itext pdf对中文支持不好,部分中文显示不了,使用系统字体
// BaseFont baseFont = BaseFont.createFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont baseFont = BaseFont.createFont("/usr/share/fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
form.addSubstitutionFont(baseFont)
;
int i = 0;
Iterator<String> it = form.getFields().keySet().iterator();
while (it.hasNext()) {
String name = it.next();
form.setField(name, (String) map.get(name));

}
//true代表生成的PDF文件不可编辑
stamper.setFormFlattening(true);
stamper.close();
com.itextpdf.text.Document doc = new com.itextpdf.text.Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
for (int j = 1; j < index; j++) {
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), j);
copy.addPage(importPage);
}
doc.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
return true;
}


  • 发表于 2023-12-26 19:52
  • 阅读 ( 466 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
石天
石天

437 篇文章

作家榜 »

  1. shitian 662 文章
  2. 石天 437 文章
  3. 每天惠23 33 文章
  4. 小A 29 文章