私のAndroidアプリケーションでPDFを生成しています。私はiTextを使用してPDFを生成しますが、英文のみが表示されます。私はユニコードで動作するiTextのサンプルコードを見つけました。私は単純なcomsole Javaアプリケーションでこのサンプルコードを試してみましたが、うまくいきました。これはコードです:私はアンドロイドの活動のためにこのコードを適応した場合AndroidでiTextで生成されたキリル文字が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 {}
}
問題は、I場合は、ファイルcomic.ttfの場所ではなく、パスを間違ったものに変更します。私はIOExceptionを取得します。問題は、それ自体、私はfont1
せずにこのコードを使用している場合、それはSDカード上のPDFファイルを生成するので、PDFの生成にはありませんが、それは、Unicode文字なしです:
document1.add(new Paragraph(text1));
document1.add(new Paragraph(text2));
document1.add(new Paragraph(text3));
何が問題になることができますか?
まあ、解決策を見つけました。正しいコードは次のとおりです。BaseFont bfComic = BaseFont.createFont( "assets/fonts/comic.ttf"、 "Cp1251"、BaseFont.EMBEDDED); – Georgi
解決策が見つかった場合は、自分の質問に答えてください(コメントとして追加するのではなく)。 – Ian
どの標準フォントでも動作しますか?私は "assets/fonts/comic.ttf"を見つけられません –