1

I have an Android application and I want to show up a PDF into it (without external applications). The only solution that I thought is to convert the pages of the PDF to images. Someone has experience with that issue? which library do you recommend me?

I tested the next libraries, but I have had troubles:

1) PdfRenderer library does not work with modern PDF
2) jPDFImages not work in Android (works in java desktop application)

Sorry for my English

  • You can take a look into iText pdf, it is free as long as you dont charge for your product/services – ug_ Oct 16 '13 at 18:18
  • Check this: http://stackoverflow.com/questions/2456344/display-pdf-within-app-on-android – Chintan Soni Oct 16 '13 at 18:28
  • Also check this library: http://code.google.com/p/apv/ – Chintan Soni Oct 16 '13 at 18:29
  • @ns47731 The gratis version iText is licensed under AGPL, which allows charging money, though it also requires code distributed with it to be licensed as GPL or AGPL. – Dev Oct 16 '13 at 18:36

3 Answers3

2

I'm expanding on the accpeted answer and providing a complete 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
0

You can use MuPDF. Here is a link that describes how to build MuPDF for android: http://mupdf.com/docs/how-to-build-mupdf-for-android

HABJAN
  • 9,212
  • 3
  • 35
  • 59
0

I recommend use this library, android-pdfview