使用iText在Android中将文本转换为PDF

Far*_*Ala 1 pdf android

我试图在Android中使用iText(这里)将文本转换为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();
        }
Run Code Online (Sandbox Code Playgroud)

你能帮帮我吗?谢谢

use*_*305 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();
    }
Run Code Online (Sandbox Code Playgroud)

在清单文件中,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)