public void savePDF(View view) {
//create document object
Document doc = new Document();
//output file path
String outpath= Environment.getExternalStorageDirectory()+"/mypdf.pdf";
try {
//create pdf writer instance
PdfWriter.getInstance(doc, new FileOutputStream(outpath));
//open the document for writing
doc.open();
//add paragraph to the document
doc.add(new Paragraph("bob"));
//close the document
doc.close();
Toast.makeText(this, "Worked", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am using the above code to create a PDF file and store it on the external storage of the device. Below is a snippet of my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.mobileappdevelopment">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
Upon pressing the button (which runs the savePDF function), nothing happens. No file is saved, and the toast isn't shown either. Am I doing something wrong here?
Also added the itext dependency...
implementation 'com.itextpdf:itextg:5.5.10'