2019-02-26 22:20:20 +00:00
|
|
|
// Custom styles using JavaScript configuration
|
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
2018-08-16 16:28:10 -04:00
|
|
|
import * as fs from "fs";
|
2019-06-13 01:07:00 +01:00
|
|
|
import { Document, HeadingLevel, Packer, Paragraph } from "../build";
|
2018-08-16 16:28:10 -04:00
|
|
|
|
|
|
|
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")
|
2018-08-16 16:28:10 -04:00
|
|
|
.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
|
2018-08-16 16:28:10 -04:00
|
|
|
|
2019-07-31 08:48:02 +01:00
|
|
|
doc.Styles.createParagraphStyle("Heading2", "Heading 2")
|
2018-08-16 16:28:10 -04:00
|
|
|
.basedOn("Normal")
|
|
|
|
.next("Normal")
|
|
|
|
.quickFormat()
|
2019-06-13 01:07:00 +01:00
|
|
|
.size(26) // 26 half-points === 13pt font
|
2018-08-16 16:28:10 -04:00
|
|
|
.bold()
|
|
|
|
.underline("double", "FF0000")
|
2019-06-13 01:07:00 +01:00
|
|
|
.spacing({ before: 240, after: 120 }); // TWIP for both
|
2018-08-16 16:28:10 -04:00
|
|
|
|
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,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2018-08-16 16:28:10 -04:00
|
|
|
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2018-08-16 16:28:10 -04:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|