2018-08-21 02:46:21 +01:00
|
|
|
// Example of how to add images to the document - You can use Buffers, UInt8Arrays or Base64 strings
|
2023-06-05 00:33:43 +01:00
|
|
|
|
2018-08-21 02:46:21 +01:00
|
|
|
import * as fs from "fs";
|
2019-07-31 08:48:02 +01:00
|
|
|
import {
|
|
|
|
Document,
|
|
|
|
HorizontalPositionAlign,
|
|
|
|
HorizontalPositionRelativeFrom,
|
2021-03-18 02:48:37 +00:00
|
|
|
ImageRun,
|
2019-07-31 08:48:02 +01:00
|
|
|
Packer,
|
|
|
|
Paragraph,
|
|
|
|
VerticalPositionAlign,
|
|
|
|
VerticalPositionRelativeFrom,
|
2023-06-05 00:33:43 +01:00
|
|
|
} from "docx";
|
2018-08-21 02:46:21 +01:00
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
const doc = new Document({
|
|
|
|
sections: [
|
|
|
|
{
|
2021-03-18 02:48:37 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph("Hello World"),
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/image1.jpeg"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
2022-10-26 23:09:36 +01:00
|
|
|
altText: {
|
|
|
|
title: "This is an ultimate title",
|
|
|
|
description: "This is an ultimate image",
|
|
|
|
name: "My Ultimate Image",
|
|
|
|
},
|
2021-03-19 20:53:56 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/dog.png").toString("base64"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/cat.jpg"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
flip: {
|
|
|
|
vertical: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/parrots.bmp"),
|
|
|
|
transformation: {
|
|
|
|
width: 150,
|
|
|
|
height: 150,
|
|
|
|
flip: {
|
|
|
|
horizontal: true,
|
|
|
|
},
|
|
|
|
rotation: 225,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/pizza.gif"),
|
|
|
|
transformation: {
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
flip: {
|
|
|
|
horizontal: true,
|
|
|
|
vertical: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/pizza.gif"),
|
|
|
|
transformation: {
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
rotation: 45,
|
|
|
|
},
|
|
|
|
floating: {
|
|
|
|
zIndex: 10,
|
|
|
|
horizontalPosition: {
|
|
|
|
offset: 1014400,
|
|
|
|
},
|
|
|
|
verticalPosition: {
|
|
|
|
offset: 1014400,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: fs.readFileSync("./demo/images/cat.jpg"),
|
|
|
|
transformation: {
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
},
|
|
|
|
floating: {
|
|
|
|
zIndex: 5,
|
|
|
|
horizontalPosition: {
|
|
|
|
relative: HorizontalPositionRelativeFrom.PAGE,
|
|
|
|
align: HorizontalPositionAlign.RIGHT,
|
|
|
|
},
|
|
|
|
verticalPosition: {
|
|
|
|
relative: VerticalPositionRelativeFrom.PAGE,
|
|
|
|
align: VerticalPositionAlign.BOTTOM,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-03-18 02:48:37 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
2019-07-31 08:48:02 +01:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2018-08-21 02:46:21 +01:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|