2018-08-04 03:28:27 +01:00
# Images
2019-08-06 17:48:07 +01:00
!> Images requires an understanding of [Sections ](usage/sections.md ) and [Paragraphs ](usage/paragraph.md ).
2019-01-09 01:16:47 +00:00
To create a `floating` image on top of text:
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
```ts
2021-03-15 02:41:37 +00:00
const image = Media.addImage({
document: doc,
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 200,
height: 200,
}
2019-01-09 01:16:47 +00:00
floating: {
horizontalPosition: {
offset: 1014400,
},
verticalPosition: {
offset: 1014400,
},
},
});
```
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
By default with no arguments, its an `inline` image:
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
```ts
2021-03-15 02:41:37 +00:00
const image = Media.addImage({
document: doc,
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 100,
height: 100,
},
});
2018-08-04 03:28:27 +01:00
```
2019-08-06 17:48:07 +01:00
Add it into the document by adding the image into a paragraph:
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
```ts
2019-08-06 17:48:07 +01:00
doc.addSection({
children: [new Paragraph(image)],
});
```
Or:
```ts
doc.addSection({
children: [
new Paragraph({
children: [image],
}),
],
});
2019-01-09 01:16:47 +00:00
```
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
## Intro
Adding images can be done in two ways:
1. Call the `createImage` method to add the image directly into the `document` :
2019-08-06 17:48:07 +01:00
```ts
2021-03-15 02:41:37 +00:00
Media.addImage({});
2019-01-09 01:16:47 +00:00
```
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
2. Create an `image` first, then add it into the `document` :
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
```ts
2021-03-15 02:41:37 +00:00
const image = Media.addImage({
document: doc,
data: [IMAGE_BUFFER],
transformation: {
width: [IMAGE_SIZE],
height: [IMAGE_SIZE],
},
});
2019-08-06 17:48:07 +01:00
doc.addSection({
children: [new Paragraph(image)],
});
2019-01-09 01:16:47 +00:00
```
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
`docx` supports `jpeg` , `jpg` , `bmp` , `gif` and `png`
## Positioning
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
> Positioning is the method on how to place the image on the document
2018-08-04 03:28:27 +01:00

2019-01-09 01:16:47 +00:00
Three types of image positioning is supported:
- Floating
- Inline
By default, picture are exported as `Inline` elements.
2018-08-04 03:28:27 +01:00
### Usage
2019-01-09 01:16:47 +00:00
Pass `options` into the `[POSITION_OPTIONS]` metioned in the [Intro above ](#Intro ).
2018-08-04 03:28:27 +01:00
2019-01-10 02:10:20 +00:00
## Floating
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
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 ](https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/ ):
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
```ts
2021-03-15 02:41:37 +00:00
const image = Media.addImage({
document: doc,
data: buffer,
transformation: {
width: 903,
height: 1149,
},
2019-01-09 01:16:47 +00:00
floating: {
horizontalPosition: {
offset: 1014400, // relative: HorizontalPositionRelativeFrom.PAGE by default
},
verticalPosition: {
offset: 1014400, // relative: VerticalPositionRelativeFrom.PAGE by default
},
},
});
```
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
```ts
2021-03-15 02:41:37 +00:00
const image = Media.addImage({
document: doc,
data: buffer,
transformation: {
width: 903,
height: 1149,
},
2018-08-04 03:28:27 +01:00
floating: {
horizontalPosition: {
2019-01-09 01:16:47 +00:00
relative: HorizontalPositionRelativeFrom.RIGHT_MARGIN,
offset: 1014400,
2018-08-04 03:28:27 +01:00
},
verticalPosition: {
2019-01-09 01:16:47 +00:00
relative: VerticalPositionRelativeFrom.BOTTOM_MARGIN,
offset: 1014400,
2018-08-04 03:28:27 +01:00
},
},
});
```
2019-01-10 02:10:20 +00:00
### Options
2019-01-09 01:16:47 +00:00
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 |
2020-12-25 00:30:58 +00:00
| zIndex | `number` | Optional |
2019-01-09 01:16:47 +00:00
`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` |
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
`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` |
2018-08-04 03:28:27 +01:00
2019-01-10 02:10:20 +00:00
## Wrap text
2018-08-04 03:28:27 +01:00
2019-01-10 02:10:20 +00:00
Wrapping only works for floating elements. Text will "wrap" around the floating `image` .
2019-01-09 01:16:47 +00:00
2019-01-10 02:10:20 +00:00
Add `wrap` options inside the `floating` options:
2018-08-04 03:28:27 +01:00
2019-01-10 02:10:20 +00:00
```ts
wrap: {
type: [TextWrappingType],
side: [TextWrappingSide],
},
```
2018-08-04 03:28:27 +01:00
2019-01-10 02:10:20 +00:00
For example:
2018-08-04 03:28:27 +01:00
2019-01-10 02:10:20 +00:00
```ts
2021-03-15 02:41:37 +00:00
Media.addImage({
document: doc,
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 200,
height: 200,
},
2019-01-10 02:10:20 +00:00
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` , `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 ](http://officeopenxml.com/drwPicInline.php ). 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:
```ts
margins: {
top: number,
bottom: number,
left: number,
right: number
},
```
For example:
```ts
2021-03-15 02:41:37 +00:00
Media.addImage({
document: doc,
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 200,
height: 200,
},
2019-01-10 02:10:20 +00:00
floating: {
horizontalPosition: {
offset: 2014400,
},
verticalPosition: {
offset: 2014400,
},
wrap: {
type: TextWrappingType.SQUARE,
side: TextWrappingSide.BOTH_SIDES,
},
margins: {
top: 201440,
bottom: 201440,
},
},
});
2018-08-04 03:28:27 +01:00
```
2019-01-09 01:16:47 +00:00
## Examples
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
### Add image to the document
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
Importing Images from file system path
2018-08-04 03:28:27 +01:00
2021-03-15 02:41:37 +00:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/5-images.ts ":include" )
2018-08-04 03:28:27 +01:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/5-images.ts_
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
### Add images to header and footer
2018-08-04 03:28:27 +01:00
2019-01-09 01:16:47 +00:00
Example showing how to add image to headers and footers
2018-08-04 03:28:27 +01:00
2021-03-15 02:41:37 +00:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/9-images-in-header-and-footer.ts ":include" )
2018-08-04 03:28:27 +01:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/9-images-in-header-and-footer.ts_
2019-01-10 02:10:20 +00:00
### Floating images
Example showing how to float images on top of text and optimally give a `margin`
2021-03-15 02:41:37 +00:00
[Example ](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/38-text-wrapping.ts ":include" )
2019-01-10 02:10:20 +00:00
2019-08-20 22:23:14 +01:00
_Source: https://github.com/dolanmiu/docx/blob/master/demo/38-text-wrapping.ts_