How to display Arabic in PDF created using iText

Wrong encoding:

It is a bad programming practice to use non-ASCII characters in your source code. For instance, you have "الموقع الإلكتروني". This String should be interpreted as double byte UNICODE characters. However, when you save the source code file using an encoding different from UNICODE, or when you compile that code using a different encoding, or when your JVM uses a different encoding each double-byte character risks to be corrupted, resulting in gibberish such as "الموقع الإلكتروني"

How to solve this? Use the UNICODE notation: "\u0627\u0644\u0645\u0648\u0642\u0639 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a"

Please consult the official documentation, the free ebook The Best iText Questions on StackOverflow, where you will discover that this problem has already been described here: Can’t get Czech characters while generating a PDF

Wrong font:

If you read this book carefully, you’ll discover that your example might not work because you may be using the wrong font. This is explained in my answer to this question: Arabic characters from html content to pdf using iText

You are assuming that arialbd.ttf can produce Arabic glyphs. As far as I know only arialuni.ttf supports Arabic.

Wrong approach:

Furthermore, you are overlooking the fact that you can only use Arabic in the context of the ColumnText and the PdfPCell object. This is explained here: how to create persian content in pdf using eclipse

For instance:

BaseFont bf = BaseFont.createFont(
    "c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 20);
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(36, 730, 569, 36);
column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
column.addElement(new Paragraph(
    "\u0627\u0644\u0645\u0648\u0642\u0639 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a", font));
column.go();

Note that I am using the Identity-H encoding because UNICODE is involved.

Leave a Comment