WordToTextRenditionProvider implements RenditionProvider { @Override public InputStream convert(InputStream fromInputSource, String toMimeType) { try { WordprocessingMLPackage pkg = WordprocessingMLPackage.load(fromInputSource); MainDocumentPart documentPart = pkg.getMainDocumentPart(); org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement(); File tmpFile = Files.createTempFile(null, null, new FileAttribute<?>[]{}).toFile(); OutputStream os = new FileOutputStream(tmpFile); Writer out = new OutputStreamWriter(os); TextUtils.extractText(wmlDocumentEl, out); out.close(); if (pkg.getMainDocumentPart().getFontTablePart() != null) { pkg.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles(); } pkg = null; return new ObservableInputStream(new FileInputStream(tmpFile), new FileRemover(tmpFile)); } catch (Exception e) { logger.warn(format("%s rendition failed", toMimeType), e); } return null; }  @Override String consumes(); @Override String[] produces(); @Override InputStream convert(InputStream fromInputSource, String toMimeType);  }
@Test public void testConvert() throws Exception { InputStream converted = service.convert(this.getClass().getResourceAsStream("/sample-docx2.docx"), "text/plain"); assertThat(converted.available(), is(greaterThan(0))); assertThat(((ObservableInputStream)converted).getObservers(), hasItem(is(instanceOf(FileRemover.class)))); String content = IOUtils.toString(converted); assertThat(content,is("This is the Document Title and this is the document body.")); }