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, 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. 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.
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);
}
}
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.
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);
}
}
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.
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);
}
}
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.
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");
}
}
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.
Top comments (0)