Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyrillic letters not displaying in PDF generated with iText in Android

I am tring to generate a PDF in my android application. I use iText and it generates PDF but only letters in english are shown. I found example code for iText working with unicode. I tried this example code in a simple comsole java application and it worked fine. This is the code:

* --> Copyright 2001 by Paulo Soares, Bruno Lowagie <--
public class Chap0903 {
   public static void main(String[] args) {
      System.out.println("Chapter 9 example 3: True Types (embedded)");
      Document document1 = new Document();

      try {
         PdfWriter.getInstance(document1,
           new FileOutputStream("c:\\Chap0903.pdf"));

         BaseFont bfComic = BaseFont.createFont("assets/fonts/comic.ttf",
           BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
         Font font1 = new Font(bfComic, 12);
         String text1 = "This is the quite popular True Type font 'Comic'.";
         String text2 = "Some greek characters: \u0393\u0394\u03b6";
         String text3 = "Some cyrillic characters: \u0418\u044f";

         document1.open();
         document1.add(new Paragraph(text1,font1));
         document1.add(new Paragraph(text2,font1));
         document1.add(new Paragraph(text3,font1)); 
         document1.close();
     }
     catch(DocumentException de) {
        document1.close();
        System.err.println(de.getMessage());
     }
     catch(IOException ioe) {
        document1.close();
        System.err.println(ioe.getMessage());
     }
  }
}

When I adapted this code for an android activity, it stopped to work:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String root = Environment.getExternalStorageDirectory().getAbsolutePath();
    Document document1 = new Document();

    try {
        PdfWriter.getInstance(document1,
          new FileOutputStream(root+"/Chap0903.pdf"));

        BaseFont bfComic = BaseFont.createFont("assets/fonts/comic.ttf",
          BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font1 = new Font(bfComic, 12);
        String text1 = "This is the quite popular True Type font 'Comic'.";
        String text2 = "Some greek characters: \u0393\u0394\u03b6";
        String text3 = "Some cyrillic characters: \u0418\u044f";

        document1.open();
        document1.add(new Paragraph(text1,font1));
        document1.add(new Paragraph(text2,font1));
        document1.add(new Paragraph(text3,font1)); 
        document1.close();

        Intent intent = new Intent();
        setResult(RESULT_OK, intent);       
    } 
    catch(DocumentException de) {
        document1.close();
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);        
        System.err.println(de.getMessage());
    }
    catch(IOException ioe) {
        document1.close();
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        System.err.println(ioe.getMessage());
        Task.mes(ioe.getMessage());
    } finally {}
}

The problem is not with the location of the file comic.ttf because if I change the path to a wrong one I get an IOException. The problem is not with the generation of the PDF itself because if I use this code without font1, it generates a PDF file on the SD card but it is without the Unicode characters:

document1.add(new Paragraph(text1));
document1.add(new Paragraph(text2));
document1.add(new Paragraph(text3)); 

What can be the problem ?

like image 350
Georgi Avatar asked Jul 28 '11 12:07

Georgi


1 Answers

I used

BaseFont bfComic = BaseFont.createFont("/system/fonts/Comic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

and it works. You should check the fonts directory on your Android device to make sure.

For Chinese characters I used

BaseFont bfSans = BaseFont.createFont("/system/fonts/DroidSansFallback.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

Make sure you provide a fallback font in the catch() block, for example:

font = new Font(Font.FontFamily.HELVETICA, 24, Font.NORMAL, BaseColor.BLACK);

Please let me know if there is a standard way to get the path of the fonts folder.

like image 138
Li_W Avatar answered Nov 06 '22 05:11

Li_W