【问题标题】:how to email pdf created with itext如何通过电子邮件发送使用 itext 创建的 pdf
【发布时间】:2015-09-25 20:03:11
【问题描述】:

我刚刚开始在我正在开发的应用程序中使用文本来加深我的 Android 知识。但是,我无法弄清楚如何获取已填充的 pdf,然后使用意图通过电子邮件发送。我一直在到处搜索和研究,但找不到任何东西。有谁知道怎么做?

在我的 onCreate 之前声明我的文件;

File file = new File(getExternalFilesDir(null),"pvgform.pdf");

这是我的代码的一部分,用于向可填写的 pdf 文本添加信息

OutputStream output = null;
try {
    output = new FileOutputStream(file);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
try {
    reader = new PdfReader(getResources().openRawResource(R.raw.form));
} catch (IOException e) {
    e.printStackTrace();
}
try {
    PdfStamper stamper = new PdfStamper(reader, output);
    AcroFields acroFields = stamper.getAcroFields();
    acroFields.setField("fullname", editText.getText().toString());
    acroFields.setField("agedob", editText2.getText().toString());
    acroFields.setField("description", editText3.getText().toString() + editText4.getText().toString() + editText5.getText().toString());
    acroFields.setField("duration", editText6.getText().toString());
    acroFields.setField("brandname", editText8.getText().toString());
    acroFields.setField("genericname", editText9.getText().toString());
    stamper.setFormFlattening(true);
    stamper.close();
} catch (DocumentException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
reader.close();

这是我对 pdf 表单的电子邮件意图的尝试:

Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, "My form");
email.putExtra(Intent.EXTRA_TEXT, "Here is a form");
Log.d("file",file.getAbsolutePath());
Uri uri = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM,uri);
email.setType("application/pdf");
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

当我运行此程序时,要发送的电子邮件没有附件。

【问题讨论】:

    标签: android email pdf itext pdfstamper


    【解决方案1】:

    这是我的代码的一部分,用于向可填写的 pdf 文本添加信息

    您似乎没有将 PDF 写入文件。

    这是我对 pdf 表单的电子邮件意图的尝试:

    Uri uri=Uri.parse("file://"+ "form.pdf");
    

    这在任何平台上都不是有效的Uri

    将您的 PDF 写入文件,例如 external storage。然后,使用解析为该文件的Uri,使用Uri.fromFile() 将您的File 对象转换为适当的Uri

    【讨论】:

    • 没有打开压模并添加用户输入文本,将其添加到可填充文本中?另外,我编辑了我的uri。好点了吗?
    • @Aria:“没有打开压模并添加用户输入文本,将其添加到可填充文本中吗?” ——是的,但仅限于记忆中。 “另外,我已经编辑了我的uri。更好吗?” -- 首先,我没有看到您将 PDF 保存到该文件的任何代码。其次,使用new File(getFilesDir(), "form.pdf") 更安全地构建该路径。第三,Uri 将毫无用处,因为第三方电子邮件应用程序无法从您应用程序的内部存储中读取。您可以将文件保存到getFilesDir(),但您需要使用FileProvider 以第三方应用可以访问的方式提供文件。
    • 那么你能告诉我要在我的代码中添加什么吗?对不起,我是文字初学者,所以我不知道还能尝试什么
    • @Aria:大概,而不是OutputStream output = nullOutputStream output = new FileOutputStream(yourFile); 会更好地为您服务,其中yourFile 是一个File 对象,指向您希望写入PDF 的位置。
    • @Aria:您现在似乎正在将文件写入内部存储上的某个位置。第三方电子邮件应用程序将无法阅读它。最简单的解决方法是将getFilesDir() 切换为getExternalFilesDir(null)。另外,将Uri.parse("file://"+ file.getAbsolutePath()) 更改为Uri.fromFile(file)
    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2018-09-19
    • 2023-03-06
    • 2015-06-21
    • 2023-03-10
    相关资源
    最近更新 更多