在Android中的SD卡中创建一个pdf文件

时间:2014-08-13 11:46:27

标签: android pdf itext

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

import com.itextpdf.text.Document;

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

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

我无法找到问题。

2 个答案:

答案 0 :(得分:0)

使用它来检索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);
}

答案 1 :(得分:0)

确保添加了

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

在AndroidManifest.xml文件中。