#940 - Add positional tab feature

Also add extra run elements as per spec
This commit is contained in:
Dolan Miu
2022-12-24 19:32:44 +00:00
parent 49b4ca67e0
commit 11bebd42ac
15 changed files with 891 additions and 164 deletions

View File

@ -1,7 +1,7 @@
// Exporting the document as a stream
// Example of using tab stops
// 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";
import { Document, HeadingLevel, Packer, Paragraph, TabStopPosition, TabStopType, TextRun, Tab } from "../build";
const columnWidth = TabStopPosition.MAX / 4;
const receiptTabStops = [
@ -30,7 +30,7 @@ const doc = new Document({
tabStops: twoTabStops,
children: [
new TextRun({
text: "To Bob.\tBy Alice.",
children: ["To Bob.", new Tab(), "By Alice."],
bold: true,
}),
],

View File

@ -0,0 +1,60 @@
// Simple example apply positional tabs to a document
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import {
Document,
Packer,
Paragraph,
PositionalTab,
Tab,
TextRun,
PositionalTabAlignment,
PositionalTabRelativeTo,
PositionalTabLeader,
} from "../build";
const doc = new Document({
sections: [
{
properties: {},
children: [
new Paragraph({
children: [
new TextRun("Full name"),
new TextRun({
children: [
new PositionalTab({
alignment: PositionalTabAlignment.RIGHT,
relativeTo: PositionalTabRelativeTo.MARGIN,
leader: PositionalTabLeader.DOT,
}),
"John Doe",
],
bold: true,
}),
],
}),
new Paragraph({
children: [
new TextRun("Hello World"),
new TextRun({
children: [
new PositionalTab({
alignment: PositionalTabAlignment.CENTER,
relativeTo: PositionalTabRelativeTo.INDENT,
leader: PositionalTabLeader.HYPHEN,
}),
"Foo bar",
],
bold: true,
}),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});