原因1.没有指定中文字体
方法一:使用itext-asian
<dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version> </dependency>
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
方法二:指定字体包地址
String fontPath = "font/simsunb.ttf"; BaseFont baseFont1 = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
原因2:代码先后顺序问题
正确示例:
Font contentFont = new Font(baseFont, 36, Font.NORMAL);
contentFont.setColor(BaseColor.BLACK);
Paragraph content = new Paragraph("Hello world!!你好!This is ...搁这搁这呢", contentFont);
Error show:
Paragraph content = new Paragraph("Hello world!!你好!This is ...搁这搁这呢");
Font contentFont = new Font(baseFont, 36);
contentFont.setColor(BaseColor.BLACK);
content.setFont(contentFont);