Convert Word to Image in Java

Spire.Doc for Java offers the Document.saveToImages() method to convert Word documents to images. This article will demonstrate how to convert a particular page to an image as well as how to convert the whole document to multiple images.

Below is a screenshot of the input Word document containing 3pages:

Convert Word to Image in Java

Convert a particular page to an image

import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class toImage {
    public static void main(String[] args) throws IOException {
        //Create a Document object
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("input.docx");

        //Save the first page to a .png image
        BufferedImage image= doc.saveToImages(0, ImageType.Bitmap);
        File file= new File("ToPNG.png");
        ImageIO.write(image, "PNG", file);
    }
}

Convert Word to Image in Java

Convert the whole document to multiple images

import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

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

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

        //Load a Word document
        doc.loadFromFile("input.docx");

        //Loop through the pages
        for (int i = 0; i < doc.getPageCount(); i++) {

            //Save all pages in the Word document to .png images
            BufferedImage image = doc.saveToImages(i, ImageType.Bitmap);
            File file = new File("out/" + String.format(("Img-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }
    }
}

Convert Word to Image in Java