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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With