DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Convert Word to Images (PNG, JPG, TIFF and SVG) using Java

Images are used almost everywhere in our daily life. For various purposes, you may need to convert documents in other formats like Word DOC/DOCX to images. In this article, I am going to describe how to convert Word document to image formats such as PNG, JPG, TIFF and SVG using Java and Spire.Doc for Java library.

Before running the following examples, you need to add dependencies for including Spire.Doc for Java library in your Java project.

Convert Word to PNG/JPG

Spire.Doc for Java uses the saveToImages method of Document class to convert Word document to PNG/JPG images.

The Document class provides several overloads for saveToImages method which you can use to convert Word document to images, or convert particular page(s) of a Word document to images, furthermore, you can also specify the resolution of the images when performing conversion.

The following example shows how to convert a Word document to PNG images.

import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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

        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Save the document to PNG images
        BufferedImage[] images = document.saveToImages(ImageType.Bitmap);
        for(int i = 0; i < images.length; i++)
        {
            BufferedImage image = images[i];
            File file = new File( "images/" + String.format(("Img-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The converted PNG images:
Convert Word to PNG/JPG

Convert Word to TIFF

Spire.Doc for Java provides the ability to convert Word document to multi-page TIFF by using the saveToTiff method of Document class. The saveToTiff method accepts the following parameter:
String: the converted TIFF file name.

The following example shows how to convert a Word document to TIFF.

import com.spire.doc.Document;

public class ConvertWordToTiff {
    public static void main(String[] args){

        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Save the document to TIFF
        document.saveToTiff("toTIFF.tiff");
    }
}
Enter fullscreen mode Exit fullscreen mode

The converted TIFF:
Convert Word to Multi-Page TIFF

Convert Word to SVG

To convert a Word document to SVG, use the saveToSVG method of Document class. The saveToSVG method accepts the following parameter:
String: the converted SVG file name.

The following example shows how to convert a Word document to SVG.

import com.spire.doc.Document;

public class ConvertWordToSvg {
    public static void main(String[] args){

        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Save the document to SVG
        document.saveToSVG("toSVG.svg");
    }
}
Enter fullscreen mode Exit fullscreen mode

The converted SVG:
Convert Word to SVG

Top comments (0)