1

I am using Itext to create pdf files in android using below code:

 Document document = new Document();
                PdfWriter.getInstance(document, new FileOutputStream(root.getPath() + "/" + System.currentTimeMillis() + ".pdf"));
                document.open();
                Font chapterFont = FontFactory.getFont("", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);
                Font paragraphFont = FontFactory.getFont("", BaseFont.IDENTITY_H, 12, Font.NORMAL);

                Chunk chunk = new Chunk(b.getTitle(), chapterFont);

                Chapter chapter = new Chapter(new Paragraph(chunk), 1);
                chapter.setNumberDepth(0);
                chapter.add(new Paragraph(b.getLead(), paragraphFont));
                document.add(chapter);
                document.close();

when i use english words it works fine, but when i use arabic or persian words it shows empty lines,

what is the problem? and how can i solve it? thanks in advance,

Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54

3 Answers3

2

step 1 - add font:

Font font= FontFactory.getFont("assets/arial.ttf", BaseFont.IDENTITY_H, 16, Font.NORMAL);

step 2 - add table (in my case) :

PdfPTable table = new PdfPTable(4);
        table.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);

        table.addCell(new PdfPCell(new Paragraph("بستانکار",font)));

and for RTL use :

setRunDirection(PdfWriter.RUN_DIRECTION_RTL) .

you can use this method in PdfWriter too;

ali koohi
  • 446
  • 3
  • 10
1

This problem fixed by changing this line:

Font chapterFont = FontFactory.getFont("", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);

to

Font chapterFont = FontFactory.getFont("assets/arial.ttf", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);
Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54
0

It's probably a encoding issue, Make sure the default enconding is set to UTF-8.

Remy
  • 1,053
  • 1
  • 19
  • 25
  • Alright, check what's it default encoding, then write the chapter with the right encoding... look here http://stackoverflow.com/questions/28564230/encoding-of-umlauts-with-the-android-pdf-writer-lib – Remy Aug 05 '16 at 16:18
  • The file is a binary file. Thus, no encoding for the `FileOutputStream`. – mkl Aug 05 '16 at 19:43
  • @mkl You right, the stream output file shall not be changed, the local default shall be. I need forgot to update it. Anyway, it could also be a font problem as another answers shows. – Remy Aug 06 '16 at 18:19