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.

-->

2 comments:

  1. Hi Dara,
    Thanks for sharing informative article on text to word converter. Your post helped to understand the career in Java. JAVA Training in Chennai

    ReplyDelete