5

I am generating PDF file using itext lib. I want to write Arabic words. When i run the below code, The words characters are reverse displayed.

The used code :

PdfContentByte cb = docWriter.getDirectContent();
BaseFont bfBold = BaseFont.createFont("assets/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);


createHeadings(cb, document.leftMargin(), 70, "السعر الاجمالي: " + tprice + " L.E.");

. . .

private void createHeadings(PdfContentByte cb, float x, float y, String text){
        cb.beginText();
        cb.setFontAndSize(bfBold, 10);
        cb.setTextMatrix(x,y);
        cb.showText(text.trim());
        cb.endText();
    }

This image describes the output of the code above: https://i.stack.imgur.com/OLoLo.jpg

Mohamed Awad
  • 53
  • 1
  • 5

1 Answers1

8

Please take a look at the Ligatures2 example.

Aren't you forgetting this line:

cb.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

The setRunDirection() method is necessary when you want iText to write the text from right to left and create ligatures where necessary. This method also exists in the context of tables in which case you apply it to a PdfPCell object instead of to a ColumnText object.

Also, I don't understand why you use this String: "السعر الاجمالي: ". Please use the Unicode notation instead (e.g. something like "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628"), because using a String like yours can create all kinds of confusion regarding encoding and ligatures. Some editors won't use the correct encoding (changing your text into gibberish); some editors will make ligatures (which isn't what iText expects).

For instance, in your case, I don't know Arabic, so I don't know if it's "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a" or "\u064a\u0644\u0627\u0645\u062c\u0627\u0644\u0627 \u0631\u0639\u0633\u0644\u0627" because I don't know if I have to start to read at the glyph with value \u0627 or at the glyph with value \u064a. In any case: iText expects the first "character" in the String to be the first thing that is read by humans.

Please take a look at the ArabicExample example:

enter image description here

The first line is incorrect, because RTL nor Arabic ligatures are supported when using document.add(). The second line is correct (as far as I know: I can't read Arabic) because I used ColumnText.

This is the code I used:

public static final String FONT = "resources/fonts/NotoNaskhArabic-Regular.ttf";
public static final String ARABIC = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Phrase p = new Phrase("This is incorrect: ");
    p.add(new Chunk(ARABIC, f));
    p.add(new Chunk(": 50.00 USD"));
    document.add(p);

    p = new Phrase("This is correct: ");
    p.add(new Chunk(ARABIC, f));
    p.add(new Phrase(": 50.00"));

    ColumnText canvas = new ColumnText(writer.getDirectContent());
    canvas.setSimpleColumn(36, 750, 559, 780);
    canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
    canvas.addElement(p);
    canvas.go();

    document.close();
}

I used a Phrase, but you can expect the same result when using a Paragraph (Paragraph extends Phrase). Please clarify if this doesn't answer your question. Take into account that most people on StackOverflow don't understand Arabic, so you have to be very explicit when you ask a question and when you say "it doesn't work". As we don't know Arabic, we don't know how it is supposed to work.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    I already made a table and I wrote arabic into it by customizing cell (cell.setRunDirection) and it works. But in this case above I cannot customize (cb). @Bruno – Mohamed Awad Nov 16 '15 at 15:26
  • 1
    Does your comment mean that the problem was solved? I'll update my answer adding the remark that the `setRunDirection()` method also exists for the `PdfPCell`. If that works for you, please accept the answer. – Bruno Lowagie Nov 16 '15 at 15:28
  • 1
    No, that was what I did before asking. I mean that I can type arabic in pdf but the problem that I have to parts in my pdf one part is the table which is arabic supported and the other part is the other text in the page and I cannot write it in arabic so I asked this question – Mohamed Awad Nov 16 '15 at 15:34
  • 1
    At all I changed the code and now I wrote the text using (Paragraph). Can I set text direction to paragraph ?? – Mohamed Awad Nov 16 '15 at 15:35
  • 1
    I've updated my answer. You can not set the run direction for a paragraph. You need to set the run direction of the `ColumnText` and add the paragraph to that column as described in my example. – Bruno Lowagie Nov 16 '15 at 17:06
  • Sorry this is not the right way to ask. But [this example](https://github.com/itext/i7js-book/blob/develop/src/main/java/com/itextpdf/samples/book/part3/chapter11/Listing_11_18_Ligatures2.java) is not working with `iText 7.2.1 (AGPL version)`. Here's the [screenshot](https://i.stack.imgur.com/8Qv3W.png). Is this the expected behavior? RTL is not supported with AGPL? – Irfan Latif Jan 15 '22 at 16:03
  • 1
    @IrfanLatif creating PDFs requiring other writing systems (Arabic, Indic,...) has been made extremely simple in iText 7, but you require the pdfCalligraph add-on to make that functionality work: https://itextpdf.com/en/products/itext-7/pdfcalligraph The code I gave in this example can only be used in obsolete versions of iText. – Bruno Lowagie Jan 17 '22 at 08:39
  • @BrunoLowagie Yes I had figured it out. But it took some time. It'd be better if the requirement of `pdfCalligraph` is clearly mentioned in the related documentation like [this FAQ](https://kb.itextpdf.com/home/it7kb/faq/how-to-create-persian-content-in-pdf), as it's mentioned in the last paragraph [here](https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/how-to-convert-html-containing-arabic-hebrew-characters-to-pdf). Thank you for your response. – Irfan Latif Jan 17 '22 at 12:02
  • 1
    @IrfanLatif I am no longer affiliated with iText Group and therefor no longer responsible for the documentation. I only answered because the comment showed up as a notification on the Tray icon in the menu on the top right ;-) – Bruno Lowagie Jan 17 '22 at 14:24
  • 1
    @BrunoLowagie well then I should thank you again. Hopefully future visitors may find my comment helpful. – Irfan Latif Jan 17 '22 at 15:29