javapdfitextarabic

How to display Arabic in PDF created using iText


I need your help in display the Arabic content and to start the writing from right to left in the PDF sample that I am trying to create. Here is the sample code:

public static void main(String[] args) throws IOException {
    try {

        BaseFont ArialBase = BaseFont.createFont("C:\\Users\\dell\\Desktop\\arialbd.ttf", BaseFont.IDENTITY_H, true);
        Font ArialFont = new Font(ArialBase, 20);


        Document document = new Document(PageSize.LETTER);


        PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\dell\\Desktop\\HelloWorld.pdf"));
        document.setMargins(72f, 72f, 72f, 0f);

        document.open();
        document.add(new Paragraph("الموقع الإلكتروني,",ArialFont));
        document.close();
        System.out.println("PDF Completed");

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

With the above code, the Arabic text will be shown as below:

الموقع الإلكتروني,

which is unidentified and the text is from left to right. So how can I solve this?


Solution

  • 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.