Android extract images from pd file

In this Android tip, I am going to show you how to extract images from a Pdf file and save them in the storage of your phone. A powerful library to achieve the goal is iTextG library. To use this library, you need to download and add the library jar file in the libs directory of your project. In Android Student, you need to specify the jar that is already in the libs directory as library by right-clicking the jar file and select Add As Library…




The followings are the steps need to be undertaken to extract images from the Pdf file.

1. You create a PdfReader object to read the Pdf file.

2. From the reader object, you can get the number of objects in the Pdf file using the getXRefSize() method.

3. You use a loop to iterate the objects in the reader. PdfReader has a method called getPdfObject() that is able to return an object at a specified index.

4. You convert the object to PRStream object.
5. From the stream object, you can check whether the object is an image using the get() method passing PdfName.SUBType as parameter.
6. If the object is an image, you will convert it to PdfImageObject and save it on your phone.

Extract images from pdf


Here is the code written in the MainActivity class to extract all images from a Pdf file called PHPTraining.pdf in the external storage directory. The images are saved in the same directory. Do not forget to declare the WRITE_EXTERNL_STORAGE permission in AndroidManifest file of your project.


import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfImageObject;


public class MainActivity extends Activity {

   ExpandableListAdapter expandableListAdapter;
   ExpandableListView expListView;
   List<String> listGroupTitles;
   HashMap<String, List<String>> listDataMembers;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
    
     extractImages(Environment.getExternalStorageDirectory()+"/"+"PHPTraining.pdf");
   }

   private void extractImages(String filepath){
     PRStream prStream;
     PdfObject pdfObject;
     PdfImageObject pdfImageObject;
     FileOutputStream fos;
     try {
        // Create pdf reader
        PdfReader reader = new PdfReader(filepath);
        // Get number of objects in pdf document
        int numOfObject=reader.getXrefSize();
        for(int i=0;i<numOfObject;i++){
           // Get PdfObject
           pdfObject=reader.getPdfObject(i);
           if(pdfObject!=null && pdfObject.isStream()){
             prStream= (PRStream)pdfObject; //cast object to stream
             PdfObject type =prStream.get(PdfName.SUBTYPE); //get the object type
             // Check if the object is the image type object
             if (type != null && type.toString().equals(PdfName.IMAGE.toString())){
                // Get the image from the stream
                pdfImageObject= new PdfImageObject(prStream);
                fos=new FileOutputStream(Environment.getExternalStorageDirectory()+"/image"+i+".jpg");
                // Read bytes of image in to an array
                byte[] imgdata=pdfImageObject.getImageAsBytes();
                // Write the bytes array to file
                fos.write(imgdata, 0, imgdata.length);
                fos.flush();
                fos.close();
             }
           }

        }

     }catch (IOException ioe){
        ioe.printStackTrace();
     }
   }
}


Posted by: Dara | post date: 03-20-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.