Files
docx-js/demo/16-multiple-sections.ts

113 lines
2.9 KiB
TypeScript
Raw Normal View History

2018-08-21 02:46:21 +01:00
// Multiple sections and headers
// 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 { Document, Footer, Header, Packer, PageNumberFormat, PageOrientation, Paragraph, TextRun } from "../build";
2018-08-21 02:46:21 +01:00
const doc = new Document();
2019-07-31 08:48:02 +01:00
doc.addSection({
children: [new Paragraph("Hello World").pageBreak()],
});
2018-08-21 02:46:21 +01:00
doc.addSection({
2019-07-31 08:48:02 +01:00
headers: {
default: new Header({
children: [new Paragraph("First Default Header on another page")],
}),
},
footers: {
default: new Footer({
children: [new Paragraph("Footer on another page")],
}),
},
2019-07-07 03:54:29 +01:00
properties: {
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
2018-10-05 01:33:17 +01:00
},
2019-07-07 03:54:29 +01:00
children: [new Paragraph("hello")],
2018-08-21 02:46:21 +01:00
});
doc.addSection({
2019-07-31 08:48:02 +01:00
headers: {
default: new Header({
children: [new Paragraph("Second Default Header on another page")],
}),
},
footers: {
default: new Footer({
children: [new Paragraph("Footer on another page")],
}),
},
size: {
orientation: PageOrientation.LANDSCAPE,
},
2019-07-07 03:54:29 +01:00
properties: {
pageNumberStart: 1,
pageNumberFormatType: PageNumberFormat.DECIMAL,
2018-10-05 01:33:17 +01:00
},
2019-07-07 03:54:29 +01:00
children: [new Paragraph("hello in landscape")],
2018-08-21 02:46:21 +01:00
});
doc.addSection({
2019-07-31 08:48:02 +01:00
headers: {
default: new Header({
children: [
new Paragraph({
children: [new TextRun("Page number: ").pageNumber()],
}),
],
}),
},
size: {
2019-07-07 03:54:29 +01:00
orientation: PageOrientation.PORTRAIT,
},
2019-07-07 03:54:29 +01:00
children: [new Paragraph("Page number in the header must be 2, because it continues from the previous section.")],
});
doc.addSection({
2019-07-31 08:48:02 +01:00
headers: {
default: new Header({
children: [
new Paragraph({
children: [new TextRun("Page number: ").pageNumber()],
}),
],
}),
},
2019-07-07 03:54:29 +01:00
properties: {
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
orientation: PageOrientation.PORTRAIT,
},
2019-07-07 03:54:29 +01:00
children: [
new Paragraph(
"Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.",
),
],
});
doc.addSection({
2019-07-31 08:48:02 +01:00
headers: {
default: new Header({
children: [
new Paragraph({
children: [new TextRun("Page number: ").pageNumber()],
}),
],
}),
},
size: {
orientation: PageOrientation.PORTRAIT,
},
2019-07-07 03:54:29 +01:00
properties: {
pageNumberFormatType: PageNumberFormat.DECIMAL,
pageNumberStart: 25,
},
2019-07-07 03:54:29 +01:00
children: [
new Paragraph("Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section."),
],
});
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);
});