我在我的android应用程序中生成PDF。我使用iText并生成PDF,但只显示英文字母。我找到了使用unicode的iText示例代码。我在一个简单的comsole java应用程序中尝试了这个示例代码,它工作正常。这是代码:用iText在Android中生成的PDF中不显示的西里尔字母
* --> 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());
}
}
}
当我适合的机器人活动的代码,它停止工作:
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 {}
}
的问题是不是与文件comic.ttf的位置,因为如果我改变路径到一个错误的我得到一个IOException。这个问题是不是与PDF本身,因为如果我用这个代码,而无需font1,它生成的SD卡上的PDF文件生成,但它是没有Unicode字符:
document1.add(new Paragraph(text1));
document1.add(new Paragraph(text2));
document1.add(new Paragraph(text3));
可以采取什么问题吗?
2011-07-28
Georgi
+1
嗯,我找到了解决方案。正确的代码是这样的:BaseFont bfComic = BaseFont.createFont(“assets/fonts/comic.ttf”,“Cp1251”,BaseFont.EMBEDDED); –
+2
如果您找到了解决方案,您可以回答您自己的问题(而不是将其添加为评论)。 –
+0
它可以与任何标准字体一起使用吗?我没有找到“assets/fonts/comic.ttf” –