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 +