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

10 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

  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

    ReplyDelete
  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

    ReplyDelete
  4. For the most part, you can discover both Cute PDF Writer and the friend Ghostscript programs at the equivalent download area. https://www.altoconvertpdftoword.com/faq

    ReplyDelete
  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. https://altomerge.com/blog/the_easiest_way_to_combine_pdfs

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

    ReplyDelete
  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

    ReplyDelete
  8. 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

    ReplyDelete