版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inforstack/article/details/47083251
1.根据路径获取:
Font fontZh = FontFactory.getFont("C:\\Windows\\Fonts\\MSYH.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
2.使用iTextAsian.jar中的字体
Font fontZh = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", 14, Font.BOLD, new CMYKColor(0, 255, 0, 0));
3.TestPDF.java
public static void main(String[] args) {
try {
Document document = new Document();
OutputStream os = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\test.pdf"));
PdfWriter.getInstance(document, os);
document.open();
document.add(new Paragraph("看看有没有输出中文?", getFont()));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
注:
1.个人习惯用FontFactory,FontFactory只不过对BaseFont进行了封装(个人理解,不喜勿喷)。FontFactory.getFont()中最后的实现还是调用了BaseFont.createFont();