Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a PDF page to an image in Android?

Tags:

java

android

pdf

All I need to do is take a (locally saved) PDF-document and convert one or all of it's pages to image format like JPG or PNG.

I've tried lot's of PDF Rendering/viewing solutions like APV PDF Viewer, APDFViewer, droidreader, android-pdf, MuPdf and many others but couldn't figure it out so far that how to convert a pdf-page into image?.

EDIT: Also I'd rather have a PDF to image converter than a PDF renderer that I need to edit to convert PDF to image.

like image 219
Pieter888 Avatar asked May 22 '12 08:05

Pieter888


People also ask

Can I turn a PDF into images?

Acrobat's online converter tool lets you quickly convert a PDF to a PNG, TIFF, or JPG image using any web browser, such as Google Chrome or Microsoft Edge. Just choose your preferred file format. The Acrobat JPG conversion process happens in seconds, with image quality you can trust.

How can I convert PDF to JPG for free on mobile?

Steps to Convert PDF to JPG The very first thing to do is to get Kaagaz on your Android. After you download the app go to PDF Tools and click on PDF to JPG, now select the PDF file from your mobile that you want to convert. Upon finding the file, click on the option and choose 'Open' to import it to the Kaagaz App.

How do I split a PDF into an image?

Drag and drop your file in the PDF to JPG converter. Select 'Convert entire pages' or 'Extract single images'. Click on 'Choose option' and wait for the process to complete. Download the converted files as single JPG files, or collectively in a ZIP file.


Video Answer


1 Answers

To support API 8 and above, follow:

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).

like image 193
Vedant Agarwala Avatar answered Oct 07 '22 02:10

Vedant Agarwala