如何在安卓系统中将PDF页面转换为图像?

30

我只需要取出一个(本地保存的)PDF文档,并将其中一个或全部页面转换为图像格式,如JPG或PNG。

我尝试了很多PDF渲染/查看解决方案,如APV PDF ViewerAPDFViewerdroidreaderandroid-pdfMuPdf和许多其他解决方案,但到目前为止我还无法弄清楚如何将PDF页面转换为图像?

编辑:我宁愿使用PDF到图像转换器,而不是需要编辑才能转换PDF到图像的PDF渲染器。


1
我不是Android开发者,但是我刚刚偶然进行的网络搜索显示ImageMagick已经被移植到这个平台。或许值得一试? - halfer
http://stackoverflow.com/questions/6757434/how-to-convert-pdf-into-image - Bugs bunny
你可以找到那些能够为你完成这项工作的人(http://www.pdf-tools.com/pdf/pdf-to-image-converter-tiff.aspx),他们有自己的API,只要你有互联网连接就可以使用,如果没有……你应该去黑掉他们,因为他们做得非常好;) - Ilya Gazman
1
@AgarwalShankar,不确定您是否已经测试过这段代码。这是行不通的。为什么?*因为此代码中使用的核心类PDFImageWriter依赖于java.awt.*类**,请自行查看源代码。我希望您或投票支持此代码的人能告诉我我错了,根据我的基本知识:Java awt不受Android支持。* - yorkw
嗯,我没有测试过,但如果有人确认了,那么我会删除这个答案。 - Shankar Agarwal
显示剩余2条评论
9个回答

10

为了支持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);
    }
}

你应该在后台执行此工作,即使用AsyncTask或类似的东西,因为有相当多的方法需要计算或IO时间(我已在注释中标记)。


是的,它真的非常快,而且按预期工作。仅需要大约17秒就可以将具有大量图像的12个PDF页面转换。感谢您发布这篇伟大的文章。 - Smeet
该项目已不再维护。 - Ebin Joy
@EbinJoy 我也有类似的疑问,但它在当前形式下完美运作。只是未来可能不会有更多功能。 - Vedant Agarwala
它的运行很好,但在打开密码保护的PDF时出现异常。有什么帮助吗?@vedant1811 - Sunil Chaudhary
AndroidUtils类未找到。 - Lasitha Lakmal

9
你需要查看这个开源项目,它也可以帮助你做很多其他的事情。
项目:PdfRenderer 有一个名为pdfview包中的Java类PDFPage.java。该类有一个方法可以获取页面的图像。
我在我的测试项目中也实现了同样的功能,Java代码在此。我创建了一个方法showPage,它接受页码和缩放级别,并将该页作为Bitmap返回。
希望对你有所帮助。你只需要获得该项目或JAR,仔细阅读文档化的JAVADOC,然后尝试按照我所做的方式实现相同的功能。
慢慢来,愉快编程:)

2
我正在尝试使用您创建的showPage方法。但是我在您发布的pastebin的第93行遇到了问题。显然,PDFPage正在尝试使用一些Android不支持的java.awt.geom中的类(Rectangle2D、ImageObserver、Image)。您是如何使其正常工作的? - Pieter888
是的,我已将 pdf-renderer-1.0.5.jar 添加到构建路径中。这是我在 Rectangle2D 中遇到的错误:The type java.awt.geom.Rectangle2D cannot be resolved. It is indirectly referenced from required .class files,并且我在之前的评论中提到的类也出现了相同的错误。 - Pieter888
1
你好MKJParekh,我已经尝试了很多解决方案,我认为大部分PDF转图像的栈解决方案都不太合适。我找不到任何免费的PDF转图像库(jar),也不知道如何在我的Activity类中实现这个jar文件。请指导我... - Mihir Trivedi
请您提供逐步操作步骤……我遇到了很多错误。 - Faiz Anwar
@MDFAIZANWAR 抱歉,我无法提供帮助,因为我几年前创建了演示,所以我不记得它了。 - MKJParekh
显示剩余6条评论

7

从Android API 21开始,您需要使用PdfRenderer来实现相关功能。


2
如何支持低至14的API版本? - hasnain_ahmad
1
仅供参考,PdfRenderer不支持自定义字体和样式。请查看https://github.com/googlesamples/android-PdfRendererBasic/issues/18 - Aaditya Paliwal

6

使用库 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;
    }

3

使用Android默认库,如AppCompat,您可以将所有PDF页面转换为图像。这种方式非常快速和优化。以下代码用于获取PDF页面的单独图像。它非常快速和快捷。

我已经实现了以下内容:

ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(new File("pdfFilePath.pdf"), MODE_READ_ONLY);
    PdfRenderer renderer = new PdfRenderer(fileDescriptor);
    final int pageCount = renderer.getPageCount();
    for (int i = 0; i < pageCount; i++) {
        PdfRenderer.Page page = renderer.openPage(i);
        Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, 0, null);
        page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
        page.close();

        if (bitmap == null)
            return null;

        if (bitmapIsBlankOrWhite(bitmap))
            return null;

        String root = Environment.getExternalStorageDirectory().toString();
        File file = new File(root + filename + ".png");

        if (file.exists()) file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            Log.v("Saved Image - ", file.getAbsolutePath());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

