POI API Implementation problem

Vishal Sharma

Vishal Sharma

@vishal-pysGmK Oct 26, 2024
hi..
I'm trying to read a .docx file using apache poi api methods.. But i'm facing a few problems..
Can anyone suggest me the changes to be made in this code??
    package apiole;
    import java.io.*;
    import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
    public class Apiole {
    public static void main(String[] args) throws IOException {
    File f = new File("D:\\vishal\\java_projects\\doc.docx");
    InputStream in = new FileInputStream(f);
    String test = extractText(in);
    System.out.print("text: "+test);
    }
    public static String extractText(InputStream in) {
    XWPFDocument doc = new XWPFDocument(in);
    XWPFWordExtractor ex = new XWPFWordExtractor(doc);
    String text = ex.getText();
    return text;
    }
    }

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Feb 19, 2013

    no response??? 😲
    anyway, problem solved...........
  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Feb 19, 2013

    Vishal0203
    no response??? 😲
    anyway, problem solved...........
    Why not share the answer so that others who're facing the problem will know how to solve it? 😀

    I'm sorry to hear that the question didn't get any response.
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Feb 19, 2013

    The_Big_K
    Why not share the answer so that others who're facing the problem will know how to solve it? 😀

    I'm sorry to hear that the question didn't get any response.
    Okay!!
    So, the above program doesn't work because I've not imported another archive..

    import org.apache.poi.xwpf.usermodel.XWPFDocument;

    this is present in poi-ooxml-....-.jar (not mentioning the full name, its big 😛 )
    you need to add this archive in the library (I used netbeans) as a part of project.
    This is the basic thing.. and the code that works fine is as below!

    package apiole;
     
    import java.io.*;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
     
    public class Apiole {
        public static void main(String[] args) throws IOException {
          File f = new File("D:\\vishal\\java_projects\\doc.docx");
          InputStream in = new FileInputStream(f);
          String test = extractText(in);
          System.out.print("text: "+test);
        }
       
        public static String extractText(InputStream in) throws IOException {
            XWPFDocument doc = new XWPFDocument(in);
        XWPFWordExtractor ex = new XWPFWordExtractor(doc);
        String text = ex.getText();
        return text;
        }
    }