所有我需要做的就是把一个(本地保存)PDF-document和转换一个或所有它的网页图像的像JPG或PNG格式.
我已经尝试了许多PDF渲染/查看解决方案,如APV PDF Viewer,APDFViewer,droidreader,android-pdf,MuPdf等等,但到目前为止还无法弄清楚如何将pdf页面转换为图像?.
编辑:此外,我宁愿拥有PDF到图像转换器而不是我需要编辑以将PDF转换为图像的PDF渲染器.
您需要查看这个开源项目以获得相同的要求,这对您做更多的事情也很有帮助.
项目:PdfRenderer
有一个名为Java类PDFPage.java的pdfview包.该类有一个方法来获取页面的图像.
我在我的测试项目中也实现了同样的东西,java代码就在这里.我创建了一个方法showPage,它接受页面号和缩放级别并返回该页面Bitmap.
希望这可以帮到你.您只需要获得该项目或JAR,阅读记录良好的JAVADOC,然后尝试和实现相同.
慢慢来,快乐编码:)
要支持API 8及更高版本,请按照:
使用此库: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);
}
}
Run Code Online (Sandbox Code Playgroud)
你应该在后台完成这项工作,即使用AsyncTask类似的东西,因为很多方法需要计算或IO时间(我在评论中标记了它们).
使用 lib https://github.com/barteksc/PdfiumAndroid
public Bitmap getBitmap(File file){
int pageNum = 0;
PdfiumCore pdfiumCore = new PdfiumCore(context);
try {
PdfDocument pdfDocument = pdfiumCore.newDocument(openFile(file));
pdfiumCore.openPage(pdfDocument, pageNum);
int width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNum);
int height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNum);
// ARGB_8888 - best quality, high memory usage, higher possibility of OutOfMemoryError
// RGB_565 - little worse quality, twice less memory usage
Bitmap bitmap = Bitmap.createBitmap(width , height ,
Bitmap.Config.RGB_565);
pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageNum, 0, 0,
width, height);
//if you need to render annotations and form fields, you can use
//the same method above adding 'true' as last param
pdfiumCore.closeDocument(pdfDocument); // important!
return bitmap;
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
public static ParcelFileDescriptor openFile(File file) {
ParcelFileDescriptor descriptor;
try {
descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return descriptor;
}
Run Code Online (Sandbox Code Playgroud)