我尝试使用iText(here )在Android中将文本转换为PDF,但它提供了“找不到文件”异常。 这是代码:
try
{
PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
Log.d("OK", "done");
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (DocumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
你能帮帮我吗?感谢
答案 0 :(得分:6)
这在我的案例中非常完美,
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() + "/hello.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
Log.d("OK", "done");
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (DocumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
在清单文件中,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:1)
此代码适合我...试试这个
试 {
String path = Environment.getExternalStorageDirectory()+"/hello/";
File file = new File(path+"hello.pdf");
if(!file.exists()){
file.getParentFile().mkdirs();
try {
file.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace(); }
}
}
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory()
+File.separator
+"hello" //folder name
+File.separator
+"hello.pdf"));
document.open();
document.add(new Paragraph("Hello World "+txt.getText() ));
document.add(new Paragraph("Hello World" +txt.getText()));
document.add(new Paragraph("Hello World" +txt.getText()));
document.add(new Paragraph("Hello World "+txt.getText()));
document.close();
Log.d("OK", "done");