Files
docx-js/demo/demo2.ts

105 lines
3.0 KiB
TypeScript
Raw Normal View History

2018-08-21 02:46:21 +01:00
// Example on how to customise the look at feel using Styles
// 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, Packer, Paragraph, TextRun, HeadingLevel } from "../build";
2018-08-21 02:46:21 +01:00
const doc = new Document({
creator: "Clippy",
title: "Sample Document",
description: "A brief example of using docx",
});
doc.Styles.createParagraphStyle("Heading1", "Heading 1")
.basedOn("Normal")
.next("Normal")
.quickFormat()
.size(28)
.bold()
.italics()
.spacing({ after: 120 });
doc.Styles.createParagraphStyle("Heading2", "Heading 2")
.basedOn("Normal")
.next("Normal")
.quickFormat()
.size(26)
.bold()
.underline("double", "FF0000")
.spacing({ before: 240, after: 120 });
doc.Styles.createParagraphStyle("aside", "Aside")
.basedOn("Normal")
.next("Normal")
.color("999999")
.italics()
.indent({ left: 720 })
.spacing({ line: 276 });
doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced")
.basedOn("Normal")
.spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 });
doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph")
.quickFormat()
.basedOn("Normal");
const numberedAbstract = doc.Numbering.createAbstractNumbering();
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");
2019-06-13 01:07:00 +01:00
doc.addParagraph(
new Paragraph({
text: "Test heading1, bold and italicized",
heading: HeadingLevel.HEADING_1,
}),
);
doc.addParagraph(new Paragraph("Some simple content"));
doc.addParagraph(
new Paragraph({
text: "Test heading2 with double red underline",
heading: HeadingLevel.HEADING_2,
}),
);
2018-08-21 02:46:21 +01:00
const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract);
const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract);
letterNumbering5.overrideLevel(0, 5);
2019-06-13 01:07:00 +01:00
doc.addParagraph(
new Paragraph({
text: "Option1",
numbering: {
num: letterNumbering,
level: 0,
},
}),
);
doc.addParagraph(new Paragraph("Option5 -- override 2 to 5").setNumbering(letterNumbering5, 0));
doc.addParagraph(new Paragraph("Option3").setNumbering(letterNumbering, 0));
2018-08-21 02:46:21 +01:00
2019-06-13 01:07:00 +01:00
doc.addParagraph(new Paragraph({}).addRun(new TextRun("Some monospaced content").font("Monospace")));
2018-08-21 02:46:21 +01:00
2019-06-13 01:07:00 +01:00
doc.addParagraph(
new Paragraph({
text: "An aside, in light gray italics and indented",
style: "aside",
}),
);
doc.addParagraph(
new Paragraph({
text: "This is normal, but well-spaced text",
style: "wellSpaced",
}),
);
const para = new Paragraph({});
doc.addParagraph(para);
para.addRun(new TextRun("This is a bold run,").bold());
para.addRun(new TextRun(" switching to normal "));
para.addRun(new TextRun("and then underlined ").underline());
para.addRun(new TextRun("and back to normal."));
2018-08-21 02:46:21 +01:00
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});