Мое решение, по крайней мере для категории .docx, заключается в использовании регулярных выражений. Зацени это
private void readDocXExtensionDocument(){
Pattern p = Pattern.compile("\\<(.+?)\\>");
File inputFile = new File(inputFolderDir, "test.docx");
try {
XWPFDocument document = new XWPFDocument(OPCPackage.open(new FileInputStream(inputFile)));
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
extractor.setFetchHyperlinks(true);
String context = extractor.getText();
Matcher m = p.matcher(context);
while (m.find()) {
String link = m.group(0); // the bracketed part
String textString = m.group(1); // the text of the link without the brackets
context = context.replaceAll(link, ""); // ordering important. Link then textString
context = context.replaceAll(textString, "");
}
System.out.println(context);
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Единственное предостережение при таком подходе состоит в том, что если есть материал с этими угловыми скобками, который не является связующим звеном, его тоже можно удалить. Если у вас есть лучшее представление о том, какие ссылки могут отображаться, вы можете использовать более конкретное регулярное выражение вместо того, которое я предоставил.