DOCX
Images
Insert images into documents with scaling, rotation, cropping, and floating positioning
Use ImageRun to embed images in paragraphs. Supports JPG, PNG, GIF, BMP, SVG, and more.
Basic Image
import { Document, Packer, Paragraph, ImageRun } from "@office-open/docx";
import { readFileSync } from "node:fs";
const imageBuffer = readFileSync("photo.png");
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ImageRun({
type: "png",
data: imageBuffer,
transformation: {
width: 200,
height: 150,
},
}),
],
}),
],
},
],
});
Transformations
Control size, rotation, and flip:
new ImageRun({
type: "jpg",
data: imageBuffer,
transformation: {
width: 300,
height: 200,
rotation: 45, // Rotate 45 degrees
flip: {
horizontal: true, // Flip horizontally
},
},
});
Cropping
Use srcRect to crop the source image:
new ImageRun({
type: "png",
data: imageBuffer,
transformation: {
width: 200,
height: 200,
},
srcRect: {
left: 1000, // Crop left edge (in EMUs)
top: 1000, // Crop top edge (in EMUs)
right: 1000, // Crop right edge (in EMUs)
bottom: 1000, // Crop bottom edge (in EMUs)
},
});
Floating Images
Use the floating option to position images with text wrapping:
new ImageRun({
type: "png",
data: imageBuffer,
transformation: {
width: 150,
height: 150,
},
floating: {
horizontalPosition: {
offset: 720000, // Position from page left (in EMUs)
},
verticalPosition: {
offset: 720000, // Position from page top (in EMUs)
},
wrap: {
type: "square", // Text wraps around the image
},
},
});
Image Effects (Blip Effects)
Apply adjustment effects directly to images using blipEffects:
new ImageRun({
type: "jpg",
data: imageBuffer,
transformation: { width: 150, height: 150 },
blipEffects: {
// Brightness +30%, Contrast -20%
luminance: { bright: 30, contrast: -20 },
},
});
Available Blip Effects
| Property | Type | Description |
|---|---|---|
grayscale | boolean | Convert to grayscale |
luminance | { bright?, contrast? } | Adjust brightness/contrast (%) |
hsl | { hue?, saturation?, luminance? } | Adjust HSL color values |
tint | { hue?, amount? } | Apply color tint |
duotone | { color1, color2 } | Two-color tone mapping |
biLevel | { threshold } | Black & white threshold (0-100) |
Grayscale
new ImageRun({
type: "jpg",
data: imageBuffer,
transformation: { width: 150, height: 150 },
blipEffects: { grayscale: true },
});
Duotone
new ImageRun({
type: "jpg",
data: imageBuffer,
transformation: { width: 150, height: 150 },
blipEffects: {
duotone: {
color1: { value: "002060" },
color2: { value: "D0CECE" },
},
},
});
Tint
new ImageRun({
type: "jpg",
data: imageBuffer,
transformation: { width: 150, height: 150 },
blipEffects: {
tint: { hue: 6000000, amount: 40 },
},
});
SVG with Fallback
new ImageRun({
type: "svg",
data: svgBuffer,
transformation: { width: 200, height: 200 },
fallback: {
type: "png",
data: pngBuffer,
},
});