Alex Rivera | Logout

Convert a Pdf page into Bitmap in Android Java

Asked 2012-01-11T05:47:42.667
46

I need to convert PDFfile(PDF page) into a Bitmap(or Image file) in Android.

1.Used Pdfbox jar from Apache. But it uses some java classes that is not supported in android. 2. Tried Itext jar which converts image to pdf(I need its reverse operation) Like that I have tried many jars. But no positive result.

byte[] bytes;
    try {

        File file = new File(this.getFilesDir().getAbsolutePath()+"/2010Q2_SDK_Overview.pdf");
        FileInputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();
        bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }


        ByteBuffer buffer = ByteBuffer.NEW(bytes);
        String data = Base64.encodeToString(bytes, Base64.DEFAULT);
        PDFFile pdf_file = new PDFFile(buffer);
        PDFPage page = pdf_file.getPage(2);

        RectF rect = new RectF(0, 0, (int) page.getBBox().width(),
                (int) page.getBBox().height());
      //  Bitmap bufferedImage = Bitmap.createBitmap((int)rect.width(), (int)rect.height(),
         //        Bitmap.Config.ARGB_8888);

        Bitmap image = page.getImage((int)rect.width(), (int)rect.height(), rect);
        FileOutputStream os = new FileOutputStream(this.getFilesDir().getAbsolutePath()+"/pdf.jpg");
        image.compress(Bitmap.CompressFormat.JPEG, 80, os);

       // ((ImageView) findViewById(R.id.testView)).setImageBitmap(image);

I am getting the Image File, enter image description here

Instead of, enter image description here

package com.test123;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.sun.pdfview.PDFFile;
Edit
Report

1 Answer

-1

We can do it using Qoppa PDF toolkit.

Steps to follow to use Qoppa PDF toolkit.

Download it from http://www.qoppa.com/android/pdfsdk/download

This file contains four items:

  1. version_history.txt – This text file will be updated with each new version of the toolkit.
  2. qoppapdf.jar – This is the main jar file for the toolkit. This jar file needs to be added to the classpath for your project.
  3. assets folder – This folder contains assets used by the toolkit, such as fonts and icons. After extracting from the zip files, you will need to copy the files inside this folder to the assets folder in your project.
  4. libs folder – This folder contains native Android libraries, these libraries are needed to decode certain JPEG images as well as JPEG 2000 images that are not supported by Android. The contents of the libs folder need to be copied to the libs folder in your project. If you don’t have a libs folder in your project, create one and copy the contents of this folder into it.

Code is:

        //Converting PDF to Bitmap Image...
        StandardFontTF.mAssetMgr = getAssets();
        //Load document to get the first page
        try {
            PDFDocument pdf = new PDFDocument("/sdcard/PDFFilename.pdf", null);
            PDFPage page = pdf.getPage(0);

            //creating Bitmap and canvas to draw the page into
            int width = (int)Math.ceil(page.getDisplayWidth());
            int height = (int)Math.ceil(page.getDisplayHeight());

            Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
            Canvas c = new Canvas(bm);
            page.paintPage(c);

            //Saving the Bitmap
            OutputStream os = new FileOutputStream("/sdcard/GeneratedImageByQoppa.jpg");
            bm.compress(CompressFormat.JPEG, 80, os);
            os.close();
        } catch (PDFEx
answered 2013-03-14T17:24:52.787

Your Answer