title: Unable to convert docx to html using Java
body: <p>I need to convert .docx file content to HTML text in order to display in web ui.</p>
  
  <p>I've used <strong>Apache POI</strong>'s <strong>XWPFDocument</strong> class but haven't been able to get any results yet; <strong>getting empty string</strong>. My code is based on <a href="https://stackoverflow.com/q/9047044/282855">this sample</a>. </p>
  
  <p>Here's also my code:</p>
  
  <pre><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;
      }
  </code></pre>