参见英文答案 > Apache PDFBox: Can I set font other than those present in PDType1Font 1个
我无法使用PDFBox将unicode字符写入PDF.下面是一些生成垃圾字符而不是输出“š”的示例代码.我可以添加什么来获得对UTF-8字符串的支持?
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDType1Font font = PDType1Font.HELVETICA;
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("š");
contentStream.endText();
contentStream.close();
document.save("test.pdf");
document.close();
解决方法:
您正在使用Adobe Reader附带的内置“Base 14”字体之一.这些字体不是Unicode;它们实际上是一个标准的拉丁字母,但有一些额外的字符.它看起来像你提到的字符,一个带有caron(š)的小写字母,在PDF拉丁文中没有…虽然大写的Š可用但仅在Windows