2021-02-22 21:04:02 +00:00
|
|
|
// Usage of different Section Types
|
2023-06-05 00:33:43 +01:00
|
|
|
|
2021-02-22 21:04:02 +00:00
|
|
|
import * as fs from "fs";
|
2023-06-05 00:33:43 +01:00
|
|
|
import { Document, Packer, Paragraph, TextRun, SectionType } from "docx";
|
2021-02-22 21:04:02 +00:00
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
const doc = new Document({
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
properties: {},
|
2021-02-22 21:04:02 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Hello World"),
|
|
|
|
new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
],
|
2021-02-22 21:04:02 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
type: SectionType.CONTINUOUS,
|
|
|
|
},
|
2021-02-22 21:04:02 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Hello World"),
|
|
|
|
new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
],
|
2021-02-22 21:04:02 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
type: SectionType.ODD_PAGE,
|
|
|
|
},
|
2021-02-22 21:04:02 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Hello World"),
|
|
|
|
new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
],
|
2021-02-22 21:04:02 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
type: SectionType.EVEN_PAGE,
|
|
|
|
},
|
2021-02-22 21:04:02 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Hello World"),
|
|
|
|
new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
],
|
2021-02-22 21:04:02 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
type: SectionType.NEXT_PAGE,
|
|
|
|
},
|
2021-02-22 21:04:02 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Hello World"),
|
|
|
|
new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
],
|
2021-02-22 21:04:02 +00:00
|
|
|
}),
|
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
2021-02-22 21:04:02 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|