PDF non-displayable Cyrillic letters created using iText in Android

I am trying to create a PDF file in my android application. I use iText and generates PDF, but only English letters are displayed. I found code example for iText working with unicode. I tried this sample code in a simple java console 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 android activity, it stopped working:

 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 the location of the comic.ttf file, because if I change the path to the wrong one, I get an IOException. The problem is not generating the PDF file itself, because if I use this code without font1 , it creates a PDF file on the SD card, but without Unicode characters:

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

What could be the problem?

+6
source share
1 answer

I used

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

and it works. You must check the font 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 font folder path.

+4
source

Source: https://habr.com/ru/post/893770/


All Articles