0

I am trying to create a PDF file and put it in the SD Card I downloaded the library iText to do this and I imported to my project but there still have a problem in this line :

import com.itextpdf.text.Document;

It tells me that com.itextpdf.text.Document collides with another import statement there is my code :

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

I am not able to find the problem with this.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
hayfa
  • 89
  • 1
  • 1
  • 4
  • 1
    Post full activity code..including imports – Alok Nair Aug 13 '14 at 11:47
  • Did you build the library file properly.for save as pdf file refer [this](http://stackoverflow.com/questions/16117085/how-to-save-the-layout-view-as-image-or-pdf-to-sd-card-in-android) – Yugesh Aug 13 '14 at 12:01
  • I just posted the imports that T have I can't find why there is a probléme with import com.itextpdf.text.Document; I added itext library but it still tell me that "com.itextpdf.text.document collides with another import statement " I can't no where is the problem – hayfa Aug 13 '14 at 14:30
  • "I just posted the imports that T have" >> where ? – 2Dee Aug 14 '14 at 07:46
  • import android.provider.DocumentsContract.Document;import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; – hayfa Aug 15 '14 at 08:11

2 Answers2

0

Use this to retrieve the FileOutputStream, works for me:

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);
}
0101100101
  • 5,786
  • 6
  • 31
  • 55
0

Make sure you added

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

in your AndroidManifest.xml file.

Metaphore
  • 749
  • 1
  • 7
  • 15