Convert .docx to HTML using JAVA
Asked Answered
J

2

9

I tried converting .doc to HTML by using WordToHtmlConverter and it worked perfectly.

But when i tried to convert .docx to HTML, i got stuck with it.

What i tried:

I used the below code to convert .docx to HTML:

The code which i tried from : How to use Tika's XWPFWordExtractorDecorator class?

        InputStream input = TikaInputStream.get(new File("C:\\Users\\Downloads\\filename.docx"));


        Parser parser = new AutoDetectParser();


        StringWriter sw = new StringWriter();
        SAXTransformerFactory factory = (SAXTransformerFactory)
                 SAXTransformerFactory.newInstance();
        TransformerHandler handler = factory.newTransformerHandler();
        handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
        handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
        handler.setResult(new StreamResult(sw));


        try {
            Metadata metadata = new Metadata();
            parser.parse(input, handler, metadata, new ParseContext());
            String xml = sw.toString();
            System.out.print("tika : "+xml); 
        } finally {
            input.close();
        }

The output what i got is,

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title/>
</head>
<body/>
</html>
  • Please explain where i gone wrong?
  • Is there any better way to convert .docx to html string

Appreciate your help, Thanks

Jason answered 9/7, 2014 at 11:51 Comment(7)
According to the documentation poi.apache.org/apidocs/org/apache/poi/hwpf/converter/… this API is meant to be used up to Word 2007 when there were only .doc . So it won't work for .docx with this API. Try so save your document in .docMetaprotein
@singe31 you dint get my point. I have converted .doc to html by using hwpf converter. But im trying to do it for .docx, is there a way?Jason
code.google.com/p/xdocreport/wiki/XWPFConverterXHTMLMetaprotein
At their simplest .docx files are an archive (you can open them with something like 7zip and view the contents) containing a bunch of XML files. With that in mind, you'd want to use something that can transform the XML into HTML.Forequarter
You could also take a look on Pandoc or any other command line tool from Java. These tasks are not that trivial and I'm not sure if there's a a working API out there for that other than POI ATM.Vincent
i figured it out by using the link : code.google.com/p/xdocreport/wiki/XWPFConverterXHTML. i'll just post it as answer, it might help someone. Thank you all for your sugesstions.Jason
You can use docx4j for that, see the example: github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/…Hedonic
J
10

This code worked for me to convert .docx to html:

You can also look at the link : Link to code

       //convert .docx to HTML string
        InputStream in= new FileInputStream(new File(path));
        XWPFDocument document = new XWPFDocument(in);


        XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File("word/media")));

        OutputStream out = new ByteArrayOutputStream();


        XHTMLConverter.getInstance().convert(document, out, options);
        String html=out.toString();
        System.out.println(html);
Jason answered 9/7, 2014 at 12:37 Comment(6)
Could anyone provide an updated example please? The reference does not work anymore. Thanks.Edith
I was getting problem using this code, as I was not able to get the jar for XHTMLOptions, XHTMLConverter and FileURIResolver and then when I searched I got these jars here "org.apache.poi.xwpf.converter.core-1.0.6.jar", "org.apache.poi.xwpf.converter.xhtml-1.0.6.jar" and "ooxml-schemas-1.1.jar", if you use these jars you will not get any kind of error with the above codeBearwood
@Vipul here you have dependency with it mvnrepository.com/artifact/fr.opensagres.xdocreport/…Nubilous
Thanks all for saving my time.Eastward
I have followed the above code it's converting docx to html. But i didn't get border styles which are applied in my docx!!. Any idea??????Aachen
Images are not working in my case. Is there a fix for it?Fragonard
R
2

You may want to make use of Mammoth docx to HTML library.Its a library for displaying doc, docx documents by converting them to html on the browser side as well as can be handled on the backend.

Ranzini answered 18/4, 2019 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.