Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write unicode text to pdf with pdfbox?

Tags:

java

pdf

pdfbox

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);
    }
like image 917
Andie2302 Avatar asked Aug 27 '14 10:08

Andie2302


1 Answers

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.

like image 86
Alex Nevidomsky Avatar answered Nov 15 '22 03:11

Alex Nevidomsky