Geo*_*rgi 6 unicode android itext internationalization
我想在我的Android应用程序中生成PDF.我使用iText并生成PDF,但只显示英文字母.我找到了使用unicode的iText的示例代码.我在一个简单的comsole java应用程序中尝试了这个示例代码,它运行正常.这是代码:
* --> 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我为Android活动调整此代码时,它停止工作:
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 {}
}
Run Code Online (Sandbox Code Playgroud)
问题不在于文件comic.ttf的位置,因为如果我将路径更改为错误的路径,我会收到IOException.问题不在于PDF本身的生成,因为如果我不使用此代码font1,它会在SD卡上生成PDF文件,但它没有Unicode字符:
document1.add(new Paragraph(text1));
document1.add(new Paragraph(text2));
document1.add(new Paragraph(text3));
Run Code Online (Sandbox Code Playgroud)
可能是什么问题?
我用了
BaseFont bfComic = BaseFont.createFont("/system/fonts/Comic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Run Code Online (Sandbox Code Playgroud)
它有效。您应该检查 Android 设备上的字体目录来确定。
对于我使用的汉字
BaseFont bfSans = BaseFont.createFont("/system/fonts/DroidSansFallback.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Run Code Online (Sandbox Code Playgroud)
确保在 catch() 块中提供后备字体,例如:
font = new Font(Font.FontFamily.HELVETICA, 24, Font.NORMAL, BaseColor.BLACK);
Run Code Online (Sandbox Code Playgroud)
请告诉我是否有获取字体文件夹路径的标准方法。
| 归档时间: |
|
| 查看次数: |
8341 次 |
| 最近记录: |