Sunday, September 16, 2018

Add images to Word document using Apache POI

You may have been worked with Apache POI Library to read and write content in excel sheet. But in this article we are going to use Apache POI Library to add images and text content to word document using java. Using Apache POI Library you can perform any kind of read and write operation in word document easily.

Prerequisites :
1. Install JDK 1.8
2.  Download Apache POI 3.17 library and jars.
3.  Install eclipse.
Add images to Word document using Apache POI

How to add images and text message to word document using Apache POI

Lets see the below source code, that helps you to write content in word document with the help of Apache POI Library in java. Go through the comment section of the code ,where we have explained code details for each line.

WordDocument.java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

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

public class WordDocument {
 
 //Creating word document instance...
 static XWPFDocument document = new XWPFDocument();
 static XWPFParagraph p = document.createParagraph();
    static XWPFRun r = p.createRun();
 
 public static void addImagesToWordDocument(File imageFile, String textToDisplay)
   throws IOException, InvalidFormatException {
  
  
        // Read image file
  BufferedImage bimg1 = ImageIO.read(imageFile);
  int width = bimg1.getWidth();
  int height = bimg1.getHeight();  
        
  // get image file name
  String imgFile = imageFile.getName(); 
  
        // get image format
  int imgFormat = getImageFormat(imgFile);
  
        // get the text value to display from calling function
  String p1 = textToDisplay;  
        
  // adding image and text parameters with the help of below function 
  r.setText(p1);
  r.addBreak();
  r.addPicture(new FileInputStream(imageFile), imgFormat, imgFile, Units.toEMU(width), Units.toEMU(height));
         
  
 }
    /*
     * Checking the image file format...
     */
 private static int getImageFormat(String imgFileName) {
  int format;
  if (imgFileName.endsWith(".emf"))
   format = XWPFDocument.PICTURE_TYPE_EMF;
  else if (imgFileName.endsWith(".wmf"))
   format = XWPFDocument.PICTURE_TYPE_WMF;
  else if (imgFileName.endsWith(".pict"))
   format = XWPFDocument.PICTURE_TYPE_PICT;
  else if (imgFileName.endsWith(".jpeg") || imgFileName.endsWith(".jpg"))
   format = XWPFDocument.PICTURE_TYPE_JPEG;
  else if (imgFileName.endsWith(".png"))
   format = XWPFDocument.PICTURE_TYPE_PNG;
  else if (imgFileName.endsWith(".dib"))
   format = XWPFDocument.PICTURE_TYPE_DIB;
  else if (imgFileName.endsWith(".gif"))
   format = XWPFDocument.PICTURE_TYPE_GIF;
  else if (imgFileName.endsWith(".tiff"))
   format = XWPFDocument.PICTURE_TYPE_TIFF;
  else if (imgFileName.endsWith(".eps"))
   format = XWPFDocument.PICTURE_TYPE_EPS;
  else if (imgFileName.endsWith(".bmp"))
   format = XWPFDocument.PICTURE_TYPE_BMP;
  else if (imgFileName.endsWith(".wpg"))
   format = XWPFDocument.PICTURE_TYPE_WPG;
  else {
   return 0;
  }
  return format;
 }
 
 
 public static void main(String[] args) throws InvalidFormatException, IOException {
  
  File image = new File("C:\\Users\\Acer-sumit\\Desktop\\template\\bar.jpg");
  //Passing imgae file lication and text/message in addImagesToWordDocument parameter.
  //This function will append content in word document.
  addImagesToWordDocument(image,"image one display");
  addImagesToWordDocument(image,"image two  display");
  
  // writing content [image & text ] to word document...
  FileOutputStream out = new FileOutputStream("sample-doc-file.docx");
  document.write(out);
  out.close();
  document.close();
 }

}

This is all about adding images to Word document using Apache POI in java. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

1 comment: