如何使用pdfbox将unicode文本写入pdf? [英] How to write unicode text to pdf with pdfbox?
问题描述
我尝试使用 Apache PDFBox 1.8.6
在 Java
中创建PDF。 (见下面的代码)
如果我写字符串:你好! 123abcäöüß
一切正常。
但是如果我添加一个€符号或它的等价\ u20ac字符串搞砸了:
þÿ你好 ! 1 2 3abcäöü߬¬|
我认为这与编码有关,因为像OpenOffice这样的程序可以用€或其他Unicode符号导出pdf而不用问题。
那么我需要做什么才能将Unicode字符串写入PDF?
I try to use Apache PDFBox 1.8.6
to create a PDF in Java
. (See code below)
If I write the string: Hello! 123 abc äöüß
everything works fine.
But if I add an € sign or it's equivalent \u20ac the String gets messed up:þÿ H e l l o ! 1 2 3 a b c ä ö ü ß ¬ ¬ ¦
I think this has something to do with the encoding, since programms like OpenOffice can export pdf with € or other Unicode signs without a problem.
So what do I have to do to write Unicode String to PDF?
try {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream stream = new PDPageContentStream(doc, page);
PDFont font = PDType1Font.COURIER;
//font.setFontEncoding(new EncodingManager().getEncoding(COSName.WIN_ANSI_ENCODING));
stream.setFont(font, 14);
stream.beginText();
stream.setNonStrokingColor(Color.BLACK);
stream.moveTextPositionByAmount(20, 750);
String text = "Hello! 123 abc äöüß € \u20ac";
//JOptionPane.showMessageDialog(null, text);
stream.drawString(text);
stream.endText();
stream.stroke();
stream.close();
doc.save("test.pdf");
doc.close();
} catch (Exception ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
推荐答案
显然,PDFBox不支持Unicode字体。也就是说,直到现在:在这个错误被一个好人修复后, PDFBox 2.0.0的主干完美地显示了我的Unicode。
Apparently, PDFBox did not support Unicode fonts. That is, until now: after this bug has been fixed by a great guy, the trunk of PDFBox 2.0.0 shows my Unicode perfectly.
这篇关于如何使用pdfbox将unicode文本写入pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!