Java: Convert Word to EPUB

EPUB (short for electronic publication) is a popular file format for e-books. EPUB files can be read on numerous e-readers and most smartphones, tablets and computers. In some circumstances, you might need to convert your Word document to EPUB file format to make it readable on various devices. This article will show you how to achieve this task programmatically in Java 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 EPUB

The following are the steps to convert a Word document to EPUB file format:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Save the Word document to EPUB using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertWordToEpub {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Save the Word document to EPUB format
        doc.saveToFile("ToEpub.epub", FileFormat.E_Pub);
    }
}

Java: Convert Word to EPUB

Convert Word to EPUB with a Cover Image

The following are the steps to convert a Word document to EPUB with a cover image:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Create a DocPicture instance.
  • Load an image using DocPicture.loadImage() method.
  • Save the Word document to EPUB with cover image using Document.saveToEpub(String, DocPicture) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.fields.DocPicture;

public class ConvertWordToEpubWithCoverImage {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Create a DocPicture instance
        DocPicture picture = new DocPicture(doc);
        //Load an image
        picture.loadImage("Cover.png");

        //Save the Word document to EPUB with cover image
        doc.saveToEpub("ToEpubWithCoverImage.epub", picture);
    }
}

Java: Convert Word to EPUB

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.