Files
docx-js/demo/51-character-styles.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-02-27 11:10:00 +00:00
// Custom character styles using JavaScript configuration
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document({
styles: {
characterStyles: [
{
id: "myRedStyle",
name: "My Wonky Style",
basedOn: "Normal",
run: {
color: "990000",
italics: true,
},
},
2020-12-22 20:56:06 +00:00
{
id: "strong",
name: "Strong",
basedOn: "Normal",
run: {
bold: true,
},
},
2020-02-27 11:10:00 +00:00
],
},
2021-03-19 20:53:56 +00:00
sections: [
{
2020-12-22 20:56:06 +00:00
children: [
2021-03-19 20:53:56 +00:00
new Paragraph({
children: [
new TextRun({
text: "Foo bar",
style: "myRedStyle",
}),
],
2020-12-22 20:56:06 +00:00
}),
2021-03-19 20:53:56 +00:00
new Paragraph({
children: [
new TextRun({
text: "First Word",
style: "strong",
}),
new TextRun({
2022-07-06 15:26:17 +01:00
text: " - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
2021-03-19 20:53:56 +00:00
}),
],
2020-12-22 20:56:06 +00:00
}),
],
2021-03-19 20:53:56 +00:00
},
2020-02-27 11:10:00 +00:00
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});