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

10 comments:

  1. Truly amazing blog. A debt of gratitude is in order for sharing this instructive blog.I have perused every one of the articles in your blog; was truly inspired in the wake of understanding it. Great to discover some new information from this blog. Much obliged for sharing such commendable article through see here.

    ReplyDelete
  2. I am trying the same code on Android Studio but getting library errors.Would you please tell me the versions of Apache POI library used by you?

    ReplyDelete
  3. Thanks your shared!... I hope you will continue to have similar posts to share with everyone! I believe a lot of people will be surprised to read this article! Custom Essay Writing Service

    ReplyDelete
  4. Thanks your shared!... I hope you will continue to have similar posts to share with everyone! I believe a lot of people will be surprised to read this article! Write My Essays

    ReplyDelete
  5. Wow, the effects are wonderful! This is so interesting to look at the changes that could be made to the photo in the process of manipulation. You don't need to miss the opportunity and to use college essay writing services to buy the Photoshop CC software. I will also download it soon.

    ReplyDelete
  6. Is your resume getting results? Are you struggling to get your foot in the door? our service will help you.

    ReplyDelete
  7. In spite of all that I not fathom what the maker is attempting to show up. In any case, here is no mean to share this kind of page and moreover the empty http://www.resumes2017.com/federal-resume-format-2017/ page. Look for this is extraordinary after us and may be this is basically abuse of our chance yet at any rate next time you should take mind next time.

    ReplyDelete
  8. With just 160 characters, you can send an instant message that guarantees that your message is clear and compact. You can tell clients precisely what you need from them and make it a simple read. On the off chance that you have a vast group of onlookers you can even execute Bulk Texting.

    ReplyDelete
  9. Really i am getting inspired and i hope you will be awesome activity you have! but i want to learn this i am using SMS Marketing at ExpertTexting and i have to get many learn and received activities messages you must to check it’s very helpful service and this is really informative blog well sharing more and keep it up your success

    ReplyDelete