Convert Word to Images in Java

This following code snippets demonstrate how to convert Word documents to image files using Spire.Doc for Java.

Convert a particular page to an image

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

//load a Word file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

//save the first page to a BufferedImage
BufferedImage image= doc.saveToImages(0, ImageType.Bitmap);

//write the image data to a .png file
File file= new File("output/ToPNG.png");
ImageIO.write(image, "PNG", file);

Convert the whole document to multiple images

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

//load a Word file
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

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

    //save the specific page to a BufferedImage
    BufferedImage image= doc.saveToImages(i, ImageType.Bitmap);

    //write the image data to a .png file
    File file = new File( "output/" + String.format(("Img-%d.png"), i));
    ImageIO.write(image, "PNG", file);
}