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
|
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2019-01-09 01:16:47 +00:00
|
|
|
// import { Document, Packer, Paragraph } from "../build";
|
2019-07-31 08:48:02 +01:00
|
|
|
import {
|
|
|
|
Document,
|
|
|
|
HorizontalPositionAlign,
|
|
|
|
HorizontalPositionRelativeFrom,
|
|
|
|
Media,
|
|
|
|
Packer,
|
|
|
|
Paragraph,
|
|
|
|
VerticalPositionAlign,
|
|
|
|
VerticalPositionRelativeFrom,
|
|
|
|
} 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/image1.jpeg"));
|
|
|
|
const image2 = Media.addImage(doc, fs.readFileSync("./demo/images/dog.png").toString("base64"));
|
|
|
|
const image3 = Media.addImage(doc, fs.readFileSync("./demo/images/cat.jpg"));
|
|
|
|
const image4 = Media.addImage(doc, fs.readFileSync("./demo/images/parrots.bmp"));
|
|
|
|
const image5 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
|
|
|
|
const image6 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
2019-01-09 01:16:47 +00:00
|
|
|
floating: {
|
2020-12-25 00:07:57 +00:00
|
|
|
zIndex: 10,
|
2019-01-09 01:16:47 +00:00
|
|
|
horizontalPosition: {
|
|
|
|
offset: 1014400,
|
|
|
|
},
|
|
|
|
verticalPosition: {
|
|
|
|
offset: 1014400,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
const image7 = Media.addImage(doc, fs.readFileSync("./demo/images/cat.jpg"), 200, 200, {
|
2019-01-09 01:16:47 +00:00
|
|
|
floating: {
|
2020-12-25 00:07:57 +00:00
|
|
|
zIndex: 5,
|
2019-01-09 01:16:47 +00:00
|
|
|
horizontalPosition: {
|
|
|
|
relative: HorizontalPositionRelativeFrom.PAGE,
|
|
|
|
align: HorizontalPositionAlign.RIGHT,
|
|
|
|
},
|
|
|
|
verticalPosition: {
|
|
|
|
relative: VerticalPositionRelativeFrom.PAGE,
|
|
|
|
align: VerticalPositionAlign.BOTTOM,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2018-08-21 02:46:21 +01:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
doc.addSection({
|
|
|
|
children: [
|
|
|
|
new Paragraph("Hello World"),
|
|
|
|
new Paragraph(image1),
|
|
|
|
new Paragraph(image2),
|
|
|
|
new Paragraph(image3),
|
|
|
|
new Paragraph(image4),
|
|
|
|
new Paragraph(image5),
|
|
|
|
new Paragraph(image6),
|
|
|
|
new Paragraph(image7),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|