Files
docx-js/demo/47-number-of-total-pages-section.ts

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-08-09 11:56:22 +03:00
// Multiple sections with total number of pages in each section
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2019-11-21 01:12:32 +00:00
import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build";
2019-08-09 11:56:22 +03:00
const doc = new Document();
2019-10-10 01:08:01 +01:00
const header = new Header({
children: [
new Paragraph({
children: [
new TextRun("Header on another page"),
2019-11-21 01:12:32 +00:00
new TextRun({
children: ["Page number: ", PageNumber.CURRENT],
}),
2019-10-10 01:08:01 +01:00
new TextRun(" to ").numberOfTotalPagesSection(),
],
alignment: AlignmentType.CENTER,
}),
],
});
2019-08-09 11:56:22 +03:00
2019-10-10 01:08:01 +01:00
const footer = new Footer({
children: [new Paragraph("Foo Bar corp. ")],
});
2019-08-09 11:56:22 +03:00
doc.addSection({
headers: {
default: header,
},
footers: {
default: footer,
},
2019-10-10 01:08:01 +01:00
properties: {
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
new Paragraph({
children: [new TextRun("Section 1"), new PageBreak(), new TextRun("Section 1"), new PageBreak()],
}),
],
2019-08-09 11:56:22 +03:00
});
doc.addSection({
headers: {
default: header,
},
footers: {
default: footer,
},
2019-10-10 01:08:01 +01:00
properties: {
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
new Paragraph({
children: [new TextRun("Section 2"), new PageBreak(), new TextRun("Section 2"), new PageBreak()],
}),
],
2019-08-09 11:56:22 +03:00
});
2019-10-10 01:08:01 +01:00
Packer.toBuffer(doc).then((buffer) => {
2019-08-09 11:56:22 +03:00
fs.writeFileSync("My Document.docx", buffer);
});