以下のコードと iText ライブラリを使用して、pdf を動的に作成しました。
try {
String str_path = Environment.getExternalStorageDirectory()
.toString();
File file;
file = new File(str_path, getString(R.string.app_name)
+ ".pdf");
FileOutputStream fos = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
document.add(new Paragraph("Hello World"));
document.add(new Paragraph(new Date().toString()));
document.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
976 バイトの pdf ファイルを取得します。以下のコードを使用して、このpdfファイルに画像を追加するためのコードを追加したとき:
try {
String str_path = Environment.getExternalStorageDirectory()
.toString();
File file;
file = new File(str_path, getString(R.string.app_name)
+ ".pdf");
FileOutputStream fos = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
document.add(new Paragraph("Hello World, iText"));
document.add(new Paragraph(new Date().toString()));
Image image = Image.getInstance("ic_launcher.png");
document.add(image);
document.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
私は0バイトのpdfファイルを取得します。
何が問題なのかわかりません。それに関連するアイデアがあれば、私と共有してください。ありがとう。