Files
docx-js/demo/36-image-to-table-cell.ts
2023-06-05 00:33:43 +01:00

86 lines
2.2 KiB
TypeScript

// Add image to table cell in a header and body
import * as fs from "fs";
import { Document, Header, ImageRun, Packer, Paragraph, Table, TableCell, TableRow } from "docx";
const table = 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 TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [],
}),
new TableCell({
children: [],
}),
],
}),
],
});
// Adding same table in the body and in the header
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [table],
}),
},
children: [table],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});