I am trying to use Apache PDFBox 1.8.6to create a pdf file in Java. (See code below)
If I write a line: Hello! 123 abc äöüßeverything works fine.
But if I add the € sign or it’s the equivalent of \ u20ac, 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 programs like OpenOffice can export pdf using or other Unicode characters without any problems.
So what do I need to do to write a Unicode String in a PDF?
try {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream stream = new PDPageContentStream(doc, page);
PDFont font = PDType1Font.COURIER;
stream.setFont(font, 14);
stream.beginText();
stream.setNonStrokingColor(Color.BLACK);
stream.moveTextPositionByAmount(20, 750);
String text = "Hello! 123 abc äöüß € \u20ac";
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);
}
source
share