[英]create a pdf file in the SD card in Android
我正在嘗試創建PDF文件並將其放入SD卡中,我下載了iText庫來執行此操作,然后將其導入到我的項目中,但是這一行仍然存在問題:
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();
}
我無法找到問題所在。
使用它來檢索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);
}
確保您添加了
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在您的AndroidManifest.xml文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.