Add images to Word document using Apache POI

In this tutorial I will show you how to insert or add images into a Word document using Apache POI API.


The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Have maven 3 installed and configured
Apache POI dependency in pom.xml

Modify the pom.xml file as shown below

<project xmlns="" xmlns:xsi=""




		<!-- apache poi for xlsx, docx etc reading/writing -->


Create below class to insert or add images into word file

package com.roytuts.apache.poi.word;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class DocxAddImages {

	public static void main(String[] args) throws IOException, InvalidFormatException {
		XWPFDocument doc = new XWPFDocument();
		XWPFParagraph p = doc.createParagraph();

		XWPFRun r = p.createRun();

		File img1 = new File("C:/Users/moni/Desktop/application.jpg");
		File img2 = new File("C:/Users/moni/Desktop/release.jpg");

		BufferedImage bimg1 =;
		//int width1 = bimg1.getWidth();
		int height1 = bimg1.getHeight();

		BufferedImage bimg2 =;
		//int width2 = bimg2.getWidth();
		int height2 = bimg2.getHeight();

		String imgFile1 = img1.getName();
		String imgFile2 = img2.getName();

		int imgFormat1 = getImageFormat(imgFile1);
		int imgFormat2 = getImageFormat(imgFile2);

		r.addPicture(new FileInputStream(img1), imgFormat1, imgFile1, Units.toEMU(600), Units.toEMU(height1));

		r.addPicture(new FileInputStream(img2), imgFormat2, imgFile2, Units.toEMU(500), Units.toEMU(height2));

		FileOutputStream out = new FileOutputStream("word_images.docx");

	private static int getImageFormat(String imgFile) {
		int format;
		if (imgFile.endsWith(".emf"))
			format = XWPFDocument.PICTURE_TYPE_EMF;
		else if (imgFile.endsWith(".wmf"))
			format = XWPFDocument.PICTURE_TYPE_WMF;
		else if (imgFile.endsWith(".pict"))
			format = XWPFDocument.PICTURE_TYPE_PICT;
		else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
			format = XWPFDocument.PICTURE_TYPE_JPEG;
		else if (imgFile.endsWith(".png"))
			format = XWPFDocument.PICTURE_TYPE_PNG;
		else if (imgFile.endsWith(".dib"))
			format = XWPFDocument.PICTURE_TYPE_DIB;
		else if (imgFile.endsWith(".gif"))
			format = XWPFDocument.PICTURE_TYPE_GIF;
		else if (imgFile.endsWith(".tiff"))
			format = XWPFDocument.PICTURE_TYPE_TIFF;
		else if (imgFile.endsWith(".eps"))
			format = XWPFDocument.PICTURE_TYPE_EPS;
		else if (imgFile.endsWith(".bmp"))
			format = XWPFDocument.PICTURE_TYPE_BMP;
		else if (imgFile.endsWith(".wpg"))
			format = XWPFDocument.PICTURE_TYPE_WPG;
		else {
			return 0;
		return format;

Run the above class to see the output in word_images.docx file. The file is created under root directory of the project.

Thanks for reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *