Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Adding Images to a Word Document using Java

  • Difficulty Level : Expert
  • Last Updated : 16 Sep, 2021

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :