I’m trying to print Arabic in some PDF documents using the Java code found here :
http://www.java2s.com/Code/Java/PDF-RTF/ArabicTextinPDF.htm
The example works great, except that the text comes out backwards. For example, changing the example slightly :
String txt = "\u0623\u0628\u062c\u062f\u064a\u0629 \u0639\u0631\u0628\u064a\u0629";
System.out.println(txt);
g2.drawString(txt, 100, 30);
What is printed on the screen are the same characters but in the opposite direction, compared to the PDF. The console output is correct, the PDF is not.
I don’t want to simply reverse the characters because otherwise I would lose bi-directional support …
Thanks much
IIRC, iText supports Arabic shaping at a highler level than
drawString. Lets see here…Ah!
ColumnText.showTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation, int runDirection, int arabicOptions)Alignment is one of
Element.ALIGN_*. Run direction is one ofPdfWriter.RUN_DIRECTION_*. Arabic options are bit flags,ColumnText.AR_*That should do the trick, with one caveat: I’m not sure that it’ll handle multiple directions in the same phrase. Your test string has CJKV, Arabic, and Latin characters, so there should be two direction changes.
Good luck.