我有一个Android应用程序,我想显示一个PDF(没有外部应用程序)。我认为唯一的解决方案是将PDF页面转换为图像。有人有这个问题的经验?你推荐我哪个库?库将PDF转换为图像
我测试了下一个库,但我有烦恼:
1)PdfRenderer library不能与现代的PDF工作
2)jPDFImages Android中无法正常工作(工作在Java桌面应用程序)
对不起for my English
我有一个Android应用程序,我想显示一个PDF(没有外部应用程序)。我认为唯一的解决方案是将PDF页面转换为图像。有人有这个问题的经验?你推荐我哪个库?库将PDF转换为图像
我测试了下一个库,但我有烦恼:
1)PdfRenderer library不能与现代的PDF工作
2)jPDFImages Android中无法正常工作(工作在Java桌面应用程序)
对不起for my English
我建议使用这个库,android-pdfview
您可以使用MuPDF。下面是介绍如何构建MuPDF为Android链接:http://mupdf.com/docs/how-to-build-mupdf-for-android
我扩大对accpeted答案,并提供完整的解决方案。
使用这个库:android-pdfview和下面的代码,就可以可靠地转换的PDF页面转换成图片(JPG,PNG):
DecodeServiceBase decodeService = new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(mContext.getContentResolver());
// a bit long running
decodeService.open(Uri.fromFile(pdf));
int pageCount = decodeService.getPageCount();
for (int i = 0; i < pageCount; i++) {
PdfPage page = decodeService.getPage(i);
RectF rectF = new RectF(0, 0, 1, 1);
// do a fit center to 1920x1080
double scaleBy = Math.min(AndroidUtils.PHOTO_WIDTH_PIXELS/(double) page.getWidth(), //
AndroidUtils.PHOTO_HEIGHT_PIXELS/(double) page.getHeight());
int with = (int) (page.getWidth() * scaleBy);
int height = (int) (page.getHeight() * scaleBy);
// you can change these values as you to zoom in/out
// and even distort (scale without maintaining the aspect ratio)
// the resulting images
// Long running
Bitmap bitmap = page.renderBitmap(with, height, rectF);
try {
File outputFile = new File(mOutputDir, System.currentTimeMillis() + FileUtils.DOT_JPEG);
FileOutputStream outputStream = new FileOutputStream(outputFile);
// a bit long running
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (IOException e) {
LogWrapper.fatalError(e);
}
}
你应该通过使用AsyncTask
或IE在后台做这个工作类似于相当多的方法需要计算或IO时间(我在注释中标记了它们)。
你可以看看iText pdf,它是免费的,只要你不收费为你的产品/服务 –
检查此:http://stackoverflow.com/questions/2456344/display-pdf-within-app- on-android –
此外,请检查此库:http://code.google.com/p/apv/ –