2021-02-22 21:04:02 +00:00
|
|
|
// Usage of different Section Types
|
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
|
|
|
import { Document, Packer, Paragraph, TextRun, SectionType } from "../build";
|
|
|
|
|
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);
|
|
|
|
});
|