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 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){
                }catch(ArrayIndexOutOfBoundsException aiobe){System.out.println("Usage: TextToWordConverter <text_file> <word_file>");}

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

                        //check source file existence
                        File sourcefile=new File(src);
                                System.out.println("Source not found");
                        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="";
                                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;
                                desdir=new File(des.substring(0,des.lastIndexOf('/')));
                                        desdir.mkdirs(); //create destination directory
                        docx.write(new FileOutputStream(des)); //write the content of the document to the output file
                        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.

compass app flashLight app


  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

    1. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training


  2. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    MS office course in chennai

  3. Similar standards of trust and genuineness ought to be found in text mystics as it is in some other you may utilize.text to speech software

  4. For the most part, you can discover both Cute PDF Writer and the friend Ghostscript programs at the equivalent download area.

  5. Another alternative is to utilize a free online PDF record change benefit. A few such administrations exist. Just enter "free PDF change" or comparative hunt terms into a web index and you ought to have a few conceivable outcomes.

  6. This can make it significantly less demanding to decide whether the words have been made

  7. Also avoid colours with the numbers 1-3 or 6-10 the former are two dark and the latter are too light. A 3N or a 4N (clearly displayed on the bottle of dye) should give you a nice, medium brown.

    digital agency Singapore jobs

  8. Such A nice post... thanks For Sharing !!Great information for new guy like Happy New year 2020

  9. Thank you for excellent article.Great information for new guy like antimalware service executable

  10. Thank you for excellent article.Great information for new guy like antimalware service executable

  11. I like what you guys are up also. Such smart work and reporting! Keep up the superb works guys I’ve incorporated you guys to my blogroll. I think it will improve the value of my website :)casket singapore