Files
docx-js/demo/5-images.ts

177 lines
6.7 KiB
TypeScript
Raw Permalink Normal View History

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 {
convertMillimetersToTwip,
2019-07-31 08:48:02 +01:00
Document,
HorizontalPositionAlign,
HorizontalPositionRelativeFrom,
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: [
{
children: [
2021-03-19 20:53:56 +00:00
new Paragraph("Hello World"),
new Paragraph({
children: [
new ImageRun({
type: "jpg",
2021-03-19 20:53:56 +00:00
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-19 20:53:56 +00:00
new Paragraph({
children: [
new ImageRun({
type: "png",
2021-03-19 20:53:56 +00:00
data: fs.readFileSync("./demo/images/dog.png").toString("base64"),
transformation: {
width: 100,
height: 100,
},
outline: {
type: "solidFill",
solidFillType: "rgb",
value: "FF0000",
},
2021-03-19 20:53:56 +00:00
}),
],
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
children: [
new ImageRun({
type: "jpg",
2021-03-19 20:53:56 +00:00
data: fs.readFileSync("./demo/images/cat.jpg"),
transformation: {
width: 100,
height: 100,
flip: {
vertical: true,
},
},
outline: {
type: "solidFill",
solidFillType: "rgb",
value: "0000FF",
width: convertMillimetersToTwip(600),
},
2021-03-19 20:53:56 +00:00
}),
],
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
children: [
new ImageRun({
type: "bmp",
2021-03-19 20:53:56 +00:00
data: fs.readFileSync("./demo/images/parrots.bmp"),
transformation: {
width: 150,
height: 150,
flip: {
horizontal: true,
},
rotation: 225,
},
}),
],
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
children: [
new ImageRun({
type: "gif",
2021-03-19 20:53:56 +00:00
data: fs.readFileSync("./demo/images/pizza.gif"),
transformation: {
width: 200,
height: 200,
flip: {
horizontal: true,
vertical: true,
},
},
}),
],
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
children: [
new ImageRun({
type: "gif",
2021-03-19 20:53:56 +00:00
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-19 20:53:56 +00:00
new Paragraph({
children: [
new ImageRun({
type: "jpg",
2021-03-19 20:53:56 +00:00
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,
},
},
}),
],
}),
new Paragraph({
children: [
new ImageRun({
type: "svg",
data: fs.readFileSync("./demo/images/linux-svg.svg"),
transformation: {
width: 200,
height: 200,
},
fallback: {
type: "png",
data: fs.readFileSync("./demo/images/linux-png.png"),
},
}),
],
}),
],
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);
});