I need to convert .docx file content to HTML text in order to display in web ui.
I've used Apache POI's XWPFDocument class but haven't been able to get any results yet; getting empty string. My code is based on this sample.
Here's also my code:
public JSONObject uploadDocxFile(MultipartFile multipartFile) throws Exception {
InputStream inputStream = multipartFile.getInputStream();
XWPFDocument wordDocument = new XWPFDocument(inputStream);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StringWriter stringWriter = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, new StreamResult(stringWriter));
out.close();
String result = new String(out.toByteArray());
String htmlText = result;
JSONObject jsonObject = new JSONObject();
jsonObject.put("content", htmlText);
jsonObject.put("success", true);
return jsonObject;
}