Sumit
Sumit

Reputation: 411

itext android hindi pdf creation

I am using itextg lib for creation of pdf on my android device in hindi. (PDF is getting created perfectly in English).

Following is my code-

Gradle-

compile 'com.itextpdf:itextg:5.5.10'

Java code-

final String FONT = "file:///android_asset/FreeSans.ttf";
Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
String outputString= "\u0915\u093e\u0930 \u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917";
document.add(new Paragraph(outputString,f));

Output string is taken from itext documentation here- http://developers.itextpdf.com/examples/font-examples-itext5/language-specific-examples

Hindi text is not getting printed on pdf. So the line in outputString is not getting printed on pdf file hence created.

Thanks in Advance

Upvotes: 2

Views: 2497

Answers (3)

Chinki Sai
Chinki Sai

Reputation: 251

We can use Arial unicode for multiple language support in iTextPDF

I. Copy window fonts from "control Panel" -> fonts -> Arial,Arial Unicode MS Regular and Arial Rounded MT Bold -> make a folder "fonts" and Paste all fonts in fonts folder.

Now we can keep this folder into assets folder in Android or keep in internal storage of your device(mobile)

  1. using assest :

    BaseFont baseFont = BaseFont.createFont("assets/unicode/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

    Font catFont = new Font(baseFont , 12);

    [Note : - this will make ur .apk file heavy.]

  2. using Internal Stroge :

    File f = new File(Environment.getExternalStorageDirectory() + "/" + "fonts"); String fontFile = f.getAbsolutePath() + File.separator + "arialuni.ttf";

    BaseFont baseFont= BaseFont.createFont(fontFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

    Font catFont = new Font(baseFont, 12);

And finally use catFont wherever you want to use in your PDF file.

Upvotes: 2

sumsanny
sumsanny

Reputation: 9

First, put the your FreeSans.ttf file in the assets forlder. iText library takes the file path of the font file so you cannot just give the assets path to it since you cannot directly access assets folder. Following is the code to copy your font file from the assets folder to your app's local directory.

public static final String HINDI_FONT = "FreeSans.ttf";
try {
    File file = new File(getFilesDir(), HINDI_FONT);
    if (file.length() == 0) {
      InputStream fs = getAssets().open(HINDI_FONT);
      FileOutputStream os = new FileOutputStream(file);
      int i;
      while ((i = fs.read()) != -1) {
        os.write(i);
      }
      os.flush();
      os.close();
      fs.close();
    }
} catch (IOException e) {
   e.printStackTrace();
}

Execute the try block code in a separate thread, do not do this in the main thread. The if statement insures that this happens only once. Please write the stream close statements in finally block instead of try block as shown above.

Next is the itext statements to use the font

Font f = FontFactory.getFont(getFilesDir() + "/" + HINDI_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfPCell eCell = new PdfPCell(new Phrase(entry, f));
eCell.setPadding(5);
table.addCell(eCell);

Here, the varaible entry is your hindi text and the code is written in an Activity so getFilesDir() works. I have added a phrase in a table cell. You can use it similarly in a paragraph as well. I have checked this code and it works.

Upvotes: 0

blagae
blagae

Reputation: 2392

The way I see it, you have 2 problems:

  1. My guess is that your Android file system cannot locate the font. An easy test for this is to check the output of font.getFamilyname() which will probably be unknown. That means that the font hasn't been found and that iText has defaulted to Helvetica. Helvetica does not contain information for Indic alphabets.

  2. Another problem is that you will not get correct output for this text. Especially the cluster \u0930\u094d, which is RA + halant, will come out as र्, the regular RA character + the halant sign which you may see, in correct writing, at the end of a word. The diacritic on top of the KA (as in र्क) is impossible to achieve in iText 5.

The reason for this is that most Indic alphabets, including Devanagari which you are using to write Hindi, require OpenType features (a specific capability of certain types of fonts) in order to be rendered correctly. This is only supported in iText 7 + the pdfCalligraph add-on.

Unfortunately, there is no Android port for iText 7 yet, so this second problem is as of now not solvable.

Upvotes: 4

Related Questions