java - 在Android中生成PDF,无需任何第三方库

标签 java android pdf pdf-generation

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

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();

有人可以建议我做错了什么吗?

最佳答案

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

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

关于java - 在Android中生成PDF,无需任何第三方库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392664/

相关文章:

c# - 在 C# 中将 PDF 文件转换为 HTML

java - 如何更新 MongoDB 中特定文档的数组内特定嵌入文档的值?

java - 如何在接口(interface)中使用静态方法来返回实例方法返回的泛型实例?

java - 如何垂直打印优先队列

android - 在 Android Studio 中显示打开选项卡的列表

pdf - COS代表什么?

java - 如何获取 JConsole 中显示的 VM 信息?

java - 无法在 RealmObject 子类中创建 ArrayList

java - React-Native DisconnectableInputStream java异常

javascript - 在 Chrome IOS 上加载 PDF 时遇到问题