Apache POI从word文档中提取超链接

Ron*_*Ron 2 java ms-word apache-poi

有人知道如何使用 Apache POI 从 Word 文档中提取链接吗?或者更好的是,从一个段落中?

小智 5

Word 2003 及更新版本:

//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) {
               text.append(run.toString());
               bean.setName(run.toString());
               XWPFHyperlink link = ((XWPFHyperlinkRun)run).getHyperlink(document);
               if(link != null) {
                   text.append(" <" + link.getURL() + ">");
               }
           }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
} 
Run Code Online (Sandbox Code Playgroud)