14 KiB
Images
!> Images requires an understanding of Sections and Paragraphs.
?> To draw rectangles, lines, arrows and other shapes instead of pictures, see Shapes.
Common Use Cases
| I want to... | Use | Example |
|---|---|---|
| Insert an image inline with text | Inline (default) | Logo next to company name |
| Position image at specific coordinates | Floating with offset | Letterhead logo at top-right |
| Have text wrap around an image | Floating with wrap | Magazine-style layout |
| Put an image in a table cell | Inline in TableCell | Product catalog |
| Add image to header/footer | Inline in Header/Footer | Company letterhead |
| Trim edges off an image | crop option |
Removing unwanted borders |
Inline vs Floating: When to Use Which
Use Inline when:
- Image should flow with text
- Image is part of content (diagrams, screenshots)
- Image goes inside tables
- Image in headers/footers
Use Floating when:
- Image needs precise positioning
- Text should wrap around image
- Image overlays content (watermarks)
- Complex page layouts
To create a floating image on top of text:
const image = new ImageRun({
type: 'gif',
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 200,
height: 200,
}
floating: {
horizontalPosition: {
offset: 1014400,
},
verticalPosition: {
offset: 1014400,
},
},
});
By default with no arguments, its an inline image:
const image = new ImageRun({
type: "gif",
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 100,
height: 100,
},
});
Add it into the document by adding the image into a paragraph:
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [image],
}),
],
}];
});
Intro
Adding images can be easily done by creating an instance of ImageRun. This can be added in a Paragraph or Hyperlink:
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [
new ImageRun({
type: [IMAGE_TYPE],
data: [IMAGE_BUFFER],
transformation: {
width: [IMAGE_SIZE],
height: [IMAGE_SIZE],
},
}),
],
}),
],
}];
});
docx supports jpeg, jpg, bmp, gif and png
Positioning
Positioning is the method on how to place the image on the document
Three types of image positioning is supported:
- Floating
- Inline
By default, images are exported as Inline elements.
Usage
Pass options into the [POSITION_OPTIONS] mentioned in the Intro above.
Floating
To change the position the image to be on top of the text, simply add the floating property to the last argument. By default, the offsets are relative to the top left corner of the page. Offset units are in emus:
const image = new ImageRun({
type: "png",
data: buffer,
transformation: {
width: 903,
height: 1149,
},
floating: {
horizontalPosition: {
offset: 1014400, // relative: HorizontalPositionRelativeFrom.PAGE by default
},
verticalPosition: {
offset: 1014400, // relative: VerticalPositionRelativeFrom.PAGE by default
},
},
});
const image = new ImageRun({
type: "png",
data: buffer,
transformation: {
width: 903,
height: 1149,
},
floating: {
horizontalPosition: {
relative: HorizontalPositionRelativeFrom.RIGHT_MARGIN,
offset: 1014400,
},
verticalPosition: {
relative: VerticalPositionRelativeFrom.BOTTOM_MARGIN,
offset: 1014400,
},
},
});
Options
Full options you can pass into floating are:
| Property | Type | Notes |
|---|---|---|
| horizontalPosition | HorizontalPositionOptions |
Required |
| verticalPosition | VerticalPositionOptions |
Required |
| allowOverlap | boolean |
Optional |
| lockAnchor | boolean |
Optional |
| behindDocument | boolean |
Optional |
| layoutInCell | boolean |
Optional |
| zIndex | number |
Optional |
HorizontalPositionOptions are:
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| relative | HorizontalPositionRelativeFrom |
Required | CHARACTER, COLUMN, INSIDE_MARGIN, LEFT_MARGIN, MARGIN, OUTSIDE_MARGIN, PAGE, RIGHT_MARGIN |
| align | HorizontalPositionAlign |
You can either have align or offset, not both |
CENTER, INSIDE, LEFT, OUTSIDE, RIGHT |
| offset | number |
You can either have align or offset, not both |
0 to Infinity |
VerticalPositionOptions are:
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| relative | VerticalPositionRelativeFrom |
Required | BOTTOM_MARGIN, INSIDE_MARGIN, LINE, MARGIN, OUTSIDE_MARGIN, PAGE, PARAGRAPH, TOP_MARGIN |
| align | VerticalPositionAlign |
You can either have align or offset, not both |
BOTTOM, CENTER, INSIDE, OUTSIDE, TOP |
| offset | number |
You can either have align or offset, not both |
0 to Infinity |
Wrap text
Wrapping only works for floating elements. Text will "wrap" around the floating image.
Add wrap options inside the floating options:
wrap: {
type: [TextWrappingType],
side: [TextWrappingSide],
},
For example:
const image = new ImageRun({
type: "gif",
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 200,
height: 200,
},
floating: {
horizontalPosition: {
offset: 2014400,
},
verticalPosition: {
offset: 2014400,
},
wrap: {
type: TextWrappingType.SQUARE,
side: TextWrappingSide.BOTH_SIDES,
},
},
});
Wrap options have the following properties are:
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| type | TextWrappingType |
Optional | NONE, SQUARE, TIGHT, THROUGH, TOP_AND_BOTTOM |
| side | TextWrappingSide |
Optional | BOTH_SIDES, LEFT, RIGHT, LARGEST |
Margins
Margins give some space between the text and the image. Margins only work for floating elements. Additionally, the image must also be in wrap mode (see above).
?> Be sure to also set wrap in your options!
To use, add the margins options inside the floating options:
margins: {
top: number,
bottom: number,
left: number,
right: number
},
For example:
const image = new ImageRun({
type: "gif",
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 200,
height: 200,
},
floating: {
horizontalPosition: {
offset: 2014400,
},
verticalPosition: {
offset: 2014400,
},
wrap: {
type: TextWrappingType.SQUARE,
side: TextWrappingSide.BOTH_SIDES,
},
margins: {
top: 201440,
bottom: 201440,
},
},
});
Alternative Text
Specifies common non-visual DrawingML properties. A name, title and description for a picture can be specified.
const image = new ImageRun({
type: "gif",
data: fs.readFileSync("./demo/images/pizza.gif"),
altText: {
title: "This is an ultimate title",
description: "This is an ultimate image",
name: "My Ultimate Image",
},
});
Options
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| name | string |
Required | Specimen A |
| title | string |
Required | My awesome title of my image |
| description | string |
Required | My awesome description of my image |
Links and Decorative Images
Give an image a link to open a web page when it is clicked (with Ctrl in Word), the same as Word's Insert > Link on a picture:
const image = new ImageRun({
type: "png",
data: fs.readFileSync("./logo.png"),
transformation: { width: 120, height: 40 },
altText: { name: "Logo", description: "Company logo", title: "Logo" },
link: "https://example.com",
});
An image inside an ExternalHyperlink links to the hyperlink's address already. If it has a link of its own, its own link is used.
Mark an image as decorative when it carries no information, such as a border or a flourish, so screen readers skip it. It is the same as Word's Mark as decorative, and is used instead of alternative text:
const flourish = new ImageRun({
type: "png",
data: fs.readFileSync("./flourish.png"),
transformation: { width: 300, height: 20 },
decorative: true,
});
Both work for inline and floating images.
Options
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| link | string |
Optional | "https://example.com" |
| decorative | boolean |
Optional | true |
Cropping
Crop an image by trimming a percentage off each edge before it's stretched to fill its frame. Pass a crop property to ImageRun with left, top, right and/or bottom percentages (0 to 100):
const image = new ImageRun({
type: "png",
data: fs.readFileSync("./demo/images/pizza.png"),
transformation: {
width: 200,
height: 200,
},
crop: {
left: 10,
top: 5,
right: 10,
bottom: 5,
},
});
Options
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| left | number |
Optional | 0 to 100 |
| top | number |
Optional | 0 to 100 |
| right | number |
Optional | 0 to 100 |
| bottom | number |
Optional | 0 to 100 |
Track Changes
Images can be marked as inserted or deleted revisions for change tracking. Pass an insertion or deletion property to ImageRun:
new ImageRun({
type: "png",
data: fs.readFileSync("./image.png"),
transformation: { width: 120, height: 120 },
insertion: {
id: 30,
author: "Firstname Lastname",
date: "2020-10-06T09:00:00Z",
},
});
See Change Tracking – Image Revisions for full details and examples.
Examples
Add image to the document
Importing Images from file system path
Source: https://github.com/dolanmiu/docx/blob/master/demo/5-images.ts
Add images to header and footer
Example showing how to add image to headers and footers
Source: https://github.com/dolanmiu/docx/blob/master/demo/9-images-in-header-and-footer.ts
Floating images
Example showing how to float images on top of text and optimally give a margin
Source: https://github.com/dolanmiu/docx/blob/master/demo/38-text-wrapping.ts
Links and decorative images
Source: https://github.com/dolanmiu/docx/blob/master/demo/114-image-links.ts
