2

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?

99maas
  • 1,239
  • 12
  • 34
  • 59

2 Answers2

9

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.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I think you have answered this special question 1000 times or more. right? If a lot of people are asking the same question, something is wrong. make the Unicode, default. It's much more easier than answering this question every day. – VahidN Jul 11 '15 at 15:31
  • I can't make the UNICODE default. That's up to the OS that saves files, compilers that compile the code, JVMs that execute the bytecode. There are also implications in PDF: the uncompressed content stream of text when using composite fonts (e.g. when `/Identity-H` is used) takes double the space that is needed when using a simple font. I wouldn't have to answer this question every day if only developers weren't too stubborn to read [the documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). That's why there's an F in RTFM. – Bruno Lowagie Jul 11 '15 at 15:36
  • Thanks for the help and I understood the issue now – 99maas Jul 11 '15 at 17:00
  • what about iText 7 ? the above mentioned lines do not work – Mohammed Nafie Jul 26 '16 at 18:28
  • 1
    @MohammedNafie Please read [chapter 1](http://developers.itextpdf.com/content/itext-7-building-blocks/chapter-1-introducing-pdffont-class) of the "iText 7: Building Blocks" tutorial and you'll learn that iText 7 requires the closed source add-on [pdfCalligraph](http://itextpdf.com/itext7/pdfcalligraph) to render Arabic. – Bruno Lowagie Jul 27 '16 at 06:48
1

go with PdfTable will print arabic text in pdf. following is the code

Font f = FontFactory.getFont("assets/NotoNaskhArabic-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    f.setStyle(Font.BOLD);
    f.setColor(new BaseColor(context.getResources().getColor(R.color.colorPrimary)));
   PdfPTable pdfTable=new PdfPTable(1);
   pdfTable.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
   PdfPCell pdfPCell=new PdfPCell();
   pdfPCell.setBorder(Rectangle.NO_BORDER);
   Paragraph paragraph=new Paragraph(string,f);
   paragraph.setAlignment(PdfPCell.ALIGN_LEFT);
   pdfPCell.addElement(paragraph);
   pdfTable.addCell(pdfPCell);
   document.add(pdfTable);

this code will add arabic text in your pdf. pdfPCell.setBorder(Rectangle.NO_BORDER); will give paragraph view for PdfTable check output of above code

Mohd Qasim
  • 896
  • 9
  • 20