【问题标题】:create a pdf file in the SD card in Android在 Android 的 SD 卡中创建一个 pdf 文件
【发布时间】:2014-08-14 15:42:45
【问题描述】:

我正在尝试创建一个 PDF 文件并将其放入 SD 卡中我下载了 iText 库来执行此操作并导入到我的项目中,但这一行仍然存在问题:

import com.itextpdf.text.Document;

它告诉我com.itextpdf.text.Document 与另一个导入语句冲突 有我的代码:

String loan_principal = rslt_loan_principal.getText().toString();
String dsr = rslt_dsr.getText().toString();
String flat_rate = rslt_flat_rate.getText().toString();
String ins_amount = rslt_installement_amount.getText().toString();

try
{
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() + "/HomeFinance.pdf"));
    document.open();
    document.add(new Paragraph("Loan Principal : "+String.valueOf(loan_principal)));
    document.add(new Paragraph("DSR : "+String.valueOf(dsr)+ "%"));
    document.add(new Paragraph("Flat Rate  : "+String.valueOf(flat_rate)+ "%"));
    document.add(new Paragraph("Installment Amount : "+String.valueOf(ins_amount)+ "%"));
    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();
}

我找不到这个问题。

【问题讨论】:

  • 发布完整的活动代码..包括进口
  • 您是否正确构建了库文件。保存为pdf文件请参阅this
  • 我刚刚发布了 T 的导入我找不到为什么 import com.itextpdf.text.Document 有问题;我添加了 itext 库,但它仍然告诉我“com.itextpdf.text.document 与另一个导入语句冲突”我不知道问题出在哪里
  • “我刚刚发布了 T 的导入”>> 在哪里?
  • import android.provider.DocumentsContract.Document;import com.itextpdf.text.Document;导入 com.itextpdf.text.DocumentException;导入com.itextpdf.text.Paragraph;导入com.itextpdf.text.pdf.PdfWriter;

标签: android pdf itext


【解决方案1】:

使用它来检索 FileOutputStream,对我有用:

File folder = new File(Environment.getExternalStorageDirectory()
    + File.separator + <your package name here>);
folder.mkdirs();
File file;
try {
    file = new File(folder, "HomeFinance.pdf");
    file.createNewFile();
} catch (IOException e) {
    // handle exception
}
FileOutputStream fos;
try {
    fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
    throw new RuntimeException(e);
}

【讨论】:

  • 我试过了,但我有 FileNotFoundException 你能帮帮我吗!
  • IOException也抛出了吗?
  • 我有一条消息告诉我它是 FileNotFoundException 没有 IOException !
  • 它还说“权限被拒绝”
  • 你有Metaphore提到的权限集吗?
【解决方案2】:

确保你添加了

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

在您的 AndroidManifest.xml 文件中。

【讨论】:

  • 我用过但还是有同样的问题!!
猜你喜欢
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 2016-04-18
  • 2023-03-13
相关资源
最近更新 更多