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…
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();
}
}
}
|
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.