Reputation: 668
I tried to convert a text to PDF in Android, using iText (here ), but it gives the "File not found" exception. Here is the code:
try
{
PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
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();
}
Would you please help me? Thanks
Upvotes: 1
Views: 8114
Reputation: 438
This code work for me... Try this
try {
String path = Environment.getExternalStorageDirectory()+"/hello/";
File file = new File(path+"hello.pdf");
if(!file.exists()){
file.getParentFile().mkdirs();
try {
file.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block e.printStackTrace(); }
}
}
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory()
+File.separator
+"hello" //folder name
+File.separator
+"hello.pdf"));
document.open();
document.add(new Paragraph("Hello World "+txt.getText() ));
document.add(new Paragraph("Hello World" +txt.getText()));
document.add(new Paragraph("Hello World" +txt.getText()));
document.add(new Paragraph("Hello World "+txt.getText()));
document.close();
Log.d("OK", "done");
Upvotes: 1
Reputation: 109257
This works perfect in my case,
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory() + "/hello.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
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();
}
And in manifest file,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Upvotes: 6