deno.land / x / docx@8.5.0 / demo / 11-declaritive-styles-2.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317// Setting styles with JavaScript configuration
import * as fs from "fs";import { AlignmentType, convertInchesToTwip, Document, Footer, HeadingLevel, ImageRun, Packer, Paragraph, Table, TableCell, TableRow, TabStopPosition, UnderlineType, LevelFormat,} from "docx";
const table = new Table({ rows: [ new TableRow({ children: [ new TableCell({ children: [new Paragraph("Test cell 1.")], }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph("Test cell 2.")], }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph("Test cell 3.")], }), ], }), new TableRow({ children: [ new TableCell({ children: [new Paragraph("Test cell 4.")], }), ], }), ],});
const doc = new Document({ numbering: { config: [ { reference: "ref1", levels: [ { level: 0, format: LevelFormat.DECIMAL, text: "%1)", start: 50, }, ], }, ], }, styles: { default: { heading1: { run: { font: "Calibri", size: 52, bold: true, color: "000000", underline: { type: UnderlineType.SINGLE, color: "000000", }, }, paragraph: { alignment: AlignmentType.CENTER, spacing: { line: 340 }, }, }, heading2: { run: { font: "Calibri", size: 26, bold: true, }, paragraph: { spacing: { line: 340 }, }, }, heading3: { run: { font: "Calibri", size: 26, bold: true, }, paragraph: { spacing: { line: 276 }, }, }, heading4: { run: { font: "Calibri", size: 26, bold: true, }, paragraph: { alignment: AlignmentType.JUSTIFIED, }, }, }, paragraphStyles: [ { id: "normalPara", name: "Normal Para", basedOn: "Normal", next: "Normal", quickFormat: true, run: { font: "Calibri", size: 26, bold: true, }, paragraph: { spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, rightTabStop: TabStopPosition.MAX, leftTabStop: 453.543307087, }, }, { id: "normalPara2", name: "Normal Para2", basedOn: "Normal", next: "Normal", quickFormat: true, run: { font: "Calibri", size: 26, }, paragraph: { alignment: AlignmentType.JUSTIFIED, spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, }, }, { id: "aside", name: "Aside", basedOn: "Normal", next: "Normal", run: { color: "999999", italics: true, }, paragraph: { spacing: { line: 276 }, indent: { left: convertInchesToTwip(0.5) }, }, }, { id: "wellSpaced", name: "Well Spaced", basedOn: "Normal", paragraph: { spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, }, }, { id: "numberedPara", name: "Numbered Para", basedOn: "Normal", next: "Normal", quickFormat: true, run: { font: "Calibri", size: 26, bold: true, }, paragraph: { spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, rightTabStop: TabStopPosition.MAX, leftTabStop: 453.543307087, numbering: { reference: "ref1", instance: 0, level: 0, }, }, }, ], }, sections: [ { properties: { page: { margin: { top: 700, right: 700, bottom: 700, left: 700, }, }, }, footers: { default: new Footer({ children: [ new Paragraph({ text: "1", style: "normalPara", alignment: AlignmentType.RIGHT, }), ], }), }, children: [ new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/pizza.gif"), transformation: { width: 100, height: 100, }, }), ], }), new Paragraph({ text: "HEADING", heading: HeadingLevel.HEADING_1, alignment: AlignmentType.CENTER, }), new Paragraph({ text: "Ref. :", style: "normalPara", }), new Paragraph({ text: "Date :", style: "normalPara", }), new Paragraph({ text: "To,", style: "normalPara", }), new Paragraph({ text: "The Superindenting Engineer,(O &M)", style: "normalPara", }), new Paragraph({ text: "Sub : ", style: "normalPara", }), new Paragraph({ text: "Ref. : ", style: "normalPara", }), new Paragraph({ text: "Sir,", style: "normalPara", }), new Paragraph({ text: "BRIEF DESCRIPTION", style: "normalPara", }), table, new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/pizza.gif"), transformation: { width: 100, height: 100, }, }), ], }), new Paragraph({ text: "Test", style: "normalPara2", }), new Paragraph({ children: [ new ImageRun({ data: fs.readFileSync("./demo/images/pizza.gif"), transformation: { width: 100, height: 100, }, }), ], }), new Paragraph({ text: "Test 2", style: "normalPara2", }), new Paragraph({ text: "Numbered paragraph that has numbering attached to custom styles", style: "numberedPara", }), new Paragraph({ text: "Numbered para would show up in the styles pane at Word", style: "numberedPara", }), ], }, ],});
Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer);});
Version Info