Export Rich Text with Images to Word Documents Using Java

Required Dependencies

To manipulate Word documents and handle embedded images, we rely on Apache POI for core Word processing and the XWPF XHTML converter for rich text support. Add these Maven dependencies to you're project:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
    <version>1.0.6</version>
</dependency>

Core Implementation

The following clas ancapsulates the logic to generate a Word document with both rich text content and embedded images. We use try-with-resources to ensure proper resource management:

import org.apache.poi.xwpf.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class RichTextWordGenerator {
    public void generateDocument(String richTextContent, String sourceImagePath, String targetDocPath) throws IOException {
        // Create a new empty Word document
        try (XWPFDocument doc = new XWPFDocument()) {
            // Add a paragraph for the rich text content
            XWPFParagraph textParagraph = doc.createParagraph();
            XWPFRun textRun = textParagraph.createRun();
            textRun.setText(richTextContent);

            // Add a new paragraph for the image
            XWPFParagraph imageParagraph = doc.createParagraph();
            XWPFRun imageRun = imageParagraph.createRun();
            
            // Embed the image into the document
            try (FileInputStream imageStream = new FileInputStream(new File(sourceImagePath))) {
                imageRun.addPicture(imageStream, XWPFDocument.PICTURE_TYPE_JPEG, sourceImagePath, 
                        Units.toEMU(300), Units.toEMU(300));
            }

            // Write the document to the target file
            try (FileOutputStream outputStream = new FileOutputStream(new File(targetDocPath))) {
                doc.write(outputStream);
            }
        }
    }
}

Usage Example

To use the generator class, call the method with your rich text content, image path, and target document path:

public class DocumentGenerationDemo {
    public static void main(String[] args) {
        RichTextWordGenerator generator = new RichTextWordGenerator();
        String sampleRichText = "This is a sample piece of rich text content that will appear in the Word document.";
        String imageLocation = "assets/sample-image.jpg";
        String outputDocument = "generated-document.docx";

        try {
            generator.generateDocument(sampleRichText, imageLocation, outputDocument);
            System.out.println("Word document generated successfully!");
        } catch (IOException e) {
            System.err.println("Failed to generate document: " + e.getMessage());
        }
    }
}

Tags: java Apache POI Word Document Generation Rich Text Export Image Embedding in Word

Posted on Fri, 07 Aug 2026 16:19:01 +0000 by tijger