如何将PDF文件转换为位图图像



我找不到正确的源代码。当我下载和添加源代码时,我收到错误,例如类找不到创建类。当然,我也在库中添加了一些 jar。请为此提供一些适当且有用的代码。

要将pdf转换为图像,请使用android-pdfview和以下代码

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

最新更新