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

269 lines
8.6 KiB
TypeScript
Raw Permalink Normal View History

2022-07-06 14:05:37 +01:00
// Example on how to customize the look at feel using Styles
2023-06-05 00:33:43 +01:00
2018-08-21 02:46:21 +01:00
import * as fs from "fs";
2023-06-01 02:05:35 +01:00
import { AlignmentType, convertInchesToTwip, Document, HeadingLevel, LevelFormat, Packer, Paragraph, TextRun, UnderlineType } from "docx";
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: "FF0000",
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: {
2020-12-22 21:08:10 +00:00
color: "#FF0000",
},
},
2023-06-28 22:02:09 +01:00
document: {
run: {
size: "11pt",
font: "Calibri",
},
paragraph: {
alignment: AlignmentType.RIGHT,
},
},
},
paragraphStyles: [
2019-10-04 01:20:41 +01:00
{
id: "aside",
name: "Aside",
basedOn: "Normal",
next: "Normal",
run: {
color: "999999",
italics: true,
},
paragraph: {
indent: {
2020-12-24 03:37:43 +00:00
left: convertInchesToTwip(0.5),
2019-10-04 01:20:41 +01:00
},
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 },
},
},
2022-10-25 21:03:59 +01:00
{
id: "strikeUnderline",
name: "Strike Underline",
basedOn: "Normal",
quickFormat: true,
run: {
strike: true,
underline: {
type: UnderlineType.SINGLE,
},
},
},
2019-10-04 01:20:41 +01:00
],
characterStyles: [
{
id: "strikeUnderlineCharacter",
name: "Strike Underline",
basedOn: "Normal",
quickFormat: true,
run: {
strike: true,
underline: {
type: UnderlineType.SINGLE,
},
},
},
],
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: LevelFormat.LOWER_LETTER,
2019-11-08 03:11:19 +00:00
text: "%1)",
alignment: AlignmentType.LEFT,
},
],
},
],
},
2021-03-19 20:53:56 +00:00
sections: [
{
children: [
2021-03-19 20:53:56 +00:00
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: {
reference: "my-crazy-numbering",
level: 0,
},
2021-03-19 20:53:56 +00:00
style: "aside",
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
text: "Option5 -- override 2 to 5",
numbering: {
reference: "my-crazy-numbering",
level: 0,
},
2019-07-31 08:48:02 +01:00
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
text: "Option3",
numbering: {
reference: "my-crazy-numbering",
level: 0,
},
2019-07-31 08:48:02 +01:00
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
children: [
new TextRun({
text: "Some monospaced content",
font: {
name: "Monospace",
},
}),
],
2020-05-22 12:56:02 +08:00
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
text: "An aside, in light gray italics and indented",
style: "aside",
2019-07-31 08:48:02 +01:00
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
text: "This is normal, but well-spaced text",
style: "wellSpaced",
2020-12-22 21:08:10 +00:00
}),
2021-03-19 20:53:56 +00:00
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: {},
}),
new TextRun({
text: "and then emphasis-mark ",
emphasisMark: {},
}),
new TextRun({
text: "and back to normal.",
}),
2022-10-28 22:20:16 +01:00
new TextRun({
text: "This text will be invisible!",
vanish: true,
}),
new TextRun({
text: "This text will be VERY invisible! Word processors cannot override this!",
specVanish: true,
}),
2021-03-19 20:53:56 +00:00
],
}),
new Paragraph({
style: "Strong",
children: [
new TextRun({
text: "Strong Style",
}),
new TextRun({
text: " - Very strong.",
}),
],
2020-12-22 21:08:10 +00:00
}),
new Paragraph({
2022-10-25 21:03:59 +01:00
style: "strikeUnderline",
children: [
new TextRun({
text: "Underline and Strike",
}),
new TextRun({
text: " Override Underline ",
underline: {
type: UnderlineType.NONE,
},
}),
new TextRun({
text: "Strike and Underline",
}),
],
}),
new Paragraph({
children: [
new TextRun({
text: "Hello World ",
}),
new TextRun({
style: "strikeUnderlineCharacter",
text: "Underline and Strike",
}),
new TextRun({
text: " Another Hello World",
}),
2022-11-03 00:30:16 +00:00
new TextRun({
scale: 50,
text: " Scaled text",
}),
],
}),
new Paragraph({
scale: 200,
children: [
new TextRun({
text: "Scaled paragraph",
}),
],
}),
2020-12-22 21:08:10 +00:00
],
2021-03-19 20:53:56 +00:00
},
2019-07-31 08:48:02 +01:00
],
});
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);
});