2

I am trying to create a new pdf from scratch, but I cant seem to print special characters correctly, for example I try to print the following string in the PDF:

『I like 《this string》 yeah.』

and get

þÿ0I like 0this string0 yeah.0

The result is correctly printed by means of the logger and println, would anyone know how to do it, allready tried ascii encoding like \u008A.

Any ideas?

Ricardo Garza V.
  • 899
  • 1
  • 11
  • 26
  • possible duplicate of [Using PDFBox to write UTF-8 encoded strings to a PDF](http://stackoverflow.com/questions/5425251/using-pdfbox-to-write-utf-8-encoded-strings-to-a-pdf) – Sean F Dec 22 '14 at 00:10
  • allready tried that, no result, kinda better, but still does not work – Ricardo Garza V. Dec 22 '14 at 00:34
  • UTF-8 is not supported in the official release 1.8.8. But it is supported (since a few days ago!) in the unreleased 2.0 version, which you can get with svn. https://pdfbox.apache.org/downloads.html#scm – Tilman Hausherr Dec 22 '14 at 08:12
  • Furthermore, please make sure the font you use contains a glyph for that character. – mkl Dec 22 '14 at 09:02

1 Answers1

3

It is now possible to print utf-8 characters with PDFBOX. You need to download (https://github.com/apache/pdfbox) the latest trunk version and compile it (you can compile it using maven it is the best option belive me). The second thing that you need is a ttf font that has the glphys you are using in it.

The code that works for me:

package utftext;

import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

public class utftext {

    public static void main(String[] args) throws Exception {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        document.addPage(page);
        PDType0Font font = PDType0Font.load(document, new File("E:\\arialuni.ttf"));
        PDPageContentStream stream = new PDPageContentStream(document, page);
        String text = "ssdfg #$%&&English 012 čćžšđ ČĆŽŠĐ as äöüÖÜÄ";
        stream.beginText();
        stream.setFont(font, 12);
        stream.moveTextPositionByAmount(50, 600);
        stream.drawString(text);
        stream.endText();
        stream.close();
        document.save("type0.pdf");
        document.close();
    }

}
ASu
  • 156
  • 1
  • 5
  • Is there any source for arialuni.ttf (or any other reasonable unicode font) that has MIT, Apache, BSD, or similar license? – Tolstoyevsky Oct 26 '17 at 18:05