Java: Convert HTML to Word

Converting HTML to Word documents is important for sharing, archiving, and keeping formatting consistent. It makes editing easier, works well with other tools, and meets industry standards. Plus, Word documents can be accessed offline and have a professional look, making them perfect for formal submissions. Overall, this conversion enhances accessibility and simplifies content management.

In this article, you will learn how to use Spire.Doc for Java to convert HTML files and strings into Word documents in 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>13.5.3</version>
    </dependency>
</dependencies>
    

Convert HTML File to Word in Java

To transform an HTML file into a Word document, you mainly use the Document class. This class offers methods that enable you to import HTML content from a file and export it into multiple formats including DOC and DOCX.

Steps for converting an HTML file to a Word document using Java:

  • Instantiate a new Document object
  • Import HTML content using the Document.loadFromFile() method.
  • Save the modified document as a Word file using the Document.saveToFile().
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.XHTMLValidationType;

public class ConvertHtmlFileToWord {

    public static void main(String[] args) {

        // Create a Document object
        Document document = new Document();

        // Load an HTML file
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Html.html", FileFormat.Html, XHTMLValidationType.None);

        // Get the first section
        Section section = document.getSections().get(0);

        // Set the page margins
        section.getPageSetup().getMargins().setAll(2);

        // Save the document to a Word file
        document.saveToFile("output/ToWord.docx",FileFormat.Docx);

        // Dispose resources
        document.dispose();
    }
}

Convert HTML file to Word in Java

Convert HTML String to Word in Java

In some scenarios, you may need to generate or modify HTML content at runtime—for example, when creating HTML strings from user input, database queries, or template engines. Instead of converting a static HTML file, Spire.Doc for Java allows you to render dynamic HTML content directly into a Word document using the Paragraph.appendHTML() method.

Steps for converting an HTML sting to a Word document using Java:

  • Create a Document object.
  • Add a section and a paragraph to the document.
  • Read an HTML string from an HTML file or any other data sources.
  • Render the HTML string into the document using the Paragraph.appendHTML() method.
  • Saves the document as a Word file using the Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.interfaces.IParagraph;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ConvertHtmlStringToWord {

    public static void main(String[] args) throws IOException {

        // Create a Document object
        Document document = new Document();

        // Add a section
        Section section = document.addSection();

        // Set the page margins
        section.getPageSetup().getMargins().setAll(2);

        // Add a paragraph
        IParagraph paragraph = section.addParagraph();

        // Read HTML string from a file
        String htmlFilePath = "C:\\Users\\Administrator\\Desktop\\Html.html";
        String htmlString = new String(Files.readAllBytes(Paths.get(htmlFilePath)), "UTF-8");

        // Append the HTML string to the paragraph
        paragraph.appendHTML(htmlString);

        // Save the document to a Word file
        document.saveToFile("output/ToWord.docx", FileFormat.Docx);

        // Dispose resources
        document.dispose();
    }
}

Convert HTML string to Word in Java

Get a Free License

To fully experience the capabilities of Spire.Doc for Java without any evaluation limitations, you can request a free 30-day trial license.