Files
docx-js/demo/75-tab-stops.ts

81 lines
2.8 KiB
TypeScript
Raw Normal View History

// Example of using tab stops
2022-10-17 14:49:32 +05:45
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2022-12-24 19:49:13 +00:00
import { Document, HeadingLevel, Packer, Paragraph, TabStopPosition, TabStopType, TextRun } from "../build";
2022-10-17 14:49:32 +05:45
const columnWidth = TabStopPosition.MAX / 4;
2022-10-19 19:58:46 +01:00
const receiptTabStops = [
// 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 }];
2022-10-17 14:49:32 +05:45
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
heading: HeadingLevel.HEADING_1,
2022-10-19 19:58:46 +01:00
children: [new TextRun("Receipt 001")],
2022-10-17 14:49:32 +05:45
}),
new Paragraph({
tabStops: twoTabStops,
2022-10-19 19:58:46 +01:00
children: [
new TextRun({
2022-12-24 19:49:13 +00:00
text: "To Bob.\tBy Alice.",
2022-10-19 19:58:46 +01:00
bold: true,
}),
2022-10-17 14:49:32 +05:45
],
}),
new Paragraph({
tabStops: twoTabStops,
2022-10-19 19:58:46 +01:00
children: [new TextRun("Foo Inc\tBar Inc")],
2022-10-17 14:49:32 +05:45
}),
2022-10-19 19:58:46 +01:00
new Paragraph({ text: "" }),
2022-10-17 14:49:32 +05:45
new Paragraph({
2022-10-19 19:58:46 +01:00
tabStops: receiptTabStops,
2022-10-17 14:49:32 +05:45
children: [
new TextRun({
text: "Item\tPrice\tQuantity\tSub-total",
2022-10-19 19:58:46 +01:00
bold: true,
}),
2022-10-17 14:49:32 +05:45
],
}),
new Paragraph({
2022-10-19 19:58:46 +01:00
tabStops: receiptTabStops,
2022-10-17 14:49:32 +05:45
text: "Item 3\t10\t5\t50",
}),
new Paragraph({
2022-10-19 19:58:46 +01:00
tabStops: receiptTabStops,
2022-10-17 14:49:32 +05:45
text: "Item 3\t10\t5\t50",
}),
new Paragraph({
2022-10-19 19:58:46 +01:00
tabStops: receiptTabStops,
text: "Item 3\t10\t5\t50",
2022-10-17 14:49:32 +05:45
}),
new Paragraph({
2022-10-19 19:58:46 +01:00
tabStops: receiptTabStops,
2022-10-17 14:49:32 +05:45
children: [
new TextRun({
2022-10-17 14:56:12 +05:45
text: "\t\t\tTotal: 200",
2022-10-19 19:58:46 +01:00
bold: true,
}),
2022-10-17 14:49:32 +05:45
],
2022-10-19 19:58:46 +01:00
}),
2022-10-17 14:49:32 +05:45
],
},
],
});
const stream = Packer.toStream(doc);
stream.pipe(fs.createWriteStream("My Document.docx"));