Java: Convert Word to HTML

An HTML (Hyper Text Markup Language) file is a webpage coded in HTML that can be displayed in a Web browser. It is widely used on the Web as most static webpages have an .html extension. In some cases, you need to convert some document formats (such as Word) to HTML. This tutorial will demonstrate how to convert Word to HTML using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert Word to HTML

Spire.Doc for Java can easily convert Word to HTML using Document.saveToFile() method. You can find the steps as blow.

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Save the document as an HTML file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;

public class WordToHtml {
    public static void main(String[] args) {
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");

        //Save the document as HTML 
        document.saveToFile("output/toHtml.html", FileFormat.Html);
    }
}

Java: Convert Word to HTML

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.