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

标签 android pdf

我尝试使用 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();
        }

你能帮帮我吗?谢谢

最佳答案

这对我来说很完美,

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();
    }

在 list 文件中,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

关于android - 使用 iText 在 Android 中将文本转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10595138/

相关文章:

具有垂直分页的android pdf查看器

android - Facebook 安卓 : "Security Error: There are problems with the security certificate for this site"

android - 如何使用 RxJava 逃离这个回调 hell

java - 批量重命名PDF文件中的PDF书签/大纲

android - 来自 FragmentActivity 的 fragment 在一些设备上不显示背景图像

java - 如何在 Activity 类中启用 xml Button?

java - 如何在使用 itext 7 API 生成的 PDF 中显示来自 RTL 的阿拉伯语字符串?

delphi - 将 OpenOffice Calc 文件导出为 PDF 时如何尊重打印区域?

android - Android Activity 中保存的实例状态未保留的保留 fragment

pdf - 动态生成的 PDF 文件适用于除 Adob​​e Reader 之外的大多数阅读器