From 2cf1cce06d44fb185ebcfc9af3410577e907baec Mon Sep 17 00:00:00 2001 From: ramonmata Date: Thu, 24 Oct 2019 13:46:39 -0500 Subject: [PATCH] Adds demo to show XML StyleId applied to a table --- demo/48-table-xml-styles.ts | 50 +++++++++++++++++++++++++++++++++++ demo/assets/custom-styles.xml | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 demo/48-table-xml-styles.ts diff --git a/demo/48-table-xml-styles.ts b/demo/48-table-xml-styles.ts new file mode 100644 index 0000000000..e993c5be34 --- /dev/null +++ b/demo/48-table-xml-styles.ts @@ -0,0 +1,50 @@ +// Example of how you would create a table and add data to it +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "../build"; + +const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8"); +const doc = new Document({ + title: "Title", + externalStyles: styles +}); + + +// Create a table and pass the XML Style +const table = new Table({ + style: 'MyCustomTableStyle', + width: { + size: 9070, + type: WidthType.DXA + }, + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Header Colum 1")], + }), + new TableCell({ + children: [new Paragraph("Header Colum 2")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Column Content 3")], + }), + new TableCell({ + children: [new Paragraph("Column Content 2")], + }), + ], + }), + ], +}); + +doc.addSection({ + children: [table], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/assets/custom-styles.xml b/demo/assets/custom-styles.xml index 76159f2985..33becc2d37 100644 --- a/demo/assets/custom-styles.xml +++ b/demo/assets/custom-styles.xml @@ -1,2 +1,2 @@ - \ No newline at end of file +