Files
docx-js/demo/39-page-numbers.ts

46 lines
1.4 KiB
TypeScript
Raw Normal View History

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-07-31 08:48:02 +01:00
import { AlignmentType, Document, Footer, Header, Packer, 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: [
new Paragraph("Foo Bar corp. ")
.addRun(new TextRun("Page Number ").pageNumber())
.addRun(new TextRun(" to ").numberOfTotalPages()),
],
}),
},
footers: {
default: new Footer({
children: [
new Paragraph({
text: "Foo Bar corp. ",
alignment: AlignmentType.CENTER,
})
.addRun(new TextRun("Page Number: ").pageNumber())
.addRun(new TextRun(" to ").numberOfTotalPages()),
],
}),
},
properties: {
2019-01-16 00:13:18 +00:00
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
2019-07-31 08:48:02 +01:00
children: [
new Paragraph("Hello World 1").pageBreak(),
new Paragraph("Hello World 2").pageBreak(),
new Paragraph("Hello World 3").pageBreak(),
new Paragraph("Hello World 4").pageBreak(),
new Paragraph("Hello World 5").pageBreak(),
],
});
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);
});