=======================================================

private static boolean bitmapIsBlankOrWhite(Bitmap bitmap) {
    if (bitmap == null)
        return true;

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    for (int i =  0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            int pixel =  bitmap.getPixel(i, j);
            if (pixel != Color.WHITE) {
                return false;
            }
        }
    }
    return true;
}

我已经在另一个问题中发布了它 :P
链接是 - https://dev59.com/XGAg5IYBdhLWcg3wLIXS#58420401

谢谢你的代码。一切都运行得很完美,但是图片质量不太好。如何获得更好的质量? - Real Tech Hacks
这段代码对于某些pdf文件无法正常工作。 - K Pradeep Kumar Reddy
谢谢,看起来它运行良好并且拯救了我的一天。其他库需要超过40MB,所以这个解决方案就是我需要的 ;) - Androdos

2

从下面的代码中,您可以使用PDFRender从PDF中提取所有页面的图像(PNG格式):

// This method is used to extract all pages in image (PNG) format.
    private void getImagesFromPDF(File pdfFilePath, File DestinationFolder) throws IOException {

        // Check if destination already exists then delete destination folder.
        if(DestinationFolder.exists()){
            DestinationFolder.delete();
        }

        // Create empty directory where images will be saved.
        DestinationFolder.mkdirs();

        // Reading pdf in READ Only mode.
        ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(pdfFilePath, ParcelFileDescriptor.MODE_READ_ONLY);

        // Initializing PDFRenderer object.
        PdfRenderer renderer = new PdfRenderer(fileDescriptor);

        // Getting total pages count.
        final int pageCount = renderer.getPageCount();

        // Iterating pages
        for (int i = 0; i < pageCount; i++) {

            // Getting Page object by opening page.
            PdfRenderer.Page page = renderer.openPage(i);

            // Creating empty bitmap. Bitmap.Config can be changed.
            Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),Bitmap.Config.ARGB_8888);

            // Creating Canvas from bitmap.
            Canvas canvas = new Canvas(bitmap);

            // Set White background color.
            canvas.drawColor(Color.WHITE);

            // Draw bitmap.
            canvas.drawBitmap(bitmap, 0, 0, null);

            // Rednder bitmap and can change mode too.
            page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

            // closing page
            page.close();

            // saving image into sdcard.
            File file = new File(DestinationFolder.getAbsolutePath(), "image"+i + ".png");

            // check if file already exists, then delete it.
            if (file.exists()) file.delete();

            // Saving image in PNG format with 100% quality.
            try {
                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                Log.v("Saved Image - ", file.getAbsolutePath());
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

您可以以下面的方式调用此方法:
// Getting images from Test.pdf file.
        File source = new File(Environment.getExternalStorageDirectory() + "/" + "Test" + ".pdf");

        // Images will be saved in Test folder.
        File destination = new File(Environment.getExternalStorageDirectory() + "/Test");

        // Getting images from pdf in png format.
        try {
            getImagesFromPDF(source, destination);
        } catch (IOException e) {
            e.printStackTrace();
        }

干杯!


这个可行吗?似乎没有人尝试过这个解决方案。 - K Pradeep Kumar Reddy

1
最后我找到了一个非常简单的解决方案, 从这里下载库。
使用以下代码从PDF中获取图片:
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;

import org.vudroid.core.DecodeServiceBase;
import org.vudroid.core.codec.CodecPage;
import org.vudroid.pdfdroid.codec.PdfContext;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

/**
 * Created by deepakd on 06-06-2016.
 */
public class PrintUtils extends AsyncTask<Void, Void, ArrayList<Uri>>
{
    File file;
    Context context;
    ProgressDialog progressDialog;

    public PrintUtils(File file, Context context)
    {

        this.file = file;
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        // create and show a progress dialog
        progressDialog = ProgressDialog.show(context, "", "Please wait...");
        progressDialog.show();
    }

    @Override
    protected ArrayList<Uri> doInBackground(Void... params)
    {
        ArrayList<Uri> uris = new ArrayList<>();

        DecodeServiceBase decodeService = new DecodeServiceBase(new PdfContext());
        decodeService.setContentResolver(context.getContentResolver());
        // a bit long running
        decodeService.open(Uri.fromFile(file));
        int pageCount = decodeService.getPageCount();
        for (int i = 0; i < pageCount; i++)
        {
            CodecPage page = decodeService.getPage(i);
            RectF rectF = new RectF(0, 0, 1, 1);
            // do a fit center to A4 Size image 2480x3508
            double scaleBy = Math.min(UIUtils.PHOTO_WIDTH_PIXELS / (double) page.getWidth(), //
                    UIUtils.PHOTO_HEIGHT_PIXELS / (double) page.getHeight());
            int with = (int) (page.getWidth() * scaleBy);
            int height = (int) (page.getHeight() * scaleBy);
            // Long running
            Bitmap bitmap = page.renderBitmap(with, height, rectF);
            try
            {
                OutputStream outputStream = FileUtils.getReportOutputStream(System.currentTimeMillis() + ".JPEG");
                // a bit long running
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.close();
               // uris.add(getImageUri(context, bitmap));
                uris.add(saveImageAndGetURI(bitmap));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return uris;
    }

    @Override
    protected void onPostExecute(ArrayList<Uri> uris)
    {
        progressDialog.hide();
        //get all images by uri 
        //ur implementation goes here
    }




    public void shareMultipleFilesToBluetooth(Context context, ArrayList<Uri> uris)
    {
        try
        {
            Intent sharingIntent = new Intent();
            sharingIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
            sharingIntent.setType("image/*");
           // sharingIntent.setPackage("com.android.bluetooth");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, uris);
            context.startActivity(Intent.createChooser(sharingIntent,"Print PDF using..."));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }





    private Uri saveImageAndGetURI(Bitmap finalBitmap) {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/print_images");
        myDir.mkdirs();
        String fname = "Image-"+ MathUtils.getRandomID() +".jpeg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return Uri.parse("file://"+file.getPath());
    }

}

FileUtils.java

package com.airdata.util;

import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * Created by DeepakD on 21-06-2016.
 */
public class FileUtils
{

    @NonNull
    public static OutputStream getReportOutputStream(String fileName) throws FileNotFoundException
{
    // create file
    File pdfFolder = getReportFilePath(fileName);
    // create output stream
    return new FileOutputStream(pdfFolder);
}

    public static Uri getReportUri(String fileName)
    {
        File pdfFolder = getReportFilePath(fileName);
        return Uri.fromFile(pdfFolder);
    }
    public static File getReportFilePath(String fileName)
    {
        /*File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), FileName);*/
        File file = new File(Environment.getExternalStorageDirectory() + "/AirPlanner/Reports");
        //Create report directory if does not exists
        if (!file.exists())
        {
            //noinspection ResultOfMethodCallIgnored
            file.mkdirs();
        }
        file = new File(Environment.getExternalStorageDirectory() + "/AirPlanner/Reports/" + fileName);
        return file;
    }
}

您可以在图库或SD卡中查看转换后的图片。如果您需要任何帮助,请告诉我。

它的工作很好,但在打开受密码保护的 PDF 时出现异常,请帮忙。@dd619 - Sunil Chaudhary
抱歉兄弟,我无法帮助你解决密码保护的PDF问题。 - dd619
你可以在pdfContext类的以下方法中添加密码:public CodecDocument openDocument(String fileName) { return PdfDocument.openDocument(fileName, mPassword); } - coder

1

我会告诉你一个简单的技巧,而不是完整的解决方案。一旦你成功地呈现了pdf页面,你将从屏幕上获取它的位图,如下所示。

View view = MuPDFActivity.this.getWindow().getDecorView();
if (false == view.isDrawingCacheEnabled()) {
    view.setDrawingCacheEnabled(true);
}
Bitmap bitmap = view.getDrawingCache();

然后您可以将此位图保存为本地图像,即为 PDF 页面转换成图像。
try {
    new File(Environment.getExternalStorageDirectory()+"/PDF Reader").mkdirs();
    File outputFile = new File(Environment.getExternalStorageDirectory()+"/PDF Reader", System.currentTimeMillis()+"_pdf.jpg");
    FileOutputStream outputStream = new FileOutputStream(outputFile);

    // a bit long running
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.close();
} catch (IOException e) {
    Log.e("During IMAGE formation", e.toString());
}

就这些,希望能帮到您。


1

经过尝试所有答案后,对于所有PDF文件都没有起作用。自定义字体PDF文件存在渲染问题。然后我尝试使用library。我受到了NickUncheck的回答启发,从所有PDF页面获取图像。

代码如下:

在您的app build.gradle文件中添加以下依赖项:

implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

将PDF页面转换为图像的代码:

      public static List<Bitmap> renderToBitmap(Context context, String filePath) {
            List<Bitmap> images = new ArrayList<>();
            PdfiumCore pdfiumCore = new PdfiumCore(context);
            try {
                File f = new File(pdfPath);
                ParcelFileDescriptor fd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
                PdfDocument pdfDocument = pdfiumCore.newDocument(fd);
                final int pageCount = pdfiumCore.getPageCount(pdfDocument);
                for (int i = 0; i < pageCount; i++) {
                    pdfiumCore.openPage(pdfDocument, i);
                    int width = pdfiumCore.getPageWidthPoint(pdfDocument, i);
                    int height = pdfiumCore.getPageHeightPoint(pdfDocument, i);
                    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    pdfiumCore.renderPageBitmap(pdfDocument, bmp, i, 0, 0, width, height);
                    images.add(bmp);
                }
                pdfiumCore.closeDocument(pdfDocument);
            } catch(Exception e) {
                //todo with exception
            }
     return images;
   }

到目前为止,我尝试过的所有PDF文件都可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接