Files
docx-js/demo/24-images-to-table-cell.ts

98 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-08-21 02:46:21 +01:00
// Add image to table cell
2023-06-05 00:33:43 +01:00
2018-08-21 02:46:21 +01:00
import * as fs from "fs";
2023-06-05 00:33:43 +01:00
import { Document, ImageRun, Packer, Paragraph, Table, TableCell, TableRow } from "docx";
2018-08-21 02:46:21 +01:00
2021-03-19 20:53:56 +00:00
const doc = new Document({
sections: [
{
2019-09-13 00:51:20 +01:00
children: [
2021-03-19 20:53:56 +00:00
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [
new Paragraph({
children: [
new ImageRun({
data: fs.readFileSync("./demo/images/image1.jpeg"),
transformation: {
width: 100,
height: 100,
},
}),
],
}),
],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [new Paragraph("Hello")],
}),
new TableCell({
children: [],
}),
],
}),
new TableRow({
children: [
2021-03-19 20:53:56 +00:00
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
],
2019-09-13 00:51:20 +01:00
}),
],
2021-03-19 20:53:56 +00:00
},
2019-09-13 00:51:20 +01:00
],
2019-03-18 23:50:21 +00:00
});
2019-06-25 01:21:28 +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);
});