Open In App

Adding Images to a Word Document using Java

Last Updated : 16 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Java makes it possible to add images to Word documents using the addPicture() method of XWPFRun class provided by Apache POI package. Apache POI is a popular API developed and maintained by the Apache Software Foundation. It provides several classes and methods to perform different file operations on Microsoft office files using Java. In order to attach an image to a word document, the basic necessity is to import the following library of Apache.

poi-ooxml.jar

Method:

  • addPicture(): Helps in attaching the image to overall file. It is defined as follows:

Syntax:

run.addPicture(java.io.InputStream imageData,int imageType,java.lang.String imageFileName,int width,int height)

Parameters:

  • imageData: The raw picture data
  • imageType: The type of the picture, eg XWPFDocument.PICTURE_TYPE_JPEG
  • imageFileName: Name of the image file
  • width: width in EMUs
  • height: height in EMUs 

Approach:

  1. Create a Blank document using XWPFDocument of Apache POI package.
  2. Create a paragraph using the createParagraph() method of XWPFParagraph object.
  3. Create FileOutputStream and FileInputStream of Word and Image respectively.
  4. Create XWPFRun object and add the picture using addPicture() method.

Implementation:

  • Step 1: Creating a blank document
  • Step 2: Creating a Paragraph
  • Step 3: Creating a File output stream of word document at the required location
  • Step 4: Creating a file input stream of the image by specifying its path
  • Step 5: Retrieving the image file name and image type
  • Step 6: Setting the width and height of the image in pixels
  • Step 7: Adding the picture using the addPicture() method and writing into the document
  • Step 8: Closing the connections

Sample input image:  Before implementation 

Example:

Java




// Java program to Demonstrate Adding a jpg image
// To a Word Document
  
// Importing Input output package for basic file handling
import java.io.*;
import org.apache.poi.util.Units;
// Importing Apache POI package
import org.apache.poi.xwpf.usermodel.*;
  
// Main class
// To add image into a word document
public class GFG {
  
    // Main driver method
    public static void main(String[] args) throws Exception
    {
  
        // Step 1: Creating a blank document
        XWPFDocument document = new XWPFDocument();
  
        // Step 2: Creating a Paragraph using
        // createParagraph() method
        XWPFParagraph paragraph
            = document.createParagraph();
        XWPFRun run = paragraph.createRun();
  
        // Step 3: Creating a File output stream of word
        // document at the required location
        FileOutputStream fout = new FileOutputStream(
            new File("D:\\WordFile.docx"));
  
        // Step 4: Creating a file input stream of image by
        // specifying its path
        File image = new File("D:\\Images\\image.jpg");
        FileInputStream imageData
            = new FileInputStream(image);
  
        // Step 5: Retrieving the image file name and image
        // type
        int imageType = XWPFDocument.PICTURE_TYPE_JPEG;
        String imageFileName = image.getName();
  
        // Step 6: Setting the width and height of the image
        // in pixels.
        int width = 450;
        int height = 400;
  
        // Step 7: Adding the picture using the addPicture()
        // method and writing into the document
        run.addPicture(imageData, imageType, imageFileName,
                       Units.toEMU(width),
                       Units.toEMU(height));
        document.write(fout);
  
        // Step 8: Closing the connections
        fout.close();
        document.close();
    }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads