0
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class MainActivity extends Activity {
private static String FILE = "/HelloWorld.pdf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
</i>
try {
Document document = new Document();
boolean mExternalStorageAvailable = true;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
String file = null;
if(mExternalStorageWriteable) {
file = Environment.getExternalStorageDirectory().getPath() + FILE;
}
PdfWriter.getInstance(document,new FileOutputStream(file));
document.open();
Paragraph p = new Paragraph("Hello iText World!");
document.add(p);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我試圖生成一個更多的另一組數據和生成的文件有大小0KB,並whene打開顯示消息「ADOBE READER無法打開‘List.pdf’和鍵入或者使文件損壞「。無法讀取的iText __OKBPDF生成的PDF
你確定你不錯過記錄在logcat中的任何異常嗎?乍一看,這段代碼似乎是正確的。 – 2013-03-27 09:39:17