2019-02-26 22:20:20 +00:00
|
|
|
// Example how to display page numbers
|
2019-01-16 00:13:18 +00:00
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2019-11-21 01:02:46 +00:00
|
|
|
import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build";
|
2019-01-16 00:13:18 +00:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
const doc = new Document({});
|
|
|
|
|
|
|
|
doc.addSection({
|
|
|
|
headers: {
|
|
|
|
default: new Header({
|
|
|
|
children: [
|
2019-11-21 01:02:46 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Foo Bar corp. "),
|
|
|
|
new TextRun({
|
|
|
|
children: ["Page Number ", PageNumber.CURRENT],
|
|
|
|
}),
|
|
|
|
new TextRun({
|
|
|
|
children: [" to ", PageNumber.TOTAL_PAGES],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
2019-07-31 08:48:02 +01:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
footers: {
|
|
|
|
default: new Footer({
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
alignment: AlignmentType.CENTER,
|
2019-11-21 01:02:46 +00:00
|
|
|
children: [
|
|
|
|
new TextRun("Foo Bar corp. "),
|
|
|
|
new TextRun({
|
|
|
|
children: ["Page Number: ", PageNumber.CURRENT],
|
|
|
|
}),
|
|
|
|
new TextRun({
|
|
|
|
children: [" to ", PageNumber.TOTAL_PAGES],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
2019-07-31 08:48:02 +01:00
|
|
|
],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
properties: {
|
2019-01-16 00:13:18 +00:00
|
|
|
pageNumberStart: 1,
|
|
|
|
pageNumberFormatType: PageNumberFormat.DECIMAL,
|
|
|
|
},
|
2019-07-31 08:48:02 +01:00
|
|
|
children: [
|
2019-11-21 01:02:46 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Hello World 1"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Hello World 2"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Hello World 3"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Hello World 4"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Hello World 5"), new PageBreak()],
|
|
|
|
}),
|
2019-07-31 08:48:02 +01:00
|
|
|
],
|
|
|
|
});
|
2019-01-16 00:13:18 +00:00
|
|
|
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2019-01-16 00:13:18 +00:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|