Files
docx-js/demo/27-declaritive-styles-3.ts

45 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();
// 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-07-31 08:48:02 +01:00
doc.Styles.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-07-31 08:48:02 +01:00
doc.Styles.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-07-31 08:48:02 +01:00
doc.addSection({
children: [
new Paragraph({
text: "Hello",
style: "myWonkyStyle",
}),
new Paragraph({
text: "World",
heading: HeadingLevel.HEADING_2,
}),
],
});
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});