PPTX
Images
Embed pictures, set slide backgrounds, and use image fills on shapes
@office-open/pptx supports embedding images into slides, setting background images, and using images as shape fills.
Embedding a Picture
import { Slide, Picture } from "@office-open/pptx";
import fs from "node:fs";
new Slide({
children: [
new Picture({
x: 1,
y: 1,
width: 4,
height: 3,
data: fs.readFileSync("photo.png"),
imageType: "png",
}),
],
});
Image from URL (Browser)
const response = await fetch("https://example.com/image.png");
const arrayBuffer = await response.arrayBuffer();
const data = new Uint8Array(arrayBuffer);
new Picture({
x: 1,
y: 1,
width: 5,
height: 3,
data,
imageType: "png",
});
Supported Image Types
| Type | Extension | imageType value |
|---|---|---|
| JPEG | .jpg / .jpeg | "jpg" |
| PNG | .png | "png" |
| GIF | .gif | "gif" |
| BMP | .bmp | "bmp" |
| SVG | .svg | "svg" |
| TIFF | .tiff | "tiff" |
Slide Background Image
Set a background image for the entire slide:
import { Slide } from "@office-open/pptx";
import fs from "node:fs";
new Slide({
background: {
type: "image",
data: fs.readFileSync("background.png"),
imageType: "png",
},
children: [
// Your slide content goes on top of the background
],
});
Solid Color Background
new Slide({
background: { type: "solid", color: "1F4E79" },
children: [],
});
Gradient Background
new Slide({
background: {
type: "gradient",
stops: [
{ position: 0, color: "0D47A1" },
{ position: 100, color: "42A5F5" },
],
angle: 45,
},
children: [],
});
Image Fill (Blip Fill) on Shapes
Use an image as a fill for any shape:
import { Shape } from "@office-open/pptx";
import fs from "node:fs";
new Shape({
x: 1,
y: 1,
width: 6,
height: 4,
geometry: "roundRect",
fill: {
type: "blip",
data: fs.readFileSync("texture.png"),
imageType: "png",
},
text: "Text over image fill",
paragraphs: [
new Paragraph({
children: [
new TextRun({
text: "Shape with image fill",
fontSize: 24,
bold: true,
color: "FFFFFF",
}),
],
}),
],
});
Sizing and Positioning
All position and size values are in inches:
new Picture({
x: 1, // Left position (inches)
y: 0.5, // Top position (inches)
width: 8, // Width (inches)
height: 4.5, // Height (inches)
data: imageData,
imageType: "png",
});
Use convertPixelsToEmu for pixel-to-EMU conversions when needed:
import { convertPixelsToEmu } from "@office-open/pptx";
const emu = convertPixelsToEmu(800); // Convert 800 pixels to EMU