Files
docx-js/demo/85-template-document.ts

119 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-02-08 03:18:11 +00:00
// Simple template example
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2023-02-25 19:33:12 +00:00
import {
HeadingLevel,
Paragraph,
patchDocument,
PatchType,
Table,
TableCell,
TableRow,
TextDirection,
TextRun,
VerticalAlign,
} from "../src";
2023-02-08 03:18:11 +00:00
2023-02-16 20:17:48 +00:00
patchDocument(fs.readFileSync("demo/assets/simple-template.docx"), {
2023-02-17 10:38:03 +00:00
patches: [
{
2023-02-25 19:33:12 +00:00
type: PatchType.PARAGRAPH,
children: [new TextRun("Sir. "), new TextRun("John Doe"), new TextRun("(The Conqueror)")],
2023-02-17 10:38:03 +00:00
text: "{{ name }}",
},
2023-02-24 01:05:43 +00:00
{
2023-02-25 19:33:12 +00:00
type: PatchType.PARAGRAPH,
2023-02-24 01:05:43 +00:00
children: [new TextRun("Heading wow!")],
text: "{{ table_heading_1 }}",
},
{
2023-02-25 19:33:12 +00:00
type: PatchType.PARAGRAPH,
2023-02-24 01:05:43 +00:00
children: [new TextRun("#657")],
text: "{{ item_1 }}",
},
{
2023-02-25 19:33:12 +00:00
type: PatchType.DOCUMENT,
children: [new Paragraph("Lorem ipsum paragraph"), new Paragraph("Another paragraph")],
2023-02-24 01:05:43 +00:00
text: "{{ paragraph_replace }}",
},
{
type: PatchType.PARAGRAPH,
children: [new TextRun("Delightful Header")],
text: "{{ header_adjective }}",
},
{
type: PatchType.PARAGRAPH,
children: [new TextRun("replaced just as well")],
text: "{{ footer_text }}",
},
2023-02-25 19:33:12 +00:00
{
type: PatchType.DOCUMENT,
children: [
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})],
textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT,
}),
new TableCell({
children: [new Paragraph({ text: "top to bottom" }), new Paragraph({})],
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [
new Paragraph({
text: "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah",
heading: HeadingLevel.HEADING_1,
}),
],
}),
new TableCell({
children: [
new Paragraph({
text: "This text should be in the middle of the cell",
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: "Text above should be vertical from bottom to top",
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: "Text above should be vertical from top to bottom",
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
],
}),
],
}),
],
text: "{{ table }}",
},
2023-02-17 10:38:03 +00:00
],
2023-02-16 20:17:48 +00:00
}).then((doc) => {
2023-02-08 03:18:11 +00:00
fs.writeFileSync("My Document.docx", doc);
});