0

Is there any compatible android library to convert a pdf page to an image ? I found several java libraries (pdfbox, jpod, jpedal, icepdf) fitting my needs but they give me compile errors (based on awT or swt). I'm currently making a pdf viewer on android, I'll be great if I don't have to write the pdf decoding from scratch.

tshepang
  • 12,111
  • 21
  • 91
  • 136
parser_failed
  • 697
  • 9
  • 20

4 Answers4

4

Your best bet is this library: http://code.google.com/p/apv/

It's a C project, so you'll need to invoke using JNI.

James L
  • 16,456
  • 10
  • 53
  • 70
1

None of the answers above helped me so I am writing my solution.

Using this library: android-pdfview and the following code, you can reliably convert the PDF pages into images (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);
    }
}

You should do this work in the background i.e. by using an AsyncTask or something similar as quite a few methods take computation or IO time (I have marked them in comments).

Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89
1

Thanks for the help. I checked the project and used the MuPDF library (on which the APV project is based). It works fine ! Thanks a lot and God bless.

parser_failed
  • 697
  • 9
  • 20
0

Use itextpdf5.3.1.jar

Here is the code for extracting image from pdf:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);`enter code here`
    PdfReader reader;

    File file = new File("/sdcard/vineeth/anni.prc");
    try{
        reader = new PdfReader(file.getAbsolutePath());

        for (int i = 0; i < reader.getXrefSize(); i++) {
            PdfObject pdfobj= reader.getPdfObject(i);
            if (pdfobj == null || !pdfobj.isStream()) {
                continue;
            }

            PdfStream stream = (PdfStream) pdfobj;
            PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);

            if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
                byte[] img = PdfReader.getStreamBytesRaw((PRStream) stream);
                FileOutputStream out = new FileOutputStream(new 
                File(file.getParentFile(),String.format("%1$05d", i) + ".jpg"));
                out.write(img); out.flush(); out.close();
            }
        }
    } catch (Exception e) { }
}
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
  • The code above only _extracts_ images contained in a pdf, it does not generate an image of a pdf file. – Minsky Aug 07 '14 at 08:16