Files
docx-js/demo/9-images-in-header-and-footer.ts

36 lines
952 B
TypeScript
Raw Permalink Normal View History

2018-08-21 02:46:21 +01:00
// Add images to header and footer
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2019-07-31 08:48:02 +01:00
import { Document, Footer, Header, Media, Packer, Paragraph } from "../build";
2018-08-21 02:46:21 +01:00
const doc = new Document();
2019-07-31 08:48:02 +01:00
const image1 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
const image2 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
2018-08-21 02:46:21 +01:00
2019-07-31 08:48:02 +01:00
doc.addSection({
headers: {
default: new Header({
children: [
new Paragraph({
children: [image1],
}),
],
}),
},
footers: {
default: new Footer({
children: [
new Paragraph({
children: [image2],
}),
],
}),
},
children: [new Paragraph("Hello World")],
});
2018-08-21 02:46:21 +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);
});