Showing posts with label extract images. Show all posts
Showing posts with label extract images. Show all posts

Monday, July 1, 2013

Extract images from Word

WordImageExtractor is able to extract all images in a microsoft word 2007+ file. The Java library that is used to extract the images from the microsoft word 2007+ file is Apache POI. In this libray, there is a class called XWPFDocument. The XWPFDocument class has the getAllPictures method that can extract all images in the file to store in a list.


extract images from microsoft word file

WordImageExtractor source code:

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordImageExtractor{
public static void main(String[] args){

selectwORD();

}

//allow office word file selection for extracting
public static void selectwORD(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("DOCX","docx");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(false);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
    File file=chooser.getSelectedFile();
    System.out.println("Please wait...");  
    extractImages(file.toString());
    System.out.println("Extraction complete");
           }

     
}
public static void extractImages(String src){
try{

//create file inputstream to read from a binary file
FileInputStream fs=new FileInputStream(src);
//create office word 2007+ document object to wrap the word file
XWPFDocument docx=new XWPFDocument(fs);
//get all images from the document and store them in the list piclist
List<XWPFPictureData> piclist=docx.getAllPictures();
//traverse through the list and write each image to a file
Iterator<XWPFPictureData> iterator=piclist.iterator();
int i=0;
while(iterator.hasNext()){
XWPFPictureData pic=iterator.next();
byte[] bytepic=pic.getData();
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytepic));
        ImageIO.write(imag, "jpg", new File("D:/imagefromword"+i+".jpg"));
        i++;
}

}catch(Exception e){System.exit(-1);}

}



}

In the code above, the JFileChooser is used to display a file dialog that the users can easily browse for a Microsoft Word file. Once, the path of the file is obtained, the extracting image process can start. The FileInputStream class reads the byte data of the Microsoft Word file. To get images from the original Microsoft Word file, firstly you need to construct a Microsoft Word document object by using the  XWPFDocument and pass the FileInputStream object to its constructor. Once you have document object, it seems like the original Microsoft Word file now is in you hand so you can do somethings with it. To get the images from the document, you will use the getAllPictures method. This method returns a lit of XWPFPictureData objects. Each XWPFPictureData object refer to an image. You can read all bytes from the XWPFPictureData object by using the getData. When you have the byte array of the image, you can construct the BufferedImage object from it. Then use the write method of the ImageIO class to write the image out to a file.
compass app flashLight app
compass app flashLight app

Wednesday, June 26, 2013

Extract images from PDF

PDFImageExtractor is a simple program that can extract all images on a PDF document. Sometimes, we don't want to convert PDF pages to image files. We only want to take all images from each page. In this scenario, PDFImageExtractor is useful. It is easy to use. When the program runs, its allows you select a PDF file that contains images to be extracted out. The extracted image files are stored in your current working directory.

extract images from pdf java


PDFImageExtractor source code

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
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;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;

public class PDFImageExtractor{
public static void main(String[] args){

selectPDF();
}

//allow pdf file selection for extracting
public static void selectPDF(){

JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF","pdf");
    chooser.setFileFilter(filter);
    chooser.setMultiSelectionEnabled(false);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
File file=chooser.getSelectedFile();
System.out.println("Please wait...");  
            extractImage(file.toString());
System.out.println("Extraction complete");
            }

     
}


public static void extractImage(String src){

try{

//create pdf reader object
PdfReader pr=new PdfReader(src);
PRStream pst;
PdfImageObject pio;
PdfObject po;
int n=pr.getXrefSize(); //number of objects in pdf document
for(int i=0;i<n;i++){
po=pr.getPdfObject(i); //get the object at the index i in the objects collection
if(po==null || !po.isStream()) //object not found so continue
continue;
pst=(PRStream)po; //cast object to stream
PdfObject type=pst.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())){
pio=new PdfImageObject(pst); //get the image
BufferedImage bi=pio.getBufferedImage(); //convert the image to buffered image
ImageIO.write(bi, "jpg", new File("image"+i+".jpg")); //write the buffered image
//to local disk

}

}


}catch(Exception e){e.printStackTrace();}

}


}

In the example code above, the getPdfObject(int index) is used to extract an object from the pdf document at the specified index. To determine whether the object is an image, you need to get the type of the object by using the get  method of the stream created from the object.

Note: When you use this program to extract the images from the PDF document, some images might be in wrong order (different from what you see on the PDF pages). It is the problem from iText library itself. I tried to solve this problem with PdfBox. However, it can not be solved.

Merge or Combine PDF, Txt, Images