From c75abb7cb93d1229e908ab4364112d38a8773cec Mon Sep 17 00:00:00 2001 From: Ronit Ramdam BK Date: Mon, 17 Oct 2022 14:49:32 +0545 Subject: [PATCH] DEMO: added demo/75-tab-stops.ts --- demo/75-tab-stops.ts | 86 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 demo/75-tab-stops.ts diff --git a/demo/75-tab-stops.ts b/demo/75-tab-stops.ts new file mode 100644 index 0000000000..7603e4c2e3 --- /dev/null +++ b/demo/75-tab-stops.ts @@ -0,0 +1,86 @@ +// Exporting the document as a stream +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, HeadingLevel, Packer, Paragraph, TabStopPosition, TabStopType, TextRun } from "../build"; + +const columnWidth = TabStopPosition.MAX / 4; +const reciptTabStops = [ + // no need to define first left tab column + // the right aligned tab column position should point to the end of column + // i.e. in this case + // (end position of 1st) + (end position of current) + // columnWidth + columnWidth = columnWidth * 2 + + { type: TabStopType.RIGHT, position: columnWidth * 2 }, + { type: TabStopType.RIGHT, position: columnWidth * 3 }, + { type: TabStopType.RIGHT, position: TabStopPosition.MAX } +], twoTabStops = [ + { type: TabStopType.RIGHT, position: TabStopPosition.MAX } +]; + +const doc = new Document({ + sections: [ + { + properties: {}, + children: [ + new Paragraph({ + heading: HeadingLevel.HEADING_1, + children: [ new TextRun("Recipt 001")], + }), + new Paragraph({ + tabStops: twoTabStops, + children: [ + new TextRun({ + text: "To Bob.\tBy Alice.", + bold: true + }) + ], + + }), + new Paragraph({ + tabStops: twoTabStops, + children: [ + new TextRun("Foo Inc\tBar Inc") + ], + }), + new Paragraph({text: ""}), + new Paragraph({ + tabStops: reciptTabStops, + + children: [ + new TextRun({ + text: "Item\tPrice\tQuantity\tSub-total", + bold: true + }) + ], + }), + new Paragraph({ + tabStops: reciptTabStops, + text: "Item 3\t10\t5\t50", + }), + new Paragraph({ + tabStops: reciptTabStops, + text: "Item 3\t10\t5\t50", + }), + new Paragraph({ + tabStops: reciptTabStops, + text: "Item 3\t10\t5\t50" + }), + new Paragraph({ + tabStops: reciptTabStops, + children: [ + new TextRun({ + text: "\t\tTotal: 200", + bold: true + }) + ], + }) + ], + }, + ], +}); + + +const stream = Packer.toStream(doc); +stream.pipe(fs.createWriteStream("My Document.docx")); +