如何在Android中将PDF页面转换为图像?

Pie*_*888 22 java pdf android

所有我需要做的就是把一个(本地保存)PDF-document转换一个或所有它的网页图像的像JPG或PNG格式.

我已经尝试了许多PDF渲染/查看解决方案,如APV PDF Viewer,APDFViewer,droidreader,android-pdf,MuPdf等等,但到目前为止还无法弄清楚如何将pdf页面转换为图像?.

编辑:此外,我宁愿拥有PDF到图像转换器而不是我需要编辑以将PDF转换为图像的PDF渲染器.

MKJ*_*ekh 9

您需要查看这个开源项目以获得相同的要求,这对您做更多的事情也很有帮助.

项目:PdfRenderer

有一个名为Java类PDFPage.javapdfview包.该类有一个方法来获取页面的图像.

我在我的测试项目中也实现了同样的东西,java代码就在这里.我创建了一个方法showPage,它接受页面号和缩放级别并返回该页面Bitmap.

希望这可以帮到你.您只需要获得该项目或JAR,阅读记录良好的JAVADOC,然后尝试和实现相同.

慢慢来,快乐编码:)

  • 我正在尝试使用你制作的showPage方法.但是我在你发布的pastebin的第93行遇到了麻烦.显然``PDFPage`试图使用Android不支持的来自`java.awt.geom`的一些类(`Rectangle2D`,`ImageObserver`,`Image`).你是怎么做到的? (2认同)

ved*_*ant 9

要支持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时间(我在评论中标记了它们).


bir*_*rdy 6

从Android API 21开始,您正在寻找PdfRenderer.

  • 如何支持最低14的API版本? (2认同)

Nic*_*hek 5

使用 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)