Images

Embed images in docx documents with position, size, and rotation.

Embedding an Image

using DotnetPoi.XWPF.UserModel;

using var doc = new XWPFDocument();
var imageBytes = File.ReadAllBytes("photo.jpg");

var imagePara = doc.createParagraph();
var imageRun = imagePara.createRun();
var picture = imageRun.addPicture(
    imageBytes,
    XWPFPictureData.PICTURE_TYPE_JPEG,
    "photo.jpg",
    width:  914400,    // 1 inch in EMU
    height: 914400);

picture.setRotation(45.0);  // rotate 45 degrees

Image Dimensions

Dimensions are in EMU (English Metric Units):

UnitEMU
1 inch914400
1 cm360000
1 point12700

Supported Image Types

ConstantFormat
PICTURE_TYPE_JPEGJPEG
PICTURE_TYPE_PNGPNG
PICTURE_TYPE_GIFGIF
PICTURE_TYPE_BMPBMP

Reading Images Back

var allPictures = doc.getAllPictures();
var pic = allPictures[0];
var picType = pic.getPictureType();  // PICTURE_TYPE_JPEG

// Check image count per run
var runs = paragraphs[0].getRuns();
var embedded = runs[0].getEmbeddedPictures();
var rotation = embedded[0].getRotation();

Floating Images

Floating or anchored images (wp:anchor) are preserved when an existing document is read and written again. dotnet-poi currently models inline images for API-level creation; anchored positioning, wrapping, and floating-image editing are preserved as raw OOXML rather than exposed as public setters.

Full Runnable Example

See examples/Phase32DocxExample/ and examples/UsageSamples/Program.cs (CreateDocument):

examples/Phase32DocxExample

examples/UsageSamples