diff --git a/demo/11-declaritive-styles-2.ts b/demo/11-declaritive-styles-2.ts index c6780c440f..3a0154a0fb 100644 --- a/demo/11-declaritive-styles-2.ts +++ b/demo/11-declaritive-styles-2.ts @@ -1,82 +1,153 @@ // Setting styles with JavaScript configuration // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { AlignmentType, Document, Footer, HeadingLevel, Media, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; +import { + AlignmentType, + Document, + Footer, + HeadingLevel, + Media, + Packer, + Paragraph, + Table, + TableCell, + TableRow, + TabStopPosition, + UnderlineType, +} from "../build"; -const doc = new Document(); - -doc.Styles.createParagraphStyle("Heading1", "Heading 1") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .font("Calibri") - .size(52) - .center() - .bold() - .color("000000") - .spacing({ line: 340 }) - .underline("single", "000000"); - -doc.Styles.createParagraphStyle("Heading2", "Heading 2") - .basedOn("Normal") - .next("Normal") - .font("Calibri") - .quickFormat() - .size(26) - .bold() - .spacing({ line: 340 }); - -doc.Styles.createParagraphStyle("Heading3", "Heading 3") - .basedOn("Normal") - .next("Normal") - .font("Calibri") - .quickFormat() - .size(26) - .bold() - .spacing({ line: 276 }); - -doc.Styles.createParagraphStyle("Heading4", "Heading 4") - .basedOn("Normal") - .next("Normal") - .justified() - .font("Calibri") - .size(26) - .bold(); - -doc.Styles.createParagraphStyle("normalPara", "Normal Para") - .basedOn("Normal") - .next("Normal") - .font("Calibri") - .quickFormat() - .leftTabStop(453.543307087) - .maxRightTabStop() - .size(26) - .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); - -doc.Styles.createParagraphStyle("normalPara2", "Normal Para2") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .font("Calibri") - .size(26) - .justified() - .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); - -doc.Styles.createParagraphStyle("aside", "Aside") - .basedOn("Normal") - .next("Normal") - .color("999999") - .italics() - .indent({ left: 720 }) - .spacing({ line: 276 }); - -doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") - .basedOn("Normal") - .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); - -doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph") - .quickFormat() - .basedOn("Normal"); +const doc = new Document({ + styles: { + paragraphStyles: [ + { + id: "Heading1", + name: "Heading 1", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 52, + bold: true, + color: "000000", + underline: { + type: UnderlineType.SINGLE, + color: "000000", + }, + }, + paragraph: { + alignment: AlignmentType.CENTER, + spacing: { line: 340 }, + }, + }, + { + id: "Heading2", + name: "Heading 2", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 340 }, + }, + }, + { + id: "Heading3", + name: "Heading 3", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 276 }, + }, + }, + { + id: "Heading4", + name: "Heading 4", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + alignment: AlignmentType.JUSTIFIED, + }, + }, + { + 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: 720 }, + }, + }, + { + id: "wellSpaced", + name: "Well Spaced", + basedOn: "Normal", + paragraph: { + spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, + }, + }, + { + id: "ListParagraph", + name: "List Paragraph", + basedOn: "Normal", + quickFormat: true, + }, + ], + }, +}); const image = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif")); diff --git a/demo/27-declaritive-styles-3.ts b/demo/27-declaritive-styles-3.ts index 31994dfe4a..8419467799 100644 --- a/demo/27-declaritive-styles-3.ts +++ b/demo/27-declaritive-styles-3.ts @@ -1,28 +1,53 @@ // Custom styles using JavaScript configuration // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, HeadingLevel, Packer, Paragraph } from "../build"; +import { Document, HeadingLevel, Packer, Paragraph, UnderlineType } from "../build"; -const doc = new Document(); - -// The first argument is an ID you use to apply the style to paragraphs -// The second argument is a human-friendly name to show in the UI -doc.Styles.createParagraphStyle("myWonkyStyle", "My Wonky Style") - .basedOn("Normal") - .next("Normal") - .color("990000") - .italics() - .indent({ left: 720 }) // 720 TWIP === 720 / 20 pt === .5 in - .spacing({ line: 276 }); // 276 / 240 = 1.15x line spacing - -doc.Styles.createParagraphStyle("Heading2", "Heading 2") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(26) // 26 half-points === 13pt font - .bold() - .underline("double", "FF0000") - .spacing({ before: 240, after: 120 }); // TWIP for both +const doc = new Document({ + styles: { + paragraphStyles: [ + { + id: "myWonkyStyle", + name: "My Wonky Style", + basedOn: "Normal", + next: "Normal", + run: { + color: "990000", + italics: true, + }, + paragraph: { + indent: { + left: 720, + }, + spacing: { + line: 276, + }, + }, + }, + { + id: "Heading2", + name: "Heading 2", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + bold: true, + size: 26, + underline: { + type: UnderlineType.DOUBLE, + color: "FF0000", + }, + }, + paragraph: { + spacing: { + before: 240, + after: 120, + }, + }, + }, + ], + }, +}); doc.addSection({ children: [ diff --git a/demo/28-table-of-contents.ts b/demo/28-table-of-contents.ts index 83160b0177..76789ee256 100644 --- a/demo/28-table-of-contents.ts +++ b/demo/28-table-of-contents.ts @@ -3,15 +3,23 @@ import * as fs from "fs"; import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build"; -const doc = new File(); - -// The first argument is an ID you use to apply the style to paragraphs -// The second argument is a human-friendly name to show in the UI -doc.Styles.createParagraphStyle("MySpectacularStyle", "My Spectacular Style") - .basedOn("Heading1") - .next("Heading1") - .color("990000") - .italics(); +const doc = new File({ + styles: { + paragraphStyles: [ + { + id: "MySpectacularStyle", + name: "My Spectacular Style", + basedOn: "Heading1", + next: "Heading1", + quickFormat: true, + run: { + italics: true, + color: "990000", + }, + }, + ], + }, +}); // WordprocessingML docs for TableOfContents can be found here: // http://officeopenxml.com/WPtableOfContents.php diff --git a/demo/47-number-of-total-pages-section.ts b/demo/47-number-of-total-pages-section.ts index 7ccc44e22a..988206e524 100644 --- a/demo/47-number-of-total-pages-section.ts +++ b/demo/47-number-of-total-pages-section.ts @@ -1,18 +1,26 @@ // Multiple sections with total number of pages in each section // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, PageNumberFormat, TextRun } from "../build"; +import { AlignmentType, Document, Packer, PageNumberFormat, TextRun, Header, Paragraph, Footer, PageBreak } from "../build"; const doc = new Document(); +const header = new Header({ + children: [ + new Paragraph({ + children: [ + new TextRun("Header on another page"), + new TextRun("Page Number: ").pageNumber(), + new TextRun(" to ").numberOfTotalPagesSection(), + ], + alignment: AlignmentType.CENTER, + }), + ], +}); -const header = doc.createHeader(); -header.createParagraph("Header on another page"); -const footer = doc.createFooter(); -footer.createParagraph("Foo Bar corp. ") - .center() - .addRun(new TextRun("Page Number: ").pageNumber()) - .addRun(new TextRun(" to ").numberOfTotalPagesSection()); +const footer = new Footer({ + children: [new Paragraph("Foo Bar corp. ")], +}); doc.addSection({ headers: { @@ -21,13 +29,17 @@ doc.addSection({ footers: { default: footer, }, - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, + properties: { + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + }, + children: [ + new Paragraph({ + children: [new TextRun("Section 1"), new PageBreak(), new TextRun("Section 1"), new PageBreak()], + }), + ], }); -doc.createParagraph("Section 1").pageBreak(); -doc.createParagraph("Section 1").pageBreak(); - doc.addSection({ headers: { default: header, @@ -35,15 +47,17 @@ doc.addSection({ footers: { default: footer, }, - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, + properties: { + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + }, + children: [ + new Paragraph({ + children: [new TextRun("Section 2"), new PageBreak(), new TextRun("Section 2"), new PageBreak()], + }), + ], }); -doc.createParagraph("Section 2").pageBreak(); -doc.createParagraph("Section 2").pageBreak(); - -const packer = new Packer(); - -packer.toBuffer(doc).then((buffer) => { +Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); });