Java 用于将pdf转换为图像的库
我有一个Android应用程序,我想在其中显示一个PDF(没有外部应用程序)。我认为唯一的解决方案是将PDF页面转换为图像。有人有这方面的经验吗?你推荐我去哪个图书馆 我测试了下一个库,但遇到了问题: 1) 不适用于现代PDFJava 用于将pdf转换为图像的库,java,android,image,pdf,Java,Android,Image,Pdf,我有一个Android应用程序,我想在其中显示一个PDF(没有外部应用程序)。我认为唯一的解决方案是将PDF页面转换为图像。有人有这方面的经验吗?你推荐我去哪个图书馆 我测试了下一个库,但遇到了问题: 1) 不适用于现代PDF 2) jPDFImages不适用于Android(适用于java桌面应用程序) 对不起,我说的是英语你可以使用MuPDF。这里有一个描述如何为android构建MuPDF的链接:您可以使用MuPDF。下面是一个描述如何为android构建MuPDF的链接:我建议使用此库,
2) jPDFImages不适用于Android(适用于java桌面应用程序)
对不起,我说的是英语你可以使用MuPDF。这里有一个描述如何为android构建MuPDF的链接:您可以使用MuPDF。下面是一个描述如何为android构建MuPDF的链接:我建议使用此库,我建议使用此库,我正在扩展已接受的答案并提供完整的解决方案 使用此库和以下代码,您可以可靠地将PDF页面转换为图像(JPG、PNG):
DecodeServiceBase decodeService=new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(mContext.getContentResolver());
//跑得有点长
decodeService.open(Uri.fromFile(pdf));
int pageCount=decodeService.getPageCount();
for(int i=0;i
您应该在后台完成这项工作,即使用
异步任务
或类似的方法,因为许多方法需要计算或IO时间(我已在注释中标记了它们)。我正在扩展已接受的答案,并提供完整的解决方案
使用此库和以下代码,您可以可靠地将PDF页面转换为图像(JPG、PNG):
DecodeServiceBase decodeService=new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(mContext.getContentResolver());
//跑得有点长
decodeService.open(Uri.fromFile(pdf));
int pageCount=decodeService.getPageCount();
for(int i=0;i
您应该在后台完成这项工作,即使用
AsyncTask
或类似的方法,因为许多方法需要计算或IO时间(我已在注释中标记了它们)。您可以查看iText pdf,只要您的产品/服务不收费,它是免费的检查此:也检查此库:@ns47731免费版iText是根据AGPL授权的,这允许收费,尽管它还要求随它分发的代码被授权为GPL或AGPL。您可以查看iText pdf,只要您的产品/服务不收费,它是免费的检查此:也检查此库:@ns47731免费版本iText是根据AGPL授权的,这允许收费,尽管它还要求随它分发的代码被授权为GPL或AGPL。
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);
}
}