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

156 lines
6.1 KiB
TypeScript
Raw Permalink Normal View History

2023-02-25 22:18:31 +00:00
// Patch a document with patches
2023-06-05 00:33:43 +01:00
2023-02-08 03:18:11 +00:00
import * as fs from "fs";
2023-02-25 19:33:12 +00:00
import {
2023-03-05 18:46:02 +00:00
ExternalHyperlink,
2023-02-25 19:33:12 +00:00
HeadingLevel,
2023-03-03 23:47:50 +00:00
ImageRun,
2023-02-25 19:33:12 +00:00
Paragraph,
patchDocument,
PatchType,
Table,
TableCell,
TableRow,
TextDirection,
TextRun,
VerticalAlign,
2023-06-05 00:33:43 +01:00
} from "docx";
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: {
2023-02-25 22:18:56 +00:00
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:56 +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:56 +00:00
item_1: {
2023-02-25 19:33:12 +00:00
type: PatchType.PARAGRAPH,
2023-03-08 23:30:51 +00:00
children: [
new TextRun("#657"),
new ExternalHyperlink({
children: [
new TextRun({
text: "BBC News Link",
}),
],
link: "https://www.bbc.co.uk/news",
}),
],
2023-02-24 01:05:43 +00:00
},
2023-02-25 22:18:56 +00:00
paragraph_replace: {
2023-02-25 19:33:12 +00:00
type: PatchType.DOCUMENT,
2023-03-13 21:35:16 +00:00
children: [
new Paragraph("Lorem ipsum paragraph"),
new Paragraph("Another paragraph"),
new Paragraph({
children: [
new TextRun("This is a "),
new ExternalHyperlink({
children: [
new TextRun({
text: "Google Link",
}),
],
link: "https://www.google.co.uk",
}),
2023-03-16 01:55:18 +00:00
new ImageRun({ data: fs.readFileSync("./demo/images/dog.png"), transformation: { width: 100, height: 100 } }),
2023-03-13 21:35:16 +00:00
],
}),
],
2023-02-24 01:05:43 +00:00
},
2023-02-25 22:18:56 +00:00
header_adjective: {
type: PatchType.PARAGRAPH,
children: [new TextRun("Delightful Header")],
},
2023-02-25 22:18:56 +00:00
footer_text: {
type: PatchType.PARAGRAPH,
2023-03-05 18:46:02 +00:00
children: [
new TextRun("replaced just as"),
new TextRun(" well"),
new ExternalHyperlink({
children: [
new TextRun({
text: "BBC News Link",
}),
],
link: "https://www.bbc.co.uk/news",
}),
],
},
2023-03-03 23:47:50 +00:00
image_test: {
type: PatchType.PARAGRAPH,
children: [new ImageRun({ data: fs.readFileSync("./demo/images/image1.jpeg"), transformation: { width: 100, height: 100 } })],
},
2023-02-25 22:18:56 +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-16 20:17:48 +00:00
}).then((doc) => {
2023-02-08 03:18:11 +00:00
fs.writeFileSync("My Document.docx", doc);
});