Java-How to Convert Word Document to Images
Tags: java, wordtoimage, word, javaword
Published: 2022-03-18 07:02:48

Developers often need to convert Word documents to images since images are difficult to edit and easy to view. With the help of [Spire.Doc for java](https://www.e-iceblue.com/Introduce/doc-for-java.html), we can easily **convert word document to image** with high quality in Java applications. In this article, I will explain the solutions of converting word document to bitmap images and vector images from the following four parts. 

- Convert Word document to bitmap images (BMP, Jpeg, Png, and GIF)
- Convert Word document to image with high resolution 
- Convert the Word document to SVG
- Convert Word document to TIFF

###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](https://www.e-iceblue.com/Download/doc-for-java.html). 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>5.2.3</version>
    </dependency>
</dependencies>
```

### Convert Word document to bitmap images 

Spire.Doc for Java offers the **Document.saveToImages()** method to convert Word documents to bitmap images in Jpeg, Jpg, Png, Bmp, Tiff, Gif etc. Here we use PDF to .png image for example.

- Create a Document instance.
- Load a Word document using **Document.loadFromFile()** method.
- Save the document to image using **Document.saveToImages()** method.

```java

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

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

public class WordtoImage {
    public static void main(String[] args) throws Exception {

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

        //Save the first page to a .png image
        BufferedImage image= document.saveToImages(0, ImageType.Bitmap);

        File file= new File("ToPNG.png");
        ImageIO.write(image, "PNG", file);
        }
    }
```


![Word to PNG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rip86j1ct2uxdx69kpey.png)
###Convert Word document to image with high resolution 
Sometimes, the generated image is not clear enough. You could use **document.saveToImages(pageIndex,pageCount,ImageType,dpiX,dpiY)** method to set the image resolution and get high quality images.

```java
import com.spire.doc.*;
import com.spire.doc.documents.ImageType;

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

public class WordtoImage {
    public static void main(String[] args) throws Exception {

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

        //set the image resolution and save to image
        BufferedImage[] image= document.saveToImages(0, 1, ImageType.Bitmap, 300, 300);
        File file= new File("ToPNGwithresolution.png");
        ImageIO.write(image[0], "PNG", file);
    }
}
```


![SetImageresolution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fn0ufz44g1ulpjbon9kw.png)
 

###Convert Word document to SVG

**SVG (Scalable Vector Graphics)** is a vector image file format. It is more suitable for web than other image formats. Unlike raster formats such as JPG, GIF, and PNG, an SVG image stay crisp and clear at any resolution or size. Spire.Doc for Java provides the Document.saveToFile() method for converting Word to SVG. 

- Create a Document instance.
- Load a Word document using **Document.loadFromFile()** method.
- Save the document to SVG using **Document.saveToFile() **method.

```java
import com.spire.doc.*;

public class WordtoSVG {
    public static void main(String[] args) throws Exception {

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

        //Save Word as SVG.
        document.saveToFile("out/ToSVG.svg",FileFormat.SVG);

    }
}
```
![Word to SVG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8lm18omazig8oue49on.png)

### Convert Word document to TIFF

**TIFF (Tagged Image File Format)** is a flexible file format for storing raster graphics images. Spire.Doc for Java provides the **Document.saveToTiff()** method for converting Word to TIFF.

- Create a Document instance.
- Load a Word document using **Document.loadFromFile()** method.
- Save the document to TIFF using **Document.saveToTiff()** method.

```java

import com.spire.doc.*;

public class WordtoTIFF {
    public static void main(String[] args) throws Exception {

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

        //Save Word as TIFF
        document.saveToTiff("ToTiff.tiff");
    }
}
```

![Word to TIFF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/albw9w7kwlix6l9vn22k.png)

### Conclusion

In this article, you have learned how to convert word document in DOCX, DOC format to image programmatically in Java. You have explored the basic conversion scenario as well as the advanced conversion by setting the image resolution. Moreover, Spire.Doc also support to convert **word to PDF**, **word to HTML** and etc. Wish it helps.