Files
docx-js/demo/2-declaritive-styles.ts

170 lines
4.7 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-11-08 03:11:19 +00:00
import { AlignmentType, Document, HeadingLevel, Packer, Paragraph, TextRun, UnderlineType } 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",
styles: {
default: {
heading1: {
run: {
size: 28,
bold: true,
italics: true,
color: "red",
2019-10-04 01:20:41 +01:00
},
paragraph: {
spacing: {
after: 120,
},
2019-10-04 01:20:41 +01:00
},
},
heading2: {
run: {
size: 26,
bold: true,
underline: {
type: UnderlineType.DOUBLE,
color: "FF0000",
},
},
paragraph: {
spacing: {
before: 240,
after: 120,
},
2019-10-04 01:20:41 +01:00
},
},
listParagraph: {
run: {
color: '#FF0000'
}
}
},
paragraphStyles: [
2019-10-04 01:20:41 +01:00
{
id: "aside",
name: "Aside",
basedOn: "Normal",
next: "Normal",
run: {
color: "999999",
italics: true,
},
paragraph: {
indent: {
left: 720,
},
spacing: {
line: 276,
},
},
},
{
id: "wellSpaced",
name: "Well Spaced",
basedOn: "Normal",
quickFormat: true,
paragraph: {
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
},
},
],
2019-10-04 02:37:22 +01:00
},
2019-11-08 03:11:19 +00:00
numbering: {
config: [
{
reference: "my-crazy-numbering",
levels: [
{
level: 0,
format: "lowerLetter",
text: "%1)",
alignment: AlignmentType.LEFT,
},
],
},
],
},
2018-08-21 02:46:21 +01:00
});
2019-07-31 08:48:02 +01:00
doc.addSection({
children: [
new Paragraph({
text: "Test heading1, bold and italicized",
heading: HeadingLevel.HEADING_1,
}),
new Paragraph("Some simple content"),
new Paragraph({
text: "Test heading2 with double red underline",
heading: HeadingLevel.HEADING_2,
}),
new Paragraph({
text: "Option1",
numbering: {
2019-11-08 03:11:19 +00:00
reference: "my-crazy-numbering",
2019-07-31 08:48:02 +01:00
level: 0,
2019-06-17 01:51:57 +01:00
},
}),
2019-07-31 08:48:02 +01:00
new Paragraph({
text: "Option5 -- override 2 to 5",
numbering: {
2019-11-08 03:11:19 +00:00
reference: "my-crazy-numbering",
2019-07-31 08:48:02 +01:00
level: 0,
},
}),
new Paragraph({
text: "Option3",
numbering: {
2019-11-08 03:11:19 +00:00
reference: "my-crazy-numbering",
2019-07-31 08:48:02 +01:00
level: 0,
},
}),
new Paragraph({
children: [
new TextRun({
text: "Some monospaced content",
font: {
name: "Monospace",
},
}),
],
}),
2019-07-31 08:48:02 +01:00
new Paragraph({
text: "An aside, in light gray italics and indented",
style: "aside",
}),
new Paragraph({
text: "This is normal, but well-spaced text",
style: "wellSpaced",
}),
new Paragraph({
children: [
new TextRun({
text: "This is a bold run,",
bold: true,
}),
new TextRun(" switching to normal "),
new TextRun({
text: "and then underlined ",
underline: {},
}),
2020-05-22 12:56:02 +08:00
new TextRun({
text: "and then emphasis-mark ",
emphasisMark: {},
}),
2019-07-31 08:48:02 +01:00
new TextRun({
text: "and back to normal.",
}),
],
}),
],
});
2018-08-21 02:46:21 +01:00
2019-08-07 22:12:14 +01:00
Packer.toBuffer(doc).then((buffer) => {
2018-08-21 02:46:21 +01:00
fs.writeFileSync("My Document.docx", buffer);
});