2012-01-30 2 views
1

Я хочу удалить все гиперссылки документа Word и сохранить текст. У меня есть два метода для чтения документов с документами с расширениями doc и docx.Apache Poi - как удалить все ссылки из документов Word

private void readDocXExtensionDocument(){ 
    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(); 
     System.out.println(context); 
    } catch (InvalidFormatException e) { 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

private void readDocExtensionDocument(){ 
    File inputFile = new File(inputFolderDir, "test.doc"); 
    POIFSFileSystem fs; 
    try { 
     fs = new POIFSFileSystem(new FileInputStream(inputFile)); 
     HWPFDocument document = new HWPFDocument(fs); 
     WordExtractor wordExtractor = new WordExtractor(document); 
     String[] paragraphs = wordExtractor.getParagraphText(); 
     System.out.println("Word document has " + paragraphs.length + " paragraphs"); 
     for(int i=0; i<paragraphs.length; i++){ 
      paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", ""); 
      System.out.println(paragraphs[i]); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

Можно ли удалить все ссылки документа Word с помощью библиотеки apache poi? Если это не так, есть ли другие библиотеки, которые могут это предоставить?

ответ

2

Моим решением, по крайней мере для категории .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(); 
    } 
    } 

Единственное ограничение этого подхода заключается в том, что если есть материал с этих угловых скобках, что не является ссылкой, что тоже может быть удален. Если у вас есть лучшее представление о том, какие ссылки могут появиться, вы можете попробовать более определенное регулярное выражение вместо того, которое я предоставил.