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

113 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-02-25 22:18:31 +00:00
// Patch a document with patches
2023-02-08 03:18:11 +00:00
// 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-25 22:18:31 +00:00
patches: {
"name":{
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
},
2023-02-25 22:18:31 +00:00
"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("Heading wow!")],
},
2023-02-25 22:18:31 +00:00
"item_1": {
2023-02-25 19:33:12 +00:00
type: PatchType.PARAGRAPH,
2023-02-24 01:05:43 +00:00
children: [new TextRun("#657")],
},
2023-02-25 22:18:31 +00:00
"paragraph_replace": {
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
},
2023-02-25 22:18:31 +00:00
"header_adjective": {
type: PatchType.PARAGRAPH,
children: [new TextRun("Delightful Header")],
},
2023-02-25 22:18:31 +00:00
"footer_text": {
type: PatchType.PARAGRAPH,
children: [new TextRun("replaced just as well")],
},
2023-02-25 22:18:31 +00:00
"table": {
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,
}),
],
}),
],
2023-02-25 22:18:31 +00:00
2023-02-25 19:33:12 +00:00
}),
],
},
2023-02-25 22:18:31 +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);
});