6

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);
    }
Andie2302
  • 4,825
  • 4
  • 24
  • 43
  • I think the problem is with Pdf font with COURIER, look on: http://stackoverflow.com/questions/5425251/using-pdfbox-to-write-utf-8-encoded-strings-to-a-pdf – albciff Aug 27 '14 at 11:15
  • Maybe you should use `TextToPdf` see `TestUnicodeTest.java` on https://issues.apache.org/jira/browse/PDFBOX-903 – SubOptimal Aug 27 '14 at 12:00
  • possible duplicate of [PdfBox encode symbol currency euro](http://stackoverflow.com/questions/22260344/pdfbox-encode-symbol-currency-euro) – mkl Aug 27 '14 at 21:40
  • http://stackoverflow.com/questions/5425251/using-pdfbox-to-write-utf-8-encoded-strings-to-a-pdf Duplicate? – Alex Nevidomsky Jan 27 '15 at 12:38

1 Answers1

4

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.

Alex Nevidomsky
  • 668
  • 7
  • 14