问题
I need to generate PDF from my application using PdfDocument
class introduced in API 19. I don't want to use any 3rd party library.
Here is what I have done
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 300, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
View content = findViewById(R.id.testText);
content.draw(page.getCanvas());
document.finishPage(page);
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/AppName";
File dir = new File(fullPath);
File file = new File(fullPath, "TripReport.PDF");
if (!dir.exists())
dir.mkdirs();
if (file.exists())
file.delete();
file.createNewFile();
FileOutputStream os = new FileOutputStream(file);
document.writeTo(os);
document.close();
os.flush();
os.close();
Can anyone please suggest what I am doing wrong?
回答1:
I have simple solution and it also worked.........
try{
File docfile=new File(""+getExternalFilesDir("parent dir"),"filename.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(docfile), "application/pdf");
startActivity(intent);
}
catch (Exception e) {}
来源:https://stackoverflow.com/questions/25392664/generate-pdf-in-android-without-any-3rd-party-library