PDFBox would be your best option considering how easy it is to use. You can have a look at the examples and get going immediately..
EDIT : As @Adinia pointed it out, PDFBox is not working on Android. The reason being PDFBox uses AWT and Swing even for the non-ui related tasks and Android does not support these.
You could have a go at PDFjet and here are some examples to get you started.
EDIT 2: PDFBox-Android (as the name suggests) is now available for Android, as @Theo was so kind to inform.
Answer from Swayam on Stack OverflowVideos
So apparently the code I was using wasn't compatible with android, hence the error I was getting. Below you'll find the correct code that actually works right (for creating a pdf file, putting some content in it, saving in and the opening the newly created file):
PS: For this you'll need to add the jar of iTextG to your project:
// Method for creating a pdf file from text, saving it then opening it for display
public void createandDisplayPdf(String text) {
Document doc = new Document();
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "newFile.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
Paragraph p1 = new Paragraph(text);
Font paraFont= new Font(Font.COURIER);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
//add paragraph to document
doc.add(p1);
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}
finally {
doc.close();
}
viewPdf("newFile.pdf", "Dir");
}
// Method for opening a pdf file
private void viewPdf(String file, String directory) {
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file);
Uri path = Uri.fromFile(pdfFile);
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(TableActivity.this, "No application has been found to open PDF files.", Toast.LENGTH_SHORT).show();
}
}
PdfDocument class enables generating a PDF document from native Android content. By using this class we can create pdf and also open it by using PdfRenderer. Sample code for creating a pdf file
public void stringtopdf(String data) {
String extstoragedir = Environment.getExternalStorageDirectory().toString();
File fol = new File(extstoragedir, "pdf");
File folder=new File(fol,"pdf");
if(!folder.exists()) {
boolean bool = folder.mkdir();
}
try {
final File file = new File(folder, "sample.pdf");
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new
PdfDocument.PageInfo.Builder(100, 100, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawText(data, 10, 10, paint);
document.finishPage(page);
document.writeTo(fOut);
document.close();
}catch (IOException e){
Log.i("error",e.getLocalizedMessage());
}