让itextpdf可以正常生成中文,需要一个jar包,最简单的做法就是去maven库下载
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
添加如下两个依赖即可。
BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
注意:在itext5之前,使用的是com.lowagie包,而之后包名改为了com.itextpdf,不要搞混了,不然会报一个错:
Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
欢迎加入iTextPDF中国区交流群:554713439