【问题标题】:Generate PDF in Android without any 3rd party Library在没有任何第三方库的情况下在 Android 中生成 PDF
【发布时间】:2014-10-13 02:23:14
【问题描述】:

我需要使用 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();

谁能建议我做错了什么?

【问题讨论】:

  • Can anyone please suggest what I am doing wrong? - 前提是您告诉我们出了什么问题。
  • 您想要创建一个专有的二进制文件,并且您不希望任何类型的库来帮助您。好的。祝你好运。
  • @durbnpoisn 他没有这么说。他说没有第三方图书馆。 PdfDocument 正是为此而设计的。
  • 啊。我有点认为这意味着他甚至不喜欢内置的类......(毕竟那不是一个库吗?)
  • @durbnpoisn 但它不是第 3 方 - 是...?

标签: java android pdf pdf-generation


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多