Files
docx-js/demo/demo39.ts

38 lines
1.1 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-06-13 01:07:00 +01:00
import { AlignmentType, Document, Packer, PageNumberFormat, Paragraph, TextRun } from "../build";
2019-01-16 00:13:18 +00:00
const doc = new Document(
{},
{
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
);
2019-06-25 23:17:56 +01:00
doc.Header.add(
2019-06-13 01:07:00 +01:00
new Paragraph("Foo Bar corp. ").addRun(new TextRun("Page Number ").pageNumber()).addRun(new TextRun(" to ").numberOfTotalPages()),
);
2019-01-16 00:13:18 +00:00
2019-06-25 23:17:56 +01:00
doc.Footer.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "Foo Bar corp. ",
alignment: AlignmentType.CENTER,
})
.addRun(new TextRun("Page Number: ").pageNumber())
.addRun(new TextRun(" to ").numberOfTotalPages()),
);
2019-01-16 00:13:18 +00:00
2019-06-25 23:17:56 +01:00
doc.add(new Paragraph("Hello World 1").pageBreak());
doc.add(new Paragraph("Hello World 2").pageBreak());
doc.add(new Paragraph("Hello World 3").pageBreak());
doc.add(new Paragraph("Hello World 4").pageBreak());
doc.add(new Paragraph("Hello World 5").pageBreak());
2019-01-16 00:13:18 +00:00
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});