Showing posts with label docx. Show all posts
Showing posts with label docx. 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

Sunday, June 30, 2013

Word to Text Converter

The WordToTextConverter can be used to convert from a microsoft word 2007+ file to a text file. Again, this program uses the Apache POI library or tika library to extract text from Microsoft Word 2007+ file.

Word file to text converter



import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordToTextConverter{
public static void main(String[] args){
try{
convertWordToText(args[0],args[1]);
}catch(ArrayIndexOutOfBoundsException aiobe){
System.out.println("Usage:java WordToTextConverter <word_file> <text_file>");

}

}


public static void convertWordToText(String src, String desc){
try{
//create file inputstream object to read data from file
FileInputStream fs=new FileInputStream(src);
//create document object to wrap the file inputstream object
XWPFDocument docx=new XWPFDocument(fs);
//create text extractor object to extract text from the document
XWPFWordExtractor extractor=new XWPFWordExtractor(docx);
//create file writer object to write text to the output file
FileWriter fw=new FileWriter(desc);
//write text to the output file
fw.write(extractor.getText());
//clear data from memory
fw.flush();
//close inputstream and file writer
fs.close();
fw.close();

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

In the code of the program above,  XWPFDocument is used to construct a Microsoft Word document object from the FileInputStream object. FileInputStream object contains all data of the original Microsoft Word file. To extract all text from the document, you need to use the  XWPFWordExtractor class. You will pass the document object to the constructor of the XWPFWordExtractor when you create an object of the XWPFWordExtractor class. From the XWPFWordExtractor object, you can get the text content of this object by using its getText method. Once you have the text, the FileWriter class can be used to output it to the destination file.

Merge or Combine PDF, Txt, Images

Friday, June 28, 2013

Text To Word Converter

The TextToWordConverter is able to convert from a text file to a microsoft word 2007+ (docx) file. To work with microsoft office documents from Java code, you will need to use the Apache POI library. You can download this library in jar file from Apache POI website or download tika library jar file from Apache tika. The tika library contains Apache POI, PdfBox, SAX, log4j,jdom, and other useful libraries that are packaged together.

text to word converter in Java

TexToWordConverter source code:

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.File;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class TextToWordConverter {
public static void main(String[] args){
              try{
                convertTextToWord(args[0],args[1]);
                }catch(ArrayIndexOutOfBoundsException aiobe){System.out.println("Usage: TextToWordConverter <text_file> <word_file>");}
}

        public static void convertTextToWord(String src,String des){
try{

                        //check source file existence
                        File sourcefile=new File(src);
                        if(!sourcefile.exists()){
                                System.out.println("Source not found");
                                System.exit(-1);
                        }
                        System.out.println("Please wait...");
                        FileReader fr=new FileReader(sourcefile); //create file reader
                        BufferedReader br=new BufferedReader(fr); //wrap file reader in BufferedReader
                        //so the reader can read the text by line
                        XWPFDocument docx=new XWPFDocument(); //create document for ms word 2007+
String text="";
while((text=br.readLine())!=null){
                                XWPFParagraph pa=docx.createParagraph(); //create paragraph in the document
                                XWPFRun run=pa.createRun(); //create run object in the paragraph
                                run.setFontFamily("Arial"); //set the font name of the text to be written
                                run.setFontSize(12); //set the font size of the text to be writtten
                                run.setText(text);//add text to paragraph
                                run.addBreak(); //add break
}

                        //check destination directory
                        File desdir;
                        if(des.lastIndexOf('/')!=-1){
                                desdir=new File(des.substring(0,des.lastIndexOf('/')));
                                if(!desdir.exists())
                                        desdir.mkdirs(); //create destination directory
                        }
                        docx.write(new FileOutputStream(des)); //write the content of the document to the output file
                                                           
br.close();
                        System.out.println("Conversion completed");

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

}
}

Converting a text file to a pdf file is straightforward. Firstly, you will have a FileReader object to read the source text file. To read line by line from the text file, you will BufferedReader class. This class accepts the FileReader as its argument. To output the text to the Microsoft Word file, you need to create a Microsoft Word document object by using the XWPFDocument class. Once you have the Microsoft Word document object, you can create paragraphs in the document by using the createParagraph method. The Run objects of the paragraph is used to store text. You can apply styles to the text by using its methods (setFontFamily, setFontSize,...). After you prepare the Microsoft Word document already, to write it to the output file, use its write(FileOutputStream fos) method. You will need a FileOutputStream object to point to the output file.

-->
PDF To Word Converter, convert PDF To Word