Take the 2-minute tour ×

Anyone knows how to extract links from word documents using Apache POI? Or even better, from a paragraph?

share|improve this question
Old style .doc files, or new style .docx ones? (It's slightly different) –  Gagravarr Oct 24 '11 at 13:53

1 Answer 1

Word 2003 and newer :

//Links extractor
StringBuffer text = null;
try {
    FileInputStream fis = new FileInputStream(new File("YOUR_DOCX_FULL_PATH_HERE));
    XWPFDocument document = new XWPFDocument(fis);
    text = new StringBuffer();  

    // First up, all our paragraph based text
    Iterator<XWPFParagraph> i = document.getParagraphsIterator();
    while(i.hasNext()) {
        XWPFParagraph paragraph = i.next();

        // Do the paragraph text
        for(XWPFRun run : paragraph.getRuns()) {

           if(run instanceof XWPFHyperlinkRun) {
               XWPFHyperlink link = ((XWPFHyperlinkRun)run).getHyperlink(document);
               if(link != null) {
                   text.append(" <" + link.getURL() + ">");
} catch (Exception e) {
share|improve this answer
It's generally best not to open a XWFP/XSSF/XSLF instance from an InputStream if you have the file, as it causes the whole lot to have to be buffered into memory. Much better to open directly via the File instead –  Gagravarr May 2 '12 at 13:45

Your Answer


