Files
docx-js/demo/demo27.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-02-26 22:20:20 +00:00
// Custom styles using JavaScript configuration
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
2019-06-13 01:07:00 +01:00
import { Document, HeadingLevel, Packer, Paragraph } from "../build";
const doc = new Document();
const myStyles = doc.Styles;
// The first argument is an ID you use to apply the style to paragraphs
// The second argument is a human-friendly name to show in the UI
2019-06-13 01:07:00 +01:00
myStyles
.createParagraphStyle("myWonkyStyle", "My Wonky Style")
.basedOn("Normal")
.next("Normal")
.color("990000")
.italics()
2019-06-13 01:07:00 +01:00
.indent({ left: 720 }) // 720 TWIP === 720 / 20 pt === .5 in
.spacing({ line: 276 }); // 276 / 240 = 1.15x line spacing
2019-06-13 01:07:00 +01:00
myStyles
.createParagraphStyle("Heading2", "Heading 2")
.basedOn("Normal")
.next("Normal")
.quickFormat()
2019-06-13 01:07:00 +01:00
.size(26) // 26 half-points === 13pt font
.bold()
.underline("double", "FF0000")
2019-06-13 01:07:00 +01:00
.spacing({ before: 240, after: 120 }); // TWIP for both
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "Hello",
style: "myWonkyStyle",
}),
);
2019-06-25 23:17:56 +01:00
doc.add(
2019-06-13 01:07:00 +01:00
new Paragraph({
text: "World",
heading: HeadingLevel.HEADING_2,
}),
); // Uses the Heading2 style
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});