Generate PDF in Android without any 3rd party Library

大城市里の小女人 提交于 2020-01-03 02:28:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!