deno.land / x / docx@8.5.0 / demo / 2-declaritive-styles.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269// Example on how to customize the look at feel using Styles
import * as fs from "fs";import { AlignmentType, convertInchesToTwip, Document, HeadingLevel, LevelFormat, Packer, Paragraph, TextRun, UnderlineType } from "docx";
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", }, paragraph: { spacing: { after: 120, }, }, }, heading2: { run: { size: 26, bold: true, underline: { type: UnderlineType.DOUBLE, color: "FF0000", }, }, paragraph: { spacing: { before: 240, after: 120, }, }, }, listParagraph: { run: { color: "#FF0000", }, }, document: { run: { size: "11pt", font: "Calibri", }, paragraph: { alignment: AlignmentType.RIGHT, }, }, }, paragraphStyles: [ { id: "aside", name: "Aside", basedOn: "Normal", next: "Normal", run: { color: "999999", italics: true, }, paragraph: { indent: { left: convertInchesToTwip(0.5), }, 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 }, }, }, { id: "strikeUnderline", name: "Strike Underline", basedOn: "Normal", quickFormat: true, run: { strike: true, underline: { type: UnderlineType.SINGLE, }, }, }, ], characterStyles: [ { id: "strikeUnderlineCharacter", name: "Strike Underline", basedOn: "Normal", quickFormat: true, run: { strike: true, underline: { type: UnderlineType.SINGLE, }, }, }, ], }, numbering: { config: [ { reference: "my-crazy-numbering", levels: [ { level: 0, format: LevelFormat.LOWER_LETTER, text: "%1)", alignment: AlignmentType.LEFT, }, ], }, ], }, sections: [ { 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: { reference: "my-crazy-numbering", level: 0, }, style: "aside", }), new Paragraph({ text: "Option5 -- override 2 to 5", numbering: { reference: "my-crazy-numbering", level: 0, }, }), new Paragraph({ text: "Option3", numbering: { reference: "my-crazy-numbering", level: 0, }, }), new Paragraph({ children: [ new TextRun({ text: "Some monospaced content", font: { name: "Monospace", }, }), ], }), 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: {}, }), new TextRun({ text: "and then emphasis-mark ", emphasisMark: {}, }), new TextRun({ text: "and back to normal.", }), 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, }), ], }), new Paragraph({ style: "Strong", children: [ new TextRun({ text: "Strong Style", }), new TextRun({ text: " - Very strong.", }), ], }), new Paragraph({ 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", }), new TextRun({ scale: 50, text: " Scaled text", }), ], }), new Paragraph({ scale: 200, children: [ new TextRun({ text: "Scaled paragraph", }), ], }), ], }, ],});
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer);});
Version Info