0

I need to write letters with diacritics to PDF using PDFBox.

PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);

PDType1Font font = PDType1Font.HELVETICA;
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("čćšž");
contentStream.endText();
contentStream.close();
document.save("document.pdf");
document.close();

But instead of getting "čćšž" I'm getting garbage in document.

My research didin't get me anywhere. Solution 1 doesn't work, and Solution 2 says it is not possible to load right encoding. Is it really true?

Community
  • 1
  • 1
  • You have to use a font which includes those characters. the standard 14 regular fonts (e.g. Helvetica) don't. And, of course, you have to use an appropriate encoding which means some work when using the `PDPageContentStream` methods. – mkl Mar 12 '14 at 16:57
  • Thank you for your response. Ok, I added the font that supports those characters. What does this "work" look like? Do you have example maybe? – user3139293 Mar 13 '14 at 06:57
  • *Ok, I added the font that supports those characters.* - Have you made sure it is also added using an **encoding supporting your special characters**? *What does this "work" look like? Do you have example maybe?* - `contentStream.drawString(...)` writes the text without encoding it to match the font encoding mentioned above. Thus, you have to instead build the whole drawing operation and encode it into a `byte[]` yourself (respecting the encoding) and then add it using `contentStream.appendRawCommands(...)`. [This answer](http://stackoverflow.com/a/22274334/1729265) shows it for a € character. – mkl Mar 13 '14 at 09:42

0 Answers0