在Android中生成PDF,无需任何第三方库 [英] Generate PDF in Android without any 3rd party Library

查看:143
本文介绍了在Android中生成PDF,无需任何第三方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用API​​ 19中引入的 PdfDocument 类从我的应用程序生成PDF。我不想使用任何第三方库。

以下是我的工作

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?

推荐答案

我有简单的解决方案,它也有效.........

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) {}

这篇关于在Android中生成PDF,无需任何第三方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