【发布时间】:2014-05-13 09:33:30
【问题描述】:
我想使用 KitKat 中引入的PdfDocument android 类从视图生成 PDF 文件。我设法做到了,到目前为止,文件生成正常,最终得到了正确的 PDF。唯一的问题是文件很大,只有一页12Mb。有没有办法减小文件大小?
我用来生成 PDF 的代码是:
public static File generateDocument(Activity activity, String fileName, ViewGroup container) throws IOException{
File f = new File(activity.getExternalFilesDir(null), fileName);
PdfDocument document = new PdfDocument();
try{
for(int i=0;i<container.getChildCount();i++){
View v = container.getChildAt(i);
PdfDocument.PageInfo.Builder pageBuilder = new PdfDocument.PageInfo.Builder(v.getWidth(), v.getHeight(), i);
Page page = document.startPage(pageBuilder.create());
v.draw(page.getCanvas());
document.finishPage(page);
}
document.writeTo(new FileOutputStream(f));
} finally{
if(document!=null){
document.close();
}
}
return f;
}
【问题讨论】:
-
你有没有找到任何使用原生android API的解决方案
-
请阅读我在已接受答案中的评论。在生成 PDF 之前,已解决在 ImageViews 中调整位图大小的问题。
标签: android pdf-generation filesize