1

I want to make a pdf report with English and Arabic texts. I have many tables/phrases across the page. I want to display Arabic text also along with English. I have seen the Arabic example in iText doxument also, using ColumnText. I couldn't help myself with that. My doubt is how to set canvas.setSimpleColumn(36, 750, 559, 780), the arguments in this method for tables/phrases at different positions. I have referred below questions also.Still I have issues.

Writing Arabic in pdf using itext,
http://developers.itextpdf.com/examples/font-examples/language-specific-exampleshe

Below is my code..

private static final String ARABIC = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";
    private static final String FONT = "resources/fonts/ARIALUNI.TTF";


    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
    document.open();
    Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    PdfPTable table = new PdfPTable(3);
    Phrase phrase = new Phrase();
    Chunk chunk = new Chunk("test value", inlineFont);

    phrase.add(chunk);
    // I want to add Arabic text also here..but direction is not //correct.also coming as single alphabets
    p.add(new Chunk(ARABIC, f));
    PdfPCell cell1 = new PdfPCell(phrase);
    cell1.setFixedHeight(50f);
    table.addCell(cell1);
    document.add(table);
    document.close();
Community
  • 1
  • 1
Sha
  • 11
  • 4

1 Answers1

1

Your code is kind of sloppy.

For example:

  • you define a PdfPTable with 3 columns, but you only add a single cell. That table will never be rendered.
  • you define a Phrase with name phrase, but later in your code you use p.add(...). There is no variable with name p in your code.
  • ...

This lack of respect for the StackOverflow reader can result in not getting an answer, because you are expecting the reader not only to fix the actual problem –not being able to use English and Arabic text in a single PdfPCell—, but also to fix all the other (avoidable) errors in your code.

This is a working example:

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.getInstance(document, new FileOutputStream(dest));
    document.open();
    Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    PdfPTable table = new PdfPTable(1);
    Phrase phrase = new Phrase();
    Chunk chunk = new Chunk("test value");
    phrase.add(chunk);
    phrase.add(new Chunk(ARABIC, f));
    PdfPCell cell = new PdfPCell(phrase);
    cell.setUseDescender(true);
    cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
    table.addCell(cell);
    document.add(table);
    document.close();
}

The result looks like this:

enter image description here

As you can see, both the English and the Arabic text can be read fine. You may be surprised by the alignment and the order of the text. As we are working in the Right-to-Left writing system, left and right are switched. By default, text is left aligned, but as soon as we introduce the RTL run direction, this changes to right aligned.

In your code, you add the English text first, followed by the Arabic text. Text in Arabic is read from right to left. That's why you see the English text to the right, and why the Arabic text is added to the left of the English text.

All of this has been improved in iText 7. iText 7 has an extra pdfCalligraph module that takes care of other writing systems in a transparent way.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Bruno for your quick response and apologies for the issues in my code shared. I mixed my code with the sample in iText page. In the example using ColumnText, the direction given is canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR); Is there a way to use similar for this case. I need English in left side and Arabic on riight side. Also pdfCalligraph is a paid version ? Couldnt download iText 7 using Maven.Checking on that. I am planning to use separate cells for English and Arabic if nothing helps me.. – Sha Aug 05 '16 at 05:18
  • If you want English on the left side and Arabic on the right side with iText 5, you need to switch the order in which you add the content. pdfCalligraph is closed source; you can get a trial version for 30 days. I downloaded iText 7 from Maven without any problems: [installing iText 7](http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/installing-itext-7). Installing pdfCalligraph: search for pdfCalligraph on [this page](http://developers.itextpdf.com/content/itext-7-building-blocks/chapter-2-working-rootelement). – Bruno Lowagie Aug 05 '16 at 06:27