diff --git a/.nycrc b/.nycrc index 206058943b..5ac94bf6f4 100644 --- a/.nycrc +++ b/.nycrc @@ -1,9 +1,9 @@ { "check-coverage": true, - "lines": 98.79, - "functions": 97.54, - "branches": 95.64, - "statements": 98.8, + "lines": 98.86, + "functions": 97.86, + "branches": 95.86, + "statements": 98.86, "include": [ "src/**/*.ts" ], diff --git a/demo/1-basic.ts b/demo/1-basic.ts index eb417f52b4..da44119bbf 100644 --- a/demo/1-basic.ts +++ b/demo/1-basic.ts @@ -3,24 +3,26 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: {}, - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + properties: {}, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, - }), - new TextRun({ - text: "\tGithub is the best", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + new TextRun({ + text: "\tGithub is the best", + bold: true, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/10-my-cv.ts b/demo/10-my-cv.ts index 484a9c249c..293fcff6ad 100644 --- a/demo/10-my-cv.ts +++ b/demo/10-my-cv.ts @@ -129,70 +129,76 @@ const achievements = [ class DocumentCreator { // tslint:disable-next-line: typedef public create([experiences, educations, skills, achivements]): Document { - const document = new Document(); + const document = new Document({ + sections: [ + { + children: [ + new Paragraph({ + text: "Dolan Miu", + heading: HeadingLevel.TITLE, + }), + this.createContactInfo(PHONE_NUMBER, PROFILE_URL, EMAIL), + this.createHeading("Education"), + ...educations + .map((education) => { + const arr: Paragraph[] = []; + arr.push( + this.createInstitutionHeader( + education.schoolName, + `${education.startDate.year} - ${education.endDate.year}`, + ), + ); + arr.push(this.createRoleText(`${education.fieldOfStudy} - ${education.degree}`)); - document.addSection({ - children: [ - new Paragraph({ - text: "Dolan Miu", - heading: HeadingLevel.TITLE, - }), - this.createContactInfo(PHONE_NUMBER, PROFILE_URL, EMAIL), - this.createHeading("Education"), - ...educations - .map((education) => { - const arr: Paragraph[] = []; - arr.push( - this.createInstitutionHeader(education.schoolName, `${education.startDate.year} - ${education.endDate.year}`), - ); - arr.push(this.createRoleText(`${education.fieldOfStudy} - ${education.degree}`)); + const bulletPoints = this.splitParagraphIntoBullets(education.notes); + bulletPoints.forEach((bulletPoint) => { + arr.push(this.createBullet(bulletPoint)); + }); - const bulletPoints = this.splitParagraphIntoBullets(education.notes); - bulletPoints.forEach((bulletPoint) => { - arr.push(this.createBullet(bulletPoint)); - }); + return arr; + }) + .reduce((prev, curr) => prev.concat(curr), []), + this.createHeading("Experience"), + ...experiences + .map((position) => { + const arr: Paragraph[] = []; - return arr; - }) - .reduce((prev, curr) => prev.concat(curr), []), - this.createHeading("Experience"), - ...experiences - .map((position) => { - const arr: Paragraph[] = []; + arr.push( + this.createInstitutionHeader( + position.company.name, + this.createPositionDateText(position.startDate, position.endDate, position.isCurrent), + ), + ); + arr.push(this.createRoleText(position.title)); - arr.push( - this.createInstitutionHeader( - position.company.name, - this.createPositionDateText(position.startDate, position.endDate, position.isCurrent), - ), - ); - arr.push(this.createRoleText(position.title)); + const bulletPoints = this.splitParagraphIntoBullets(position.summary); - const bulletPoints = this.splitParagraphIntoBullets(position.summary); + bulletPoints.forEach((bulletPoint) => { + arr.push(this.createBullet(bulletPoint)); + }); - bulletPoints.forEach((bulletPoint) => { - arr.push(this.createBullet(bulletPoint)); - }); - - return arr; - }) - .reduce((prev, curr) => prev.concat(curr), []), - this.createHeading("Skills, Achievements and Interests"), - this.createSubHeading("Skills"), - this.createSkillList(skills), - this.createSubHeading("Achievements"), - ...this.createAchivementsList(achivements), - this.createSubHeading("Interests"), - this.createInterests("Programming, Technology, Music Production, Web Design, 3D Modelling, Dancing."), - this.createHeading("References"), - new Paragraph( - "Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk", - ), - new Paragraph("More references upon request"), - new Paragraph({ - text: "This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.", - alignment: AlignmentType.CENTER, - }), + return arr; + }) + .reduce((prev, curr) => prev.concat(curr), []), + this.createHeading("Skills, Achievements and Interests"), + this.createSubHeading("Skills"), + this.createSkillList(skills), + this.createSubHeading("Achievements"), + ...this.createAchivementsList(achivements), + this.createSubHeading("Interests"), + this.createInterests("Programming, Technology, Music Production, Web Design, 3D Modelling, Dancing."), + this.createHeading("References"), + new Paragraph( + "Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk", + ), + new Paragraph("More references upon request"), + new Paragraph({ + text: + "This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.", + alignment: AlignmentType.CENTER, + }), + ], + }, ], }); diff --git a/demo/11-declaritive-styles-2.ts b/demo/11-declaritive-styles-2.ts index ddb25a5445..11ed18fb69 100644 --- a/demo/11-declaritive-styles-2.ts +++ b/demo/11-declaritive-styles-2.ts @@ -17,6 +17,39 @@ import { UnderlineType, } from "../build"; +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({ styles: { default: { @@ -124,139 +157,107 @@ const doc = new Document({ }, ], }, -}); - -const table = new Table({ - rows: [ - new TableRow({ - children: [ - new TableCell({ - children: [new Paragraph("Test cell 1.")], + sections: [ + { + properties: { + top: 700, + right: 700, + bottom: 700, + left: 700, + }, + footers: { + default: new Footer({ + children: [ + new Paragraph({ + text: "1", + style: "normalPara", + alignment: AlignmentType.RIGHT, + }), + ], }), - ], - }), - 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.")], - }), - ], - }), - ], -}); - -doc.addSection({ - properties: { - top: 700, - right: 700, - bottom: 700, - left: 700, - }, - footers: { - default: new Footer({ + }, children: [ new Paragraph({ - text: "1", + 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", - alignment: AlignmentType.RIGHT, + }), + 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", }), ], - }), - }, - 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", - }), + }, ], }); diff --git a/demo/12-scaling-images.ts b/demo/12-scaling-images.ts index 4d70aa2fc1..bc628018b3 100644 --- a/demo/12-scaling-images.ts +++ b/demo/12-scaling-images.ts @@ -3,55 +3,57 @@ import * as fs from "fs"; import { Document, ImageRun, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph("Hello World"), - new Paragraph({ +const doc = new Document({ + sections: [ + { children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 50, - height: 50, - }, + new Paragraph("Hello World"), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 50, + height: 50, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 250, + height: 250, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 400, + height: 400, + }, + }), + ], }), ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 250, - height: 250, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 400, - height: 400, - }, - }), - ], - }), + }, ], }); diff --git a/demo/13-xml-styles.ts b/demo/13-xml-styles.ts index 1a4f60c24a..e555b963b5 100644 --- a/demo/13-xml-styles.ts +++ b/demo/13-xml-styles.ts @@ -7,23 +7,24 @@ const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8"); const doc = new Document({ title: "Title", externalStyles: styles, -}); - -doc.addSection({ - children: [ - new Paragraph({ - text: "Cool Heading Text", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph({ - text: 'This is a custom named style from the template "MyFancyStyle"', - style: "MyFancyStyle", - }), - new Paragraph("Some normal text"), - new Paragraph({ - text: "MyFancyStyle again", - style: "MyFancyStyle", - }), + sections: [ + { + children: [ + new Paragraph({ + text: "Cool Heading Text", + heading: HeadingLevel.HEADING_1, + }), + new Paragraph({ + text: 'This is a custom named style from the template "MyFancyStyle"', + style: "MyFancyStyle", + }), + new Paragraph("Some normal text"), + new Paragraph({ + text: "MyFancyStyle again", + style: "MyFancyStyle", + }), + ], + }, ], }); diff --git a/demo/14-page-numbers.ts b/demo/14-page-numbers.ts index 2e694ce2e9..9d40b30700 100644 --- a/demo/14-page-numbers.ts +++ b/demo/14-page-numbers.ts @@ -3,73 +3,75 @@ import * as fs from "fs"; import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: { - titlePage: true, - }, - headers: { - default: new Header({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, +const doc = new Document({ + sections: [ + { + properties: { + titlePage: true, + }, + headers: { + default: new Header({ children: [ - new TextRun("My Title "), - new TextRun({ - children: ["Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("My Title "), + new TextRun({ + children: ["Page ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - first: new Header({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, + first: new Header({ children: [ - new TextRun("First Page Header "), - new TextRun({ - children: ["Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("First Page Header "), + new TextRun({ + children: ["Page ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - }, - footers: { - default: new Footer({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, + }, + footers: { + default: new Footer({ children: [ - new TextRun("My Title "), - new TextRun({ - children: ["Footer - Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("My Title "), + new TextRun({ + children: ["Footer - Page ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - first: new Footer({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, + first: new Footer({ children: [ - new TextRun("First Page Footer "), - new TextRun({ - children: ["Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("First Page Footer "), + new TextRun({ + children: ["Page ", PageNumber.CURRENT], + }), + ], }), ], }), + }, + children: [ + new Paragraph({ + children: [new TextRun("First Page"), new PageBreak()], + }), + new Paragraph("Second Page"), ], - }), - }, - children: [ - new Paragraph({ - children: [new TextRun("First Page"), new PageBreak()], - }), - new Paragraph("Second Page"), + }, ], }); diff --git a/demo/15-page-break-before.ts b/demo/15-page-break-before.ts index bfb56fd7dd..a25f4b0708 100644 --- a/demo/15-page-break-before.ts +++ b/demo/15-page-break-before.ts @@ -3,15 +3,17 @@ import * as fs from "fs"; import { Document, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph("Hello World"), - new Paragraph({ - text: "Hello World on another page", - pageBreakBefore: true, - }), +const doc = new Document({ + sections: [ + { + children: [ + new Paragraph("Hello World"), + new Paragraph({ + text: "Hello World on another page", + pageBreakBefore: true, + }), + ], + }, ], }); diff --git a/demo/16-multiple-sections.ts b/demo/16-multiple-sections.ts index 6f2e350b23..a131938862 100644 --- a/demo/16-multiple-sections.ts +++ b/demo/16-multiple-sections.ts @@ -3,119 +3,118 @@ import * as fs from "fs"; import { Document, Footer, Header, Packer, PageNumber, PageNumberFormat, PageOrientation, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [new Paragraph("Hello World")], -}); - -doc.addSection({ - headers: { - default: new Header({ - children: [new Paragraph("First Default Header on another page")], - }), - }, - footers: { - default: new Footer({ - children: [new Paragraph("Footer on another page")], - }), - }, - properties: { - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, - }, - children: [new Paragraph("hello")], -}); - -doc.addSection({ - headers: { - default: new Header({ - children: [new Paragraph("Second Default Header on another page")], - }), - }, - footers: { - default: new Footer({ - children: [new Paragraph("Footer on another page")], - }), - }, - size: { - orientation: PageOrientation.LANDSCAPE, - }, - properties: { - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, - }, - children: [new Paragraph("hello in landscape")], -}); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + children: [new Paragraph("Hello World")], + }, + { + headers: { + default: new Header({ + children: [new Paragraph("First Default Header on another page")], + }), + }, + footers: { + default: new Footer({ + children: [new Paragraph("Footer on another page")], + }), + }, + properties: { + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + }, + children: [new Paragraph("hello")], + }, + { + headers: { + default: new Header({ + children: [new Paragraph("Second Default Header on another page")], + }), + }, + footers: { + default: new Footer({ + children: [new Paragraph("Footer on another page")], + }), + }, + size: { + orientation: PageOrientation.LANDSCAPE, + }, + properties: { + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + }, + children: [new Paragraph("hello in landscape")], + }, + { + headers: { + default: new Header({ children: [ - new TextRun({ - children: ["Page number: ", PageNumber.CURRENT], + new Paragraph({ + children: [ + new TextRun({ + children: ["Page number: ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - }, - size: { - orientation: PageOrientation.PORTRAIT, - }, - children: [new Paragraph("Page number in the header must be 2, because it continues from the previous section.")], -}); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ + }, + size: { + orientation: PageOrientation.PORTRAIT, + }, + children: [new Paragraph("Page number in the header must be 2, because it continues from the previous section.")], + }, + { + headers: { + default: new Header({ children: [ - new TextRun({ - children: ["Page number: ", PageNumber.CURRENT], + new Paragraph({ + children: [ + new TextRun({ + children: ["Page number: ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - }, - properties: { - pageNumberFormatType: PageNumberFormat.UPPER_ROMAN, - orientation: PageOrientation.PORTRAIT, - }, - children: [ - new Paragraph( - "Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.", - ), - ], -}); - -doc.addSection({ - headers: { - default: new Header({ + }, + properties: { + pageNumberFormatType: PageNumberFormat.UPPER_ROMAN, + orientation: PageOrientation.PORTRAIT, + }, children: [ - new Paragraph({ + new Paragraph( + "Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.", + ), + ], + }, + { + headers: { + default: new Header({ children: [ - new TextRun({ - children: ["Page number: ", PageNumber.CURRENT], + new Paragraph({ + children: [ + new TextRun({ + children: ["Page number: ", PageNumber.CURRENT], + }), + ], }), ], }), + }, + size: { + orientation: PageOrientation.PORTRAIT, + }, + properties: { + pageNumberFormatType: PageNumberFormat.DECIMAL, + pageNumberStart: 25, + }, + children: [ + new Paragraph( + "Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section.", + ), ], - }), - }, - size: { - orientation: PageOrientation.PORTRAIT, - }, - properties: { - pageNumberFormatType: PageNumberFormat.DECIMAL, - pageNumberStart: 25, - }, - children: [ - new Paragraph("Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section."), + }, ], }); diff --git a/demo/17-footnotes.ts b/demo/17-footnotes.ts index 0ba23c07cb..4f9d403ebd 100644 --- a/demo/17-footnotes.ts +++ b/demo/17-footnotes.ts @@ -12,41 +12,41 @@ const doc = new Document({ 5: { children: [new Paragraph("Test1")] }, 6: { children: [new Paragraph("My amazing reference1")] }, }, -}); - -doc.addSection({ - children: [ - new Paragraph({ + sections: [ + { children: [ - new TextRun({ - children: ["Hello", new FootnoteReferenceRun(1)], + new Paragraph({ + children: [ + new TextRun({ + children: ["Hello", new FootnoteReferenceRun(1)], + }), + new TextRun({ + children: [" World!", new FootnoteReferenceRun(2)], + }), + ], }), - new TextRun({ - children: [" World!", new FootnoteReferenceRun(2)], + new Paragraph({ + children: [new TextRun("Hello World"), new FootnoteReferenceRun(3)], }), ], - }), - new Paragraph({ - children: [new TextRun("Hello World"), new FootnoteReferenceRun(3)], - }), - ], -}); - -doc.addSection({ - children: [ - new Paragraph({ + }, + { children: [ - new TextRun({ - children: ["Hello", new FootnoteReferenceRun(4)], + new Paragraph({ + children: [ + new TextRun({ + children: ["Hello", new FootnoteReferenceRun(4)], + }), + new TextRun({ + children: [" World!", new FootnoteReferenceRun(5)], + }), + ], }), - new TextRun({ - children: [" World!", new FootnoteReferenceRun(5)], + new Paragraph({ + children: [new TextRun("Hello World"), new FootnoteReferenceRun(6)], }), ], - }), - new Paragraph({ - children: [new TextRun("Hello World"), new FootnoteReferenceRun(6)], - }), + }, ], }); diff --git a/demo/18-image-from-buffer.ts b/demo/18-image-from-buffer.ts index c4a7584c5b..6f41344117 100644 --- a/demo/18-image-from-buffer.ts +++ b/demo/18-image-from-buffer.ts @@ -3,23 +3,25 @@ import * as fs from "fs"; import { Document, ImageRun, Packer, Paragraph } from "../build"; -const doc = new Document(); - const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx8iHx8iHx8iHyAjHSAjHiAjHiAjHh4hHx8iHx8iHx8jHyAjHiAhHh4iHx8iHx8jHyAjHSAjHSAhHiAhHh4iHx8iHx8jHx8jHyAjHSAjHSAiHh4iHh4jHx8jHx8jHyAjHyAhHSAhHSAiHh4iHh4jHx8jHx8jHyAhHyAhHSAiHSAiHh4jHh4jHx8jHx8jHyAhHyAhHSAiHSAjHR4jHh4jHx8jHx8hHyAhHyAiHSAjHSAjHR4jHh4jHx8hHx8hHyAhHyAiHyAjHSAjHR4jHR4hHh4hHx8hHyAiHyAjHyAjHSAjHR4jHR4hHh4hHx8hHyAjHyAjHyAjHSAjHR4hHR4hHR4hHx8iHyAjHyAjHyAjHSAhHR4hHR4hHR4hHx8jHyAjHyAjHyAjHyC9S2xeAAAA7nRSTlMAAQIDBAUGBwgJCgsMDQ4PEBESExQVFxgZGhscHR4fICEiIyQlJicoKSorLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZISUpLTE1OUFFSU1RVVllaW1xdXmBhYmNkZWZnaGprbG1ub3Byc3R1dnd4eXp8fn+AgYKDhIWGiImKi4yNj5CRkpOUlZaXmJmam5ydnp+goaKjpKaoqqusra6vsLGys7S1tri5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+fkZpVQAABcBJREFUGBntwftjlQMcBvDnnLNL22qzJjWlKLHFVogyty3SiFq6EZliqZGyhnSxsLlMRahYoZKRFcul5dKFCatYqWZaNKvWtrPz/A2+7/b27qRzec/lPfvl/XxgMplMJpPJZDKZAtA9HJ3ppnIez0KnSdtC0RCNznHdJrbrh85wdSlVVRaEXuoGamYi5K5430HNiTiEWHKJg05eRWgNfKeV7RxbqUhGKPV/207VupQ8is0IoX5vtFC18SqEHaK4GyHTZ2kzVR8PBTCO4oANIZL4ShNVZcOhKKeYg9DoWdhI1ec3os2VFI0JCIUez5+i6st0qJZRrEAIJCw+QdW223BG/EmKwTBc/IJ/qfp2FDrkUnwFo8U9dZyqnaPhxLqfYjyM1S3vb6p+GGOBszsojoTDSDFz6qj66R4LzvYJxVMwUNRjf1H1ywQr/megg2RzLximy8waqvbda8M5iijegVEiHjlM1W/3h+FcXesphsMY4dMOUnUgOxyuPEzxPQwRNvV3qg5Nj4BreyimwADWe/dRVTMjEm6MoGLzGwtystL6RyOY3qSqdlYU3FpLZw1VW0sK5943MvUCKwJ1noNtjs6Ohge76Zq9ZkfpigU5WWkDYuCfbs1U5HWFR8/Qq4a9W0uK5k4ZmdrTCl8spGIePLPlbqqsc1Afe83O0hULc8alDYiBd7ZyitYMeBfR55rR2fOKP6ioPk2dGvZ+UVI0d8rtqT2tcCexlqK2F3wRn5Q+YVbBqrLKOupkr9lZujAOrmS0UpTb4JeIPkNHZ+cXr6uoPk2vyuBSPhWLEKj45PQJuQWryyqP0Z14uGLdROHIRNBEXDR09EP5r62rOHCazhrD4VKPwxTH+sIA3ZPTJ+YuWV22n+IruHFDC8X2CBjnPoolcGc2FYUwzmsUWXDHsoGKLBhmN0VvuBVfTVE/AAbpaid5CB4MbaLY1QXGuIViLTyZQcVyGGMuxWPwaA0Vk2GI9RRp8Ci2iuLkIBjhT5LNUfAspZFiTwyC72KK7+DNg1SsRvCNp3gZXq2k4iEEXSHFJHgVXUlxejCCbTvFAHiXdIJiXxyCK7KJ5FHoMZGK9xBcwyg2QpdlVMxEUM2iyIMuXXZQNF+HswxMsSAAJRQjoE//eoqDCXBSTO6f1xd+O0iyNRY6jaWi1ALNYCocZROj4JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`; -doc.addSection({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { children: [ - new ImageRun({ - data: Buffer.from(imageBase64Data, "base64"), - transformation: { - width: 100, - height: 100, - }, + new Paragraph({ + children: [ + new ImageRun({ + data: Buffer.from(imageBase64Data, "base64"), + transformation: { + width: 100, + height: 100, + }, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/19-export-to-base64.ts b/demo/19-export-to-base64.ts index 1d94cf5ff7..f9e871353e 100644 --- a/demo/19-export-to-base64.ts +++ b/demo/19-export-to-base64.ts @@ -3,23 +3,25 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo", - bold: true, - }), - new TextRun({ - text: "\tBar", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo", + bold: true, + }), + new TextRun({ + text: "\tBar", + bold: true, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts index 7658ee2495..5795ebf6e1 100644 --- a/demo/2-declaritive-styles.ts +++ b/demo/2-declaritive-styles.ts @@ -99,90 +99,91 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - 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({ + sections: [ + { children: [ - new TextRun({ - text: "Some monospaced content", - font: { - name: "Monospace", + 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: "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 Paragraph({ + text: "Option3", + numbering: { + reference: "my-crazy-numbering", + level: 0, + }, }), - new TextRun(" switching to normal "), - new TextRun({ - text: "and then underlined ", - underline: {}, + new Paragraph({ + children: [ + new TextRun({ + text: "Some monospaced content", + font: { + name: "Monospace", + }, + }), + ], }), - new TextRun({ - text: "and then emphasis-mark ", - emphasisMark: {}, + new Paragraph({ + text: "An aside, in light gray italics and indented", + style: "aside", }), - new TextRun({ - text: "and back to normal.", + 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 Paragraph({ + style: "Strong", + children: [ + new TextRun({ + text: "Strong Style", + }), + new TextRun({ + text: " - Very strong.", + }), + ], }), ], - }), - new Paragraph({ - style: "Strong", - children: [ - new TextRun({ - text: "Strong Style", - }), - new TextRun({ - text: " - Very strong.", - }), - ], - }), + }, ], }); diff --git a/demo/20-table-cell-borders.ts b/demo/20-table-cell-borders.ts index 028d9fdb04..4dbc9192b9 100644 --- a/demo/20-table-cell-borders.ts +++ b/demo/20-table-cell-borders.ts @@ -3,101 +3,105 @@ import * as fs from "fs"; import { BorderStyle, Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; -const doc = new Document(); - -const table = new Table({ - rows: [ - new TableRow({ +const doc = new Document({ + sections: [ + { children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], + new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [new Paragraph("Hello")], + borders: { + top: { + style: BorderStyle.DASH_DOT_STROKED, + size: 3, + color: "red", + }, + bottom: { + style: BorderStyle.DOUBLE, + size: 3, + color: "blue", + }, + left: { + style: BorderStyle.DASH_DOT_STROKED, + size: 3, + color: "green", + }, + right: { + style: BorderStyle.DASH_DOT_STROKED, + size: 3, + color: "#ff8000", + }, + }, + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], }), ], - }), - new TableRow({ - children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [new Paragraph("Hello")], - borders: { - top: { - style: BorderStyle.DASH_DOT_STROKED, - size: 3, - color: "red", - }, - bottom: { - style: BorderStyle.DOUBLE, - size: 3, - color: "blue", - }, - left: { - style: BorderStyle.DASH_DOT_STROKED, - size: 3, - color: "green", - }, - right: { - style: BorderStyle.DASH_DOT_STROKED, - size: 3, - color: "#ff8000", - }, - }, - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - ], - }), - new TableRow({ - children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - ], - }), - new TableRow({ - children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - ], - }), + }, ], }); -doc.addSection({ children: [table] }); - Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/21-bookmarks.ts b/demo/21-bookmarks.ts index b004f48690..5455ee8875 100644 --- a/demo/21-bookmarks.ts +++ b/demo/21-bookmarks.ts @@ -10,17 +10,45 @@ const doc = new Document({ creator: "Clippy", title: "Sample Document", description: "A brief example of using docx with bookmarks and internal hyperlinks", -}); - -doc.addSection({ - footers: { - default: new Footer({ + sections: [ + { + footers: { + default: new Footer({ + children: [ + new Paragraph({ + children: [ + new InternalHyperlink({ + child: new TextRun({ + text: "Click here!", + style: "Hyperlink", + }), + anchor: "myAnchorId", + }), + ], + }), + ], + }), + }, children: [ + new Paragraph({ + heading: HeadingLevel.HEADING_1, + children: [ + new Bookmark({ + id: "myAnchorId", + children: [new TextRun("Lorem Ipsum")], + }), + ], + }), + new Paragraph("\n"), + new Paragraph(LOREM_IPSUM), + new Paragraph({ + children: [new PageBreak()], + }), new Paragraph({ children: [ new InternalHyperlink({ child: new TextRun({ - text: "Click here!", + text: "Anchor Text", style: "Hyperlink", }), anchor: "myAnchorId", @@ -28,34 +56,7 @@ doc.addSection({ ], }), ], - }), - }, - children: [ - new Paragraph({ - heading: HeadingLevel.HEADING_1, - children: [ - new Bookmark({ - id: "myAnchorId", - children: [new TextRun("Lorem Ipsum")], - }), - ], - }), - new Paragraph("\n"), - new Paragraph(LOREM_IPSUM), - new Paragraph({ - children: [new PageBreak()], - }), - new Paragraph({ - children: [ - new InternalHyperlink({ - child: new TextRun({ - text: "Anchor Text", - style: "Hyperlink", - }), - anchor: "myAnchorId", - }), - ], - }), + }, ], }); diff --git a/demo/22-right-to-left-text.ts b/demo/22-right-to-left-text.ts index 4ee344391c..ceb5071fb1 100644 --- a/demo/22-right-to-left-text.ts +++ b/demo/22-right-to-left-text.ts @@ -3,64 +3,66 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, Table, TableCell, TableRow, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph({ - bidirectional: true, +const doc = new Document({ + sections: [ + { children: [ - new TextRun({ - text: "שלום עולם", - rightToLeft: true, - }), - ], - }), - new Paragraph({ - bidirectional: true, - children: [ - new TextRun({ - text: "שלום עולם", - bold: true, - rightToLeft: true, - }), - ], - }), - new Paragraph({ - bidirectional: true, - children: [ - new TextRun({ - text: "שלום עולם", - italics: true, - rightToLeft: true, - }), - ], - }), - new Table({ - visuallyRightToLeft: true, - rows: [ - new TableRow({ + new Paragraph({ + bidirectional: true, children: [ - new TableCell({ - children: [new Paragraph("שלום עולם")], - }), - new TableCell({ - children: [], + new TextRun({ + text: "שלום עולם", + rightToLeft: true, }), ], }), - new TableRow({ + new Paragraph({ + bidirectional: true, children: [ - new TableCell({ - children: [], + new TextRun({ + text: "שלום עולם", + bold: true, + rightToLeft: true, }), - new TableCell({ - children: [new Paragraph("שלום עולם")], + ], + }), + new Paragraph({ + bidirectional: true, + children: [ + new TextRun({ + text: "שלום עולם", + italics: true, + rightToLeft: true, + }), + ], + }), + new Table({ + visuallyRightToLeft: true, + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("שלום עולם")], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [new Paragraph("שלום עולם")], + }), + ], }), ], }), ], - }), + }, ], }); diff --git a/demo/23-base64-images.ts b/demo/23-base64-images.ts index a4419bcd36..9fbb7f83da 100644 --- a/demo/23-base64-images.ts +++ b/demo/23-base64-images.ts @@ -3,79 +3,81 @@ import * as fs from "fs"; import { Document, ImageRun, Packer, Paragraph, TextRun } from "../build"; -const doc = new Document(); - const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx8iHx8iHx8iHyAjHSAjHiAjHiAjHh4hHx8iHx8iHx8jHyAjHiAhHh4iHx8iHx8jHyAjHSAjHSAhHiAhHh4iHx8iHx8jHx8jHyAjHSAjHSAiHh4iHh4jHx8jHx8jHyAjHyAhHSAhHSAiHh4iHh4jHx8jHx8jHyAhHyAhHSAiHSAiHh4jHh4jHx8jHx8jHyAhHyAhHSAiHSAjHR4jHh4jHx8jHx8hHyAhHyAiHSAjHSAjHR4jHh4jHx8hHx8hHyAhHyAiHyAjHSAjHR4jHR4hHh4hHx8hHyAiHyAjHyAjHSAjHR4jHR4hHh4hHx8hHyAjHyAjHyAjHSAjHR4hHR4hHR4hHx8iHyAjHyAjHyAjHSAhHR4hHR4hHR4hHx8jHyAjHyAjHyAjHyC9S2xeAAAA7nRSTlMAAQIDBAUGBwgJCgsMDQ4PEBESExQVFxgZGhscHR4fICEiIyQlJicoKSorLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZISUpLTE1OUFFSU1RVVllaW1xdXmBhYmNkZWZnaGprbG1ub3Byc3R1dnd4eXp8fn+AgYKDhIWGiImKi4yNj5CRkpOUlZaXmJmam5ydnp+goaKjpKaoqqusra6vsLGys7S1tri5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+fkZpVQAABcBJREFUGBntwftjlQMcBvDnnLNL22qzJjWlKLHFVogyty3SiFq6EZliqZGyhnSxsLlMRahYoZKRFcul5dKFCatYqWZaNKvWtrPz/A2+7/b27qRzec/lPfvl/XxgMplMJpPJZDKZAtA9HJ3ppnIez0KnSdtC0RCNznHdJrbrh85wdSlVVRaEXuoGamYi5K5430HNiTiEWHKJg05eRWgNfKeV7RxbqUhGKPV/207VupQ8is0IoX5vtFC18SqEHaK4GyHTZ2kzVR8PBTCO4oANIZL4ShNVZcOhKKeYg9DoWdhI1ec3os2VFI0JCIUez5+i6st0qJZRrEAIJCw+QdW223BG/EmKwTBc/IJ/qfp2FDrkUnwFo8U9dZyqnaPhxLqfYjyM1S3vb6p+GGOBszsojoTDSDFz6qj66R4LzvYJxVMwUNRjf1H1ywQr/megg2RzLximy8waqvbda8M5iijegVEiHjlM1W/3h+FcXesphsMY4dMOUnUgOxyuPEzxPQwRNvV3qg5Nj4BreyimwADWe/dRVTMjEm6MoGLzGwtystL6RyOY3qSqdlYU3FpLZw1VW0sK5943MvUCKwJ1noNtjs6Ohge76Zq9ZkfpigU5WWkDYuCfbs1U5HWFR8/Qq4a9W0uK5k4ZmdrTCl8spGIePLPlbqqsc1Afe83O0hULc8alDYiBd7ZyitYMeBfR55rR2fOKP6ioPk2dGvZ+UVI0d8rtqT2tcCexlqK2F3wRn5Q+YVbBqrLKOupkr9lZujAOrmS0UpTb4JeIPkNHZ+cXr6uoPk2vyuBSPhWLEKj45PQJuQWryyqP0Z14uGLdROHIRNBEXDR09EP5r62rOHCazhrD4VKPwxTH+sIA3ZPTJ+YuWV22n+IruHFDC8X2CBjnPoolcGc2FYUwzmsUWXDHsoGKLBhmN0VvuBVfTVE/AAbpaid5CB4MbaLY1QXGuIViLTyZQcVyGGMuxWPwaA0Vk2GI9RRp8Ci2iuLkIBjhT5LNUfAspZFiTwyC72KK7+DNg1SsRvCNp3gZXq2k4iEEXSHFJHgVXUlxejCCbTvFAHiXdIJiXxyCK7KJ5FHoMZGK9xBcwyg2QpdlVMxEUM2iyIMuXXZQNF+HswxMsSAAJRQjoE//eoqDCXBSTO6f1xd+O0iyNRY6jaWi1ALNYCocZROj4JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`; -doc.addSection({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { children: [ - new TextRun("Hello World"), - new ImageRun({ - data: fs.readFileSync("./demo/images/parrots.bmp"), - transformation: { - width: 100, - height: 100, - }, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new ImageRun({ + data: fs.readFileSync("./demo/images/parrots.bmp"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/image1.jpeg"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/dog.png"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/cat.jpg"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/parrots.bmp"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: Buffer.from(imageBase64Data, "base64"), + transformation: { + width: 100, + height: 100, + }, + }), + ], }), ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/image1.jpeg"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/dog.png"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/cat.jpg"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/parrots.bmp"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: Buffer.from(imageBase64Data, "base64"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), + }, ], }); diff --git a/demo/24-images-to-table-cell.ts b/demo/24-images-to-table-cell.ts index 34d70ae02e..375149f4f0 100644 --- a/demo/24-images-to-table-cell.ts +++ b/demo/24-images-to-table-cell.ts @@ -3,93 +3,95 @@ import * as fs from "fs"; import { Document, ImageRun, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; -const doc = new Document(); - -const table = new Table({ - rows: [ - new TableRow({ +const doc = new Document({ + sections: [ + { children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - ], - }), - new TableRow({ - children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [ - new Paragraph({ + new Table({ + rows: [ + new TableRow({ children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/image1.jpeg"), - transformation: { - width: 100, - height: 100, - }, + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [ + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/image1.jpeg"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + ], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [new Paragraph("Hello")], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], }), ], }), ], }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), ], - }), - new TableRow({ - children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [new Paragraph("Hello")], - }), - new TableCell({ - children: [], - }), - ], - }), - new TableRow({ - children: [ - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - new TableCell({ - children: [], - }), - ], - }), + }, ], }); -doc.addSection({ - children: [table], -}); - Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/25-table-xml-styles.ts b/demo/25-table-xml-styles.ts index e993c5be34..de71438bbf 100644 --- a/demo/25-table-xml-styles.ts +++ b/demo/25-table-xml-styles.ts @@ -4,18 +4,13 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "../build"; const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8"); -const doc = new Document({ - title: "Title", - externalStyles: styles -}); - // Create a table and pass the XML Style const table = new Table({ - style: 'MyCustomTableStyle', + style: "MyCustomTableStyle", width: { size: 9070, - type: WidthType.DXA + type: WidthType.DXA, }, rows: [ new TableRow({ @@ -41,8 +36,14 @@ const table = new Table({ ], }); -doc.addSection({ - children: [table], +const doc = new Document({ + title: "Title", + externalStyles: styles, + sections: [ + { + children: [table], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/26-paragraph-borders.ts b/demo/26-paragraph-borders.ts index 40057d756a..c64be50429 100644 --- a/demo/26-paragraph-borders.ts +++ b/demo/26-paragraph-borders.ts @@ -3,28 +3,30 @@ import * as fs from "fs"; import { Document, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph("No border!"), - new Paragraph({ - text: "I have borders on my top and bottom sides!", - border: { - top: { - color: "auto", - space: 1, - value: "single", - size: 6, - }, - bottom: { - color: "auto", - space: 1, - value: "single", - size: 6, - }, - }, - }), +const doc = new Document({ + sections: [ + { + children: [ + new Paragraph("No border!"), + new Paragraph({ + text: "I have borders on my top and bottom sides!", + border: { + top: { + color: "auto", + space: 1, + value: "single", + size: 6, + }, + bottom: { + color: "auto", + space: 1, + value: "single", + size: 6, + }, + }, + }), + ], + }, ], }); diff --git a/demo/27-declaritive-styles-3.ts b/demo/27-declaritive-styles-3.ts index 8737c69101..a527bdf0a3 100644 --- a/demo/27-declaritive-styles-3.ts +++ b/demo/27-declaritive-styles-3.ts @@ -47,18 +47,19 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - children: [ - new Paragraph({ - text: "Hello", - style: "myWonkyStyle", - }), - new Paragraph({ - text: "World", - heading: HeadingLevel.HEADING_2, - }), + sections: [ + { + children: [ + new Paragraph({ + text: "Hello", + style: "myWonkyStyle", + }), + new Paragraph({ + text: "World", + heading: HeadingLevel.HEADING_2, + }), + ], + }, ], }); diff --git a/demo/28-table-of-contents.ts b/demo/28-table-of-contents.ts index 76789ee256..472ade8f67 100644 --- a/demo/28-table-of-contents.ts +++ b/demo/28-table-of-contents.ts @@ -3,6 +3,11 @@ import * as fs from "fs"; import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build"; +// WordprocessingML docs for TableOfContents can be found here: +// http://officeopenxml.com/WPtableOfContents.php + +// Let's define the properties for generate a TOC for heading 1-5 and MySpectacularStyle, +// making the entries be hyperlinks for the paragraph const doc = new File({ styles: { paragraphStyles: [ @@ -19,43 +24,38 @@ const doc = new File({ }, ], }, -}); - -// WordprocessingML docs for TableOfContents can be found here: -// http://officeopenxml.com/WPtableOfContents.php - -// Let's define the properties for generate a TOC for heading 1-5 and MySpectacularStyle, -// making the entries be hyperlinks for the paragraph - -doc.addSection({ - children: [ - new TableOfContents("Summary", { - hyperlink: true, - headingStyleRange: "1-5", - stylesWithLevels: [new StyleLevel("MySpectacularStyle", 1)], - }), - new Paragraph({ - text: "Header #1", - heading: HeadingLevel.HEADING_1, - pageBreakBefore: true, - }), - new Paragraph("I'm a little text very nicely written.'"), - new Paragraph({ - text: "Header #2", - heading: HeadingLevel.HEADING_1, - pageBreakBefore: true, - }), - new Paragraph("I'm a other text very nicely written.'"), - new Paragraph({ - text: "Header #2.1", - heading: HeadingLevel.HEADING_2, - }), - new Paragraph("I'm a another text very nicely written.'"), - new Paragraph({ - text: "My Spectacular Style #1", - style: "MySpectacularStyle", - pageBreakBefore: true, - }), + sections: [ + { + children: [ + new TableOfContents("Summary", { + hyperlink: true, + headingStyleRange: "1-5", + stylesWithLevels: [new StyleLevel("MySpectacularStyle", 1)], + }), + new Paragraph({ + text: "Header #1", + heading: HeadingLevel.HEADING_1, + pageBreakBefore: true, + }), + new Paragraph("I'm a little text very nicely written.'"), + new Paragraph({ + text: "Header #2", + heading: HeadingLevel.HEADING_1, + pageBreakBefore: true, + }), + new Paragraph("I'm a other text very nicely written.'"), + new Paragraph({ + text: "Header #2.1", + heading: HeadingLevel.HEADING_2, + }), + new Paragraph("I'm a another text very nicely written.'"), + new Paragraph({ + text: "My Spectacular Style #1", + style: "MySpectacularStyle", + pageBreakBefore: true, + }), + ], + }, ], }); diff --git a/demo/29-numbered-lists.ts b/demo/29-numbered-lists.ts index b1aa736479..e16d835c6d 100644 --- a/demo/29-numbered-lists.ts +++ b/demo/29-numbered-lists.ts @@ -57,225 +57,226 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - children: [ - new Paragraph({ - text: "line with contextual spacing", - numbering: { - reference: "my-crazy-reference", - level: 0, - }, - contextualSpacing: true, - spacing: { - before: 200, - }, - }), - new Paragraph({ - text: "line with contextual spacing", - numbering: { - reference: "my-crazy-reference", - level: 0, - }, - contextualSpacing: true, - spacing: { - before: 200, - }, - }), - new Paragraph({ - text: "line without contextual spacing", - numbering: { - reference: "my-crazy-reference", - level: 0, - }, - contextualSpacing: false, - spacing: { - before: 200, - }, - }), - new Paragraph({ - text: "line without contextual spacing", - numbering: { - reference: "my-crazy-reference", - level: 0, - }, - contextualSpacing: false, - spacing: { - before: 200, - }, - }), - new Paragraph({ - text: "Step 1 - Add sugar", - numbering: { - reference: "my-number-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "Step 2 - Add wheat", - numbering: { - reference: "my-number-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "Step 3 - Put in oven", - numbering: { - reference: "my-number-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "Next", - heading: HeadingLevel.HEADING_2, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - instance: 2, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - instance: 2, - }, - }), - new Paragraph({ - text: "Next", - heading: HeadingLevel.HEADING_2, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - instance: 3, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - instance: 3, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - instance: 3, - }, - }), - new Paragraph({ - text: "Next", - heading: HeadingLevel.HEADING_2, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "test", - numbering: { - reference: "padded-numbering-reference", - level: 0, - }, - }), + sections: [ + { + children: [ + new Paragraph({ + text: "line with contextual spacing", + numbering: { + reference: "my-crazy-reference", + level: 0, + }, + contextualSpacing: true, + spacing: { + before: 200, + }, + }), + new Paragraph({ + text: "line with contextual spacing", + numbering: { + reference: "my-crazy-reference", + level: 0, + }, + contextualSpacing: true, + spacing: { + before: 200, + }, + }), + new Paragraph({ + text: "line without contextual spacing", + numbering: { + reference: "my-crazy-reference", + level: 0, + }, + contextualSpacing: false, + spacing: { + before: 200, + }, + }), + new Paragraph({ + text: "line without contextual spacing", + numbering: { + reference: "my-crazy-reference", + level: 0, + }, + contextualSpacing: false, + spacing: { + before: 200, + }, + }), + new Paragraph({ + text: "Step 1 - Add sugar", + numbering: { + reference: "my-number-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "Step 2 - Add wheat", + numbering: { + reference: "my-number-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "Step 3 - Put in oven", + numbering: { + reference: "my-number-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "Next", + heading: HeadingLevel.HEADING_2, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + instance: 2, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + instance: 2, + }, + }), + new Paragraph({ + text: "Next", + heading: HeadingLevel.HEADING_2, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + instance: 3, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + instance: 3, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + instance: 3, + }, + }), + new Paragraph({ + text: "Next", + heading: HeadingLevel.HEADING_2, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "test", + numbering: { + reference: "padded-numbering-reference", + level: 0, + }, + }), + ], + }, ], }); diff --git a/demo/3-numbering-and-bullet-points.ts b/demo/3-numbering-and-bullet-points.ts index acff5772cc..3e6aaa644d 100644 --- a/demo/3-numbering-and-bullet-points.ts +++ b/demo/3-numbering-and-bullet-points.ts @@ -117,136 +117,137 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - children: [ - new Paragraph({ - text: "Hey you", - numbering: { - reference: "my-crazy-numbering", - level: 0, - }, - }), - new Paragraph({ - text: "What's up fam", - numbering: { - reference: "my-crazy-numbering", - level: 1, - }, - }), - new Paragraph({ - text: "Hello World 2", - numbering: { - reference: "my-crazy-numbering", - level: 1, - }, - }), - new Paragraph({ - text: "Yeah boi", - numbering: { - reference: "my-crazy-numbering", - level: 2, - }, - }), - new Paragraph({ - text: "Hey you", - bullet: { - level: 0, - }, - }), - new Paragraph({ - text: "What's up fam", - bullet: { - level: 1, - }, - }), - new Paragraph({ - text: "Hello World 2", - bullet: { - level: 2, - }, - }), - new Paragraph({ - text: "Yeah boi", - bullet: { - level: 3, - }, - }), - new Paragraph({ - text: "101 MSXFM", - numbering: { - reference: "my-crazy-numbering", - level: 3, - }, - }), - new Paragraph({ - text: "back to level 1", - numbering: { - reference: "my-crazy-numbering", - level: 1, - }, - }), - new Paragraph({ - text: "back to level 0", - numbering: { - reference: "my-crazy-numbering", - level: 0, - }, - }), - new Paragraph({ - text: "Custom Bullet points", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph({ - text: "What's up fam", - numbering: { - reference: "my-unique-bullet-points", - level: 0, - }, - }), - new Paragraph({ - text: "Hey you", - numbering: { - reference: "my-unique-bullet-points", - level: 0, - }, - }), - new Paragraph({ - text: "What's up fam", - numbering: { - reference: "my-unique-bullet-points", - level: 1, - }, - }), - new Paragraph({ - text: "Hello World 2", - numbering: { - reference: "my-unique-bullet-points", - level: 2, - }, - }), - new Paragraph({ - text: "Yeah boi", - numbering: { - reference: "my-unique-bullet-points", - level: 3, - }, - }), - new Paragraph({ - text: "my Awesome numbering", - numbering: { - reference: "my-unique-bullet-points", - level: 4, - }, - }), - new Paragraph({ - text: "Back to level 1", - numbering: { - reference: "my-unique-bullet-points", - level: 1, - }, - }), + sections: [ + { + children: [ + new Paragraph({ + text: "Hey you", + numbering: { + reference: "my-crazy-numbering", + level: 0, + }, + }), + new Paragraph({ + text: "What's up fam", + numbering: { + reference: "my-crazy-numbering", + level: 1, + }, + }), + new Paragraph({ + text: "Hello World 2", + numbering: { + reference: "my-crazy-numbering", + level: 1, + }, + }), + new Paragraph({ + text: "Yeah boi", + numbering: { + reference: "my-crazy-numbering", + level: 2, + }, + }), + new Paragraph({ + text: "Hey you", + bullet: { + level: 0, + }, + }), + new Paragraph({ + text: "What's up fam", + bullet: { + level: 1, + }, + }), + new Paragraph({ + text: "Hello World 2", + bullet: { + level: 2, + }, + }), + new Paragraph({ + text: "Yeah boi", + bullet: { + level: 3, + }, + }), + new Paragraph({ + text: "101 MSXFM", + numbering: { + reference: "my-crazy-numbering", + level: 3, + }, + }), + new Paragraph({ + text: "back to level 1", + numbering: { + reference: "my-crazy-numbering", + level: 1, + }, + }), + new Paragraph({ + text: "back to level 0", + numbering: { + reference: "my-crazy-numbering", + level: 0, + }, + }), + new Paragraph({ + text: "Custom Bullet points", + heading: HeadingLevel.HEADING_1, + }), + new Paragraph({ + text: "What's up fam", + numbering: { + reference: "my-unique-bullet-points", + level: 0, + }, + }), + new Paragraph({ + text: "Hey you", + numbering: { + reference: "my-unique-bullet-points", + level: 0, + }, + }), + new Paragraph({ + text: "What's up fam", + numbering: { + reference: "my-unique-bullet-points", + level: 1, + }, + }), + new Paragraph({ + text: "Hello World 2", + numbering: { + reference: "my-unique-bullet-points", + level: 2, + }, + }), + new Paragraph({ + text: "Yeah boi", + numbering: { + reference: "my-unique-bullet-points", + level: 3, + }, + }), + new Paragraph({ + text: "my Awesome numbering", + numbering: { + reference: "my-unique-bullet-points", + level: 4, + }, + }), + new Paragraph({ + text: "Back to level 1", + numbering: { + reference: "my-unique-bullet-points", + level: 1, + }, + }), + ], + }, ], }); diff --git a/demo/30-template-document.ts b/demo/30-template-document.ts index 3b3c724b5c..938502786a 100644 --- a/demo/30-template-document.ts +++ b/demo/30-template-document.ts @@ -12,16 +12,21 @@ fs.readFile(filePath, (err, data) => { } importDotx.extract(data).then((templateDocument) => { - const doc = new Document(undefined, { - template: templateDocument, - }); - - doc.addSection({ - properties: { - titlePage: templateDocument.titlePageIsDefined, + const doc = new Document( + { + sections: [ + { + properties: { + titlePage: templateDocument.titlePageIsDefined, + }, + children: [new Paragraph("Hello World")], + }, + ], }, - children: [new Paragraph("Hello World")], - }); + { + template: templateDocument, + }, + ); Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); diff --git a/demo/31-tables.ts b/demo/31-tables.ts index 24fc6030f8..cf5361c7c2 100644 --- a/demo/31-tables.ts +++ b/demo/31-tables.ts @@ -3,74 +3,76 @@ import * as fs from "fs"; import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign, TextDirection } from "../build"; -const doc = new Document(); - -const table = new Table({ - rows: [ - new TableRow({ +const doc = new Document({ + sections: [ + { children: [ - new TableCell({ - children: [new Paragraph({}), new Paragraph({})], - verticalAlign: VerticalAlign.CENTER, - }), - new TableCell({ - children: [new Paragraph({}), new Paragraph({})], - verticalAlign: VerticalAlign.CENTER, - }), - new TableCell({ - children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})], - textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT, - }), - new TableCell({ - children: [new Paragraph({ text: "top to bottom" }), new Paragraph({})], - textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT, + new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph({}), new Paragraph({})], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [new Paragraph({}), new Paragraph({})], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [new Paragraph({ text: "bottom to top" }), new Paragraph({})], + textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT, + }), + new TableCell({ + children: [new Paragraph({ text: "top to bottom" }), new Paragraph({})], + textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [ + new Paragraph({ + text: + "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah", + heading: HeadingLevel.HEADING_1, + }), + ], + }), + new TableCell({ + children: [ + new Paragraph({ + text: "This text should be in the middle of the cell", + }), + ], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [ + new Paragraph({ + text: "Text above should be vertical from bottom to top", + }), + ], + verticalAlign: VerticalAlign.CENTER, + }), + new TableCell({ + children: [ + new Paragraph({ + text: "Text above should be vertical from top to bottom", + }), + ], + verticalAlign: VerticalAlign.CENTER, + }), + ], + }), + ], }), ], - }), - new TableRow({ - children: [ - new TableCell({ - children: [ - new Paragraph({ - text: - "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah", - heading: HeadingLevel.HEADING_1, - }), - ], - }), - new TableCell({ - children: [ - new Paragraph({ - text: "This text should be in the middle of the cell", - }), - ], - verticalAlign: VerticalAlign.CENTER, - }), - new TableCell({ - children: [ - new Paragraph({ - text: "Text above should be vertical from bottom to top", - }), - ], - verticalAlign: VerticalAlign.CENTER, - }), - new TableCell({ - children: [ - new Paragraph({ - text: "Text above should be vertical from top to bottom", - }), - ], - verticalAlign: VerticalAlign.CENTER, - }), - ], - }), + }, ], }); -doc.addSection({ - children: [table], -}); - Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/32-merge-and-shade-table-cells.ts b/demo/32-merge-and-shade-table-cells.ts index 284b456e05..9ca432dc2d 100644 --- a/demo/32-merge-and-shade-table-cells.ts +++ b/demo/32-merge-and-shade-table-cells.ts @@ -17,8 +17,6 @@ import { WidthType, } from "../build"; -const doc = new Document(); - const table = new Table({ rows: [ new TableRow({ @@ -376,30 +374,33 @@ const table8 = new Table({ type: WidthType.PERCENTAGE, }, }); - -doc.addSection({ - children: [ - table, - new Paragraph({ - text: "Another table", - heading: HeadingLevel.HEADING_2, - }), - table2, - new Paragraph({ - text: "Another table", - heading: HeadingLevel.HEADING_2, - }), - table3, - new Paragraph("Merging columns 1"), - table4, - new Paragraph("Merging columns 2"), - table5, - new Paragraph("Merging columns 3"), - table6, - new Paragraph("Merging columns 4"), - table7, - new Paragraph("Merging columns 5"), - table8, +const doc = new Document({ + sections: [ + { + children: [ + table, + new Paragraph({ + text: "Another table", + heading: HeadingLevel.HEADING_2, + }), + table2, + new Paragraph({ + text: "Another table", + heading: HeadingLevel.HEADING_2, + }), + table3, + new Paragraph("Merging columns 1"), + table4, + new Paragraph("Merging columns 2"), + table5, + new Paragraph("Merging columns 3"), + table6, + new Paragraph("Merging columns 4"), + table7, + new Paragraph("Merging columns 5"), + table8, + ], + }, ], }); diff --git a/demo/33-sequential-captions.ts b/demo/33-sequential-captions.ts index aed113d0fa..cc9dca79b9 100644 --- a/demo/33-sequential-captions.ts +++ b/demo/33-sequential-captions.ts @@ -3,42 +3,44 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, SequentialIdentifier, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { children: [ - new TextRun("Hello World 1->"), - new SequentialIdentifier("Caption"), - new TextRun(" text after sequencial caption 2->"), - new SequentialIdentifier("Caption"), + new Paragraph({ + children: [ + new TextRun("Hello World 1->"), + new SequentialIdentifier("Caption"), + new TextRun(" text after sequencial caption 2->"), + new SequentialIdentifier("Caption"), + ], + }), + new Paragraph({ + children: [ + new TextRun("Hello World 1->"), + new SequentialIdentifier("Label"), + new TextRun(" text after sequencial caption 2->"), + new SequentialIdentifier("Label"), + ], + }), + new Paragraph({ + children: [ + new TextRun("Hello World 1->"), + new SequentialIdentifier("Another"), + new TextRun(" text after sequencial caption 3->"), + new SequentialIdentifier("Label"), + ], + }), + new Paragraph({ + children: [ + new TextRun("Hello World 2->"), + new SequentialIdentifier("Another"), + new TextRun(" text after sequencial caption 4->"), + new SequentialIdentifier("Label"), + ], + }), ], - }), - new Paragraph({ - children: [ - new TextRun("Hello World 1->"), - new SequentialIdentifier("Label"), - new TextRun(" text after sequencial caption 2->"), - new SequentialIdentifier("Label"), - ], - }), - new Paragraph({ - children: [ - new TextRun("Hello World 1->"), - new SequentialIdentifier("Another"), - new TextRun(" text after sequencial caption 3->"), - new SequentialIdentifier("Label"), - ], - }), - new Paragraph({ - children: [ - new TextRun("Hello World 2->"), - new SequentialIdentifier("Another"), - new TextRun(" text after sequencial caption 4->"), - new SequentialIdentifier("Label"), - ], - }), + }, ], }); diff --git a/demo/34-floating-tables.ts b/demo/34-floating-tables.ts index 2c9d8657fa..050f5361ff 100644 --- a/demo/34-floating-tables.ts +++ b/demo/34-floating-tables.ts @@ -16,8 +16,6 @@ import { WidthType, } from "../build"; -const doc = new Document(); - const table = new Table({ rows: [ new TableRow({ @@ -57,8 +55,12 @@ const table = new Table({ layout: TableLayoutType.FIXED, }); -doc.addSection({ - children: [table], +const doc = new Document({ + sections: [ + { + children: [table], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/35-hyperlinks.ts b/demo/35-hyperlinks.ts index bb9b82a433..f8aca4f622 100644 --- a/demo/35-hyperlinks.ts +++ b/demo/35-hyperlinks.ts @@ -22,79 +22,80 @@ const doc = new Document({ ], }, }, -}); - -doc.addSection({ - footers: { - default: new Footer({ + sections: [ + { + footers: { + default: new Footer({ + children: [ + new Paragraph({ + children: [ + new TextRun("Click here for the "), + new ExternalHyperlink({ + child: new TextRun({ + text: "Footer external hyperlink", + style: "Hyperlink", + }), + link: "http://www.example.com", + }), + ], + }), + ], + }), + }, + headers: { + default: new Footer({ + children: [ + new Paragraph({ + children: [ + new TextRun("Click here for the "), + new ExternalHyperlink({ + child: new TextRun({ + text: "Header external hyperlink", + style: "Hyperlink", + }), + link: "http://www.google.com", + }), + ], + }), + ], + }), + }, children: [ new Paragraph({ children: [ - new TextRun("Click here for the "), new ExternalHyperlink({ child: new TextRun({ - text: "Footer external hyperlink", + text: "Anchor Text", style: "Hyperlink", }), link: "http://www.example.com", }), + new FootnoteReferenceRun(1), ], }), - ], - }), - }, - headers: { - default: new Footer({ - children: [ new Paragraph({ children: [ - new TextRun("Click here for the "), new ExternalHyperlink({ - child: new TextRun({ - text: "Header external hyperlink", - style: "Hyperlink", + child: new ImageRun({ + data: fs.readFileSync("./demo/images/image1.jpeg"), + transformation: { + width: 100, + height: 100, + }, }), link: "http://www.google.com", }), + new ExternalHyperlink({ + child: new TextRun({ + text: "BBC News Link", + style: "Hyperlink", + }), + link: "https://www.bbc.co.uk/news", + }), ], }), ], - }), - }, - children: [ - new Paragraph({ - children: [ - new ExternalHyperlink({ - child: new TextRun({ - text: "Anchor Text", - style: "Hyperlink", - }), - link: "http://www.example.com", - }), - new FootnoteReferenceRun(1), - ], - }), - new Paragraph({ - children: [ - new ExternalHyperlink({ - child: new ImageRun({ - data: fs.readFileSync("./demo/images/image1.jpeg"), - transformation: { - width: 100, - height: 100, - }, - }), - link: "http://www.google.com", - }), - new ExternalHyperlink({ - child: new TextRun({ - text: "BBC News Link", - style: "Hyperlink", - }), - link: "https://www.bbc.co.uk/news", - }), - ], - }), + }, ], }); diff --git a/demo/36-image-to-table-cell.ts b/demo/36-image-to-table-cell.ts index 99ad84ddb3..818c91d659 100644 --- a/demo/36-image-to-table-cell.ts +++ b/demo/36-image-to-table-cell.ts @@ -3,8 +3,6 @@ import * as fs from "fs"; import { Document, Header, ImageRun, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; -const doc = new Document(); - const table = new Table({ rows: [ new TableRow({ @@ -69,13 +67,17 @@ const table = new Table({ }); // Adding same table in the body and in the header -doc.addSection({ - headers: { - default: new Header({ +const doc = new Document({ + sections: [ + { + headers: { + default: new Header({ + children: [table], + }), + }, children: [table], - }), - }, - children: [table], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/37-images-to-header-and-footer.ts b/demo/37-images-to-header-and-footer.ts index 8eaaf4db21..561020938c 100644 --- a/demo/37-images-to-header-and-footer.ts +++ b/demo/37-images-to-header-and-footer.ts @@ -3,49 +3,51 @@ import * as fs from "fs"; import { Document, Header, ImageRun, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + headers: { + default: new Header({ children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/image1.jpeg"), - transformation: { - width: 100, - height: 100, - }, + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/image1.jpeg"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/image1.jpeg"), + transformation: { + width: 100, + height: 100, + }, + }), + ], }), ], }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/image1.jpeg"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - ], - }), - }, - children: [new Paragraph("Hello World")], + }, + children: [new Paragraph("Hello World")], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/38-text-wrapping.ts b/demo/38-text-wrapping.ts index 220cf6162b..fb7a34d00f 100644 --- a/demo/38-text-wrapping.ts +++ b/demo/38-text-wrapping.ts @@ -4,44 +4,48 @@ import * as fs from "fs"; // import { Document, Packer, Paragraph } from "../build"; import { Document, ImageRun, Packer, Paragraph, TextWrappingSide, TextWrappingType } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph( - "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vehicula nec nulla vitae efficitur. Ut interdum mauris eu ipsum rhoncus, nec pharetra velit placerat. Sed vehicula libero ac urna molestie, id pharetra est pellentesque. Praesent iaculis vehicula fringilla. Duis pretium gravida orci eu vestibulum. Mauris tincidunt ipsum dolor, ut ornare dolor pellentesque id. Integer in nulla gravida, lacinia ante non, commodo ex. Vivamus vulputate nisl id lectus finibus vulputate. Ut et nisl mi. Cras fermentum augue arcu, ac accumsan elit euismod id. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed ac posuere nisi. Pellentesque tincidunt vehicula bibendum. Phasellus eleifend viverra nisl.", - ), - new Paragraph( - "Proin ac purus faucibus, porttitor magna ut, cursus nisl. Vivamus ante purus, porta accumsan nibh eget, eleifend dignissim odio. Integer sed dictum est, aliquam lacinia justo. Donec ultrices auctor venenatis. Etiam interdum et elit nec elementum. Pellentesque nec viverra mauris. Etiam suscipit leo nec velit fringilla mattis. Pellentesque justo lacus, sodales eu condimentum in, dapibus finibus lacus. Morbi vitae nibh sit amet sem molestie feugiat. In non porttitor enim.", - ), - new Paragraph( - "Ut eget diam cursus quam accumsan interdum at id ante. Ut mollis mollis arcu, eu scelerisque dui tempus in. Quisque aliquam, augue quis ornare aliquam, ex purus ultrices mauris, ut porta dolor dolor nec justo. Nunc a tempus odio, eu viverra arcu. Suspendisse vitae nibh nec mi pharetra tempus. Mauris ut ullamcorper sapien, et sagittis sapien. Vestibulum in urna metus. In scelerisque, massa id bibendum tempus, quam orci rutrum turpis, a feugiat nisi ligula id metus. Praesent id dictum purus. Proin interdum ipsum nulla.", - ), - new Paragraph( - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 200, - height: 200, - }, - floating: { - horizontalPosition: { - offset: 2014400, - }, - verticalPosition: { - offset: 2014400, - }, - wrap: { - type: TextWrappingType.SQUARE, - side: TextWrappingSide.BOTH_SIDES, - }, - margins: { - top: 201440, - bottom: 201440, - }, - }, - }), - ), +const doc = new Document({ + sections: [ + { + children: [ + new Paragraph( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vehicula nec nulla vitae efficitur. Ut interdum mauris eu ipsum rhoncus, nec pharetra velit placerat. Sed vehicula libero ac urna molestie, id pharetra est pellentesque. Praesent iaculis vehicula fringilla. Duis pretium gravida orci eu vestibulum. Mauris tincidunt ipsum dolor, ut ornare dolor pellentesque id. Integer in nulla gravida, lacinia ante non, commodo ex. Vivamus vulputate nisl id lectus finibus vulputate. Ut et nisl mi. Cras fermentum augue arcu, ac accumsan elit euismod id. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed ac posuere nisi. Pellentesque tincidunt vehicula bibendum. Phasellus eleifend viverra nisl.", + ), + new Paragraph( + "Proin ac purus faucibus, porttitor magna ut, cursus nisl. Vivamus ante purus, porta accumsan nibh eget, eleifend dignissim odio. Integer sed dictum est, aliquam lacinia justo. Donec ultrices auctor venenatis. Etiam interdum et elit nec elementum. Pellentesque nec viverra mauris. Etiam suscipit leo nec velit fringilla mattis. Pellentesque justo lacus, sodales eu condimentum in, dapibus finibus lacus. Morbi vitae nibh sit amet sem molestie feugiat. In non porttitor enim.", + ), + new Paragraph( + "Ut eget diam cursus quam accumsan interdum at id ante. Ut mollis mollis arcu, eu scelerisque dui tempus in. Quisque aliquam, augue quis ornare aliquam, ex purus ultrices mauris, ut porta dolor dolor nec justo. Nunc a tempus odio, eu viverra arcu. Suspendisse vitae nibh nec mi pharetra tempus. Mauris ut ullamcorper sapien, et sagittis sapien. Vestibulum in urna metus. In scelerisque, massa id bibendum tempus, quam orci rutrum turpis, a feugiat nisi ligula id metus. Praesent id dictum purus. Proin interdum ipsum nulla.", + ), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 200, + height: 200, + }, + floating: { + horizontalPosition: { + offset: 2014400, + }, + verticalPosition: { + offset: 2014400, + }, + wrap: { + type: TextWrappingType.SQUARE, + side: TextWrappingSide.BOTH_SIDES, + }, + margins: { + top: 201440, + bottom: 201440, + }, + }, + }), + ], + }), + ], + }, ], }); diff --git a/demo/39-page-numbers.ts b/demo/39-page-numbers.ts index 2ab61419ef..44dd3719e4 100644 --- a/demo/39-page-numbers.ts +++ b/demo/39-page-numbers.ts @@ -3,64 +3,70 @@ import * as fs from "fs"; import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build"; -const doc = new Document({}); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + properties: { + page: { + pageNumbers: { + start: 1, + formatType: PageNumberFormat.DECIMAL, + }, + }, + }, + headers: { + default: new Header({ children: [ - new TextRun("Foo Bar corp. "), - new TextRun({ - children: ["Page Number ", PageNumber.CURRENT], - }), - new TextRun({ - children: [" to ", PageNumber.TOTAL_PAGES], + new Paragraph({ + children: [ + new TextRun("Foo Bar corp. "), + new TextRun({ + children: ["Page Number ", PageNumber.CURRENT], + }), + new TextRun({ + children: [" to ", PageNumber.TOTAL_PAGES], + }), + ], }), ], }), - ], - }), - }, - footers: { - default: new Footer({ - children: [ - new Paragraph({ - alignment: AlignmentType.CENTER, + }, + footers: { + default: new Footer({ children: [ - new TextRun("Foo Bar corp. "), - new TextRun({ - children: ["Page Number: ", PageNumber.CURRENT], - }), - new TextRun({ - children: [" to ", PageNumber.TOTAL_PAGES], + new Paragraph({ + alignment: AlignmentType.CENTER, + children: [ + new TextRun("Foo Bar corp. "), + new TextRun({ + children: ["Page Number: ", PageNumber.CURRENT], + }), + new TextRun({ + children: [" to ", PageNumber.TOTAL_PAGES], + }), + ], }), ], }), + }, + children: [ + new Paragraph({ + children: [new TextRun("Hello World 1"), new PageBreak()], + }), + new Paragraph({ + children: [new TextRun("Hello World 2"), new PageBreak()], + }), + new Paragraph({ + children: [new TextRun("Hello World 3"), new PageBreak()], + }), + new Paragraph({ + children: [new TextRun("Hello World 4"), new PageBreak()], + }), + new Paragraph({ + children: [new TextRun("Hello World 5"), new PageBreak()], + }), ], - }), - }, - properties: { - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, - }, - children: [ - new Paragraph({ - children: [new TextRun("Hello World 1"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 2"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 3"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 4"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 5"), new PageBreak()], - }), + }, ], }); diff --git a/demo/4-basic-table.ts b/demo/4-basic-table.ts index 287e54808a..00240fc99b 100644 --- a/demo/4-basic-table.ts +++ b/demo/4-basic-table.ts @@ -3,8 +3,6 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "../build"; -const doc = new Document(); - const table = new Table({ columnWidths: [3505, 5505], rows: [ @@ -114,14 +112,18 @@ const table3 = new Table({ ], }); -doc.addSection({ - children: [ - new Paragraph({ text: "Table with skewed widths" }), - table, - new Paragraph({ text: "Table with equal widths" }), - table2, - new Paragraph({ text: "Table without setting widths" }), - table3, +const doc = new Document({ + sections: [ + { + children: [ + new Paragraph({ text: "Table with skewed widths" }), + table, + new Paragraph({ text: "Table with equal widths" }), + table2, + new Paragraph({ text: "Table without setting widths" }), + table3, + ], + }, ], }); diff --git a/demo/40-line-numbers.ts b/demo/40-line-numbers.ts index d8da6f3074..f8765874ee 100644 --- a/demo/40-line-numbers.ts +++ b/demo/40-line-numbers.ts @@ -3,24 +3,28 @@ import * as fs from "fs"; import { Document, HeadingLevel, LineNumberRestartFormat, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: { - lineNumberCountBy: 1, - lineNumberRestart: LineNumberRestartFormat.CONTINUOUS, - }, - children: [ - new Paragraph({ - text: "Hello", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph( - "Himenaeos duis luctus nullam fermentum lobortis potenti vivamus non dis, sed facilisis ultricies scelerisque aenean risus hac senectus. Adipiscing id venenatis justo ante gravida placerat, ac curabitur dis pellentesque proin bibendum risus, aliquam porta taciti vulputate primis. Tortor ipsum fermentum quam vel convallis primis nisl praesent tincidunt, lobortis quisque felis vitae condimentum class ut sem nam, aenean potenti pretium ac amet lacinia himenaeos mi. Aliquam nisl turpis hendrerit est morbi malesuada, augue interdum mus inceptos curabitur tristique, parturient feugiat sodales nulla facilisi. Aliquam non pulvinar purus nulla ex integer, velit faucibus vitae at bibendum quam, risus elit aenean adipiscing posuere.", - ), - new Paragraph( - "Sed laoreet id mattis egestas nam mollis elit lacinia convallis dui tincidunt ultricies habitant, pharetra per maximus interdum neque tempor risus efficitur morbi imperdiet senectus. Lectus laoreet senectus finibus inceptos donec potenti fermentum, ultrices eleifend odio suscipit magnis tellus maximus nibh, ac sit nullam eget felis himenaeos. Diam class sem magnis aenean commodo faucibus id proin mi, nullam sodales nec mus parturient ornare ad inceptos velit hendrerit, bibendum placerat eleifend integer facilisis urna dictumst suspendisse.", - ), +const doc = new Document({ + sections: [ + { + properties: { + lineNumbers: { + countBy: 1, + restart: LineNumberRestartFormat.CONTINUOUS, + }, + }, + children: [ + new Paragraph({ + text: "Hello", + heading: HeadingLevel.HEADING_1, + }), + new Paragraph( + "Himenaeos duis luctus nullam fermentum lobortis potenti vivamus non dis, sed facilisis ultricies scelerisque aenean risus hac senectus. Adipiscing id venenatis justo ante gravida placerat, ac curabitur dis pellentesque proin bibendum risus, aliquam porta taciti vulputate primis. Tortor ipsum fermentum quam vel convallis primis nisl praesent tincidunt, lobortis quisque felis vitae condimentum class ut sem nam, aenean potenti pretium ac amet lacinia himenaeos mi. Aliquam nisl turpis hendrerit est morbi malesuada, augue interdum mus inceptos curabitur tristique, parturient feugiat sodales nulla facilisi. Aliquam non pulvinar purus nulla ex integer, velit faucibus vitae at bibendum quam, risus elit aenean adipiscing posuere.", + ), + new Paragraph( + "Sed laoreet id mattis egestas nam mollis elit lacinia convallis dui tincidunt ultricies habitant, pharetra per maximus interdum neque tempor risus efficitur morbi imperdiet senectus. Lectus laoreet senectus finibus inceptos donec potenti fermentum, ultrices eleifend odio suscipit magnis tellus maximus nibh, ac sit nullam eget felis himenaeos. Diam class sem magnis aenean commodo faucibus id proin mi, nullam sodales nec mus parturient ornare ad inceptos velit hendrerit, bibendum placerat eleifend integer facilisis urna dictumst suspendisse.", + ), + ], + }, ], }); diff --git a/demo/41-merge-table-cells-2.ts b/demo/41-merge-table-cells-2.ts index 94e7bffe1d..04c00651e6 100644 --- a/demo/41-merge-table-cells-2.ts +++ b/demo/41-merge-table-cells-2.ts @@ -3,8 +3,6 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; -const doc = new Document(); - const table = new Table({ rows: [ new TableRow({ @@ -252,8 +250,12 @@ const table2 = new Table({ ], }); -doc.addSection({ - children: [table, new Paragraph(""), table2], +const doc = new Document({ + sections: [ + { + children: [table, new Paragraph(""), table2], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/42-restart-page-numbers.ts b/demo/42-restart-page-numbers.ts index 0ecfcee6cc..c8b9636e29 100644 --- a/demo/42-restart-page-numbers.ts +++ b/demo/42-restart-page-numbers.ts @@ -3,83 +3,88 @@ import * as fs from "fs"; import { AlignmentType, Document, Header, Packer, PageBreak, PageNumber, PageNumberSeparator, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, +const doc = new Document({ + sections: [ + { + headers: { + default: new Header({ children: [ - new TextRun("My Title "), - new TextRun({ - children: ["Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("My Title "), + new TextRun({ + children: ["Page ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - first: new Header({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, + first: new Header({ children: [ - new TextRun("First Page Header "), - new TextRun({ - children: ["Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("First Page Header "), + new TextRun({ + children: ["Page ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - }, - children: [ - new Paragraph({ - children: [new TextRun("First Page"), new PageBreak()], - }), - new Paragraph("Second Page"), - ], -}); - -doc.addSection({ - properties: { - pageNumberStart: 1, - pageNumberSeparator: PageNumberSeparator.EM_DASH - }, - headers: { - default: new Header({ + }, children: [ new Paragraph({ - alignment: AlignmentType.RIGHT, + children: [new TextRun("First Page"), new PageBreak()], + }), + new Paragraph("Second Page"), + ], + }, + { + properties: { + page: { + pageNumbers: { + start: 1, + separator: PageNumberSeparator.EM_DASH, + }, + }, + }, + headers: { + default: new Header({ children: [ - new TextRun("My Title "), - new TextRun({ - children: ["Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("My Title "), + new TextRun({ + children: ["Page ", PageNumber.CURRENT], + }), + ], }), ], }), - ], - }), - first: new Header({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, + first: new Header({ children: [ - new TextRun("First Page Header of Second section"), - new TextRun({ - children: ["Page ", PageNumber.CURRENT], + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun("First Page Header of Second section"), + new TextRun({ + children: ["Page ", PageNumber.CURRENT], + }), + ], }), ], }), + }, + children: [ + new Paragraph({ + children: [new TextRun("Third Page"), new PageBreak()], + }), + new Paragraph("Fourth Page"), ], - }), - }, - children: [ - new Paragraph({ - children: [new TextRun("Third Page"), new PageBreak()], - }), - new Paragraph("Fourth Page"), + }, ], }); diff --git a/demo/43-images-to-table-cell-2.ts b/demo/43-images-to-table-cell-2.ts index ab950d284c..e34e1fa1a2 100644 --- a/demo/43-images-to-table-cell-2.ts +++ b/demo/43-images-to-table-cell-2.ts @@ -3,8 +3,6 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; -const doc = new Document(); - const table = new Table({ rows: [ new TableRow({ @@ -75,8 +73,12 @@ const table = new Table({ ], }); -doc.addSection({ - children: [table], +const doc = new Document({ + sections: [ + { + children: [table], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/44-multiple-columns.ts b/demo/44-multiple-columns.ts index 6e01e874e7..13657eaaa4 100644 --- a/demo/44-multiple-columns.ts +++ b/demo/44-multiple-columns.ts @@ -3,51 +3,51 @@ import * as fs from "fs"; import { Document, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: { - column: { - space: 708, - count: 2, +const doc = new Document({ + sections: [ + { + properties: { + column: { + space: 708, + count: 2, + }, + }, + children: [ + new Paragraph("This text will be split into 2 columns on a page."), + new Paragraph( + "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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + ), + ], }, - }, - children: [ - new Paragraph("This text will be split into 2 columns on a page."), - new Paragraph( - "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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", - ), - ], -}); - -doc.addSection({ - properties: { - column: { - space: 708, - count: 3, + { + properties: { + column: { + space: 708, + count: 3, + }, + }, + children: [ + new Paragraph("This text will be split into 3 columns on a page."), + new Paragraph( + "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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + ), + ], }, - }, - children: [ - new Paragraph("This text will be split into 3 columns on a page."), - new Paragraph( - "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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", - ), - ], -}); - -doc.addSection({ - properties: { - column: { - space: 708, - count: 2, - separate: true, + { + properties: { + column: { + space: 708, + count: 2, + separate: true, + }, + }, + children: [ + new Paragraph("This text will be split into 2 columns on a page."), + new Paragraph( + "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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + ), + ], }, - }, - children: [ - new Paragraph("This text will be split into 2 columns on a page."), - new Paragraph( - "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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", - ), ], }); diff --git a/demo/45-highlighting-text.ts b/demo/45-highlighting-text.ts index d33a99fb94..373e9b4b17 100644 --- a/demo/45-highlighting-text.ts +++ b/demo/45-highlighting-text.ts @@ -3,31 +3,33 @@ import * as fs from "fs"; import { AlignmentType, Document, Header, Packer, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, +const doc = new Document({ + sections: [ + { + headers: { + default: new Header({ children: [ - new TextRun({ - text: "Hello World", - color: "red", - bold: true, - size: 24, - font: { - name: "Garamond", - }, - highlight: "yellow", + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun({ + text: "Hello World", + color: "red", + bold: true, + size: 24, + font: { + name: "Garamond", + }, + highlight: "yellow", + }), + ], }), ], }), - ], - }), - }, - children: [], + }, + children: [], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/46-shading-text.ts b/demo/46-shading-text.ts index 2c440d73e5..39f4f461ac 100644 --- a/demo/46-shading-text.ts +++ b/demo/46-shading-text.ts @@ -3,59 +3,61 @@ import * as fs from "fs"; import { AlignmentType, Document, Header, Packer, Paragraph, ShadingType, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ - alignment: AlignmentType.RIGHT, +const doc = new Document({ + sections: [ + { + headers: { + default: new Header({ children: [ - new TextRun({ - text: "Hello World", - color: "red", - bold: true, - size: 24, - font: { - name: "Garamond", - }, + new Paragraph({ + alignment: AlignmentType.RIGHT, + children: [ + new TextRun({ + text: "Hello World", + color: "red", + bold: true, + size: 24, + font: { + name: "Garamond", + }, + shading: { + type: ShadingType.REVERSE_DIAGONAL_STRIPE, + color: "00FFFF", + fill: "FF0000", + }, + }), + ], + }), + new Paragraph({ shading: { - type: ShadingType.REVERSE_DIAGONAL_STRIPE, + type: ShadingType.DIAGONAL_CROSS, color: "00FFFF", fill: "FF0000", }, + children: [ + new TextRun({ + text: "Hello World for entire paragraph", + }), + ], }), ], }), + }, + children: [ new Paragraph({ - shading: { - type: ShadingType.DIAGONAL_CROSS, - color: "00FFFF", - fill: "FF0000", - }, children: [ new TextRun({ - text: "Hello World for entire paragraph", + emboss: true, + text: "Embossed text - hello world", + }), + new TextRun({ + imprint: true, + text: "Imprinted text - hello world", }), ], }), ], - }), - }, - children: [ - new Paragraph({ - children: [ - new TextRun({ - emboss: true, - text: "Embossed text - hello world", - }), - new TextRun({ - imprint: true, - text: "Imprinted text - hello world", - }), - ], - }), + }, ], }); diff --git a/demo/47-number-of-total-pages-section.ts b/demo/47-number-of-total-pages-section.ts index 7e8cf91b2c..b19cc0b158 100644 --- a/demo/47-number-of-total-pages-section.ts +++ b/demo/47-number-of-total-pages-section.ts @@ -3,8 +3,6 @@ import * as fs from "fs"; import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build"; -const doc = new Document(); - const header = new Header({ children: [ new Paragraph({ @@ -26,39 +24,50 @@ const footer = new Footer({ children: [new Paragraph("Foo Bar corp. ")], }); -doc.addSection({ - headers: { - default: header, - }, - footers: { - default: footer, - }, - properties: { - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, - }, - children: [ - new Paragraph({ - children: [new TextRun("Section 1"), new PageBreak(), new TextRun("Section 1"), new PageBreak()], - }), - ], -}); - -doc.addSection({ - headers: { - default: header, - }, - footers: { - default: footer, - }, - properties: { - pageNumberStart: 1, - pageNumberFormatType: PageNumberFormat.DECIMAL, - }, - children: [ - new Paragraph({ - children: [new TextRun("Section 2"), new PageBreak(), new TextRun("Section 2"), new PageBreak()], - }), +const doc = new Document({ + sections: [ + { + properties: { + page: { + pageNumbers: { + start: 1, + formatType: PageNumberFormat.DECIMAL, + }, + }, + }, + headers: { + default: header, + }, + footers: { + default: footer, + }, + children: [ + new Paragraph({ + children: [new TextRun("Section 1"), new PageBreak(), new TextRun("Section 1"), new PageBreak()], + }), + ], + }, + { + properties: { + page: { + pageNumbers: { + start: 1, + formatType: PageNumberFormat.DECIMAL, + }, + }, + }, + headers: { + default: header, + }, + footers: { + default: footer, + }, + children: [ + new Paragraph({ + children: [new TextRun("Section 2"), new PageBreak(), new TextRun("Section 2"), new PageBreak()], + }), + ], + }, ], }); diff --git a/demo/48-vertical-align.ts b/demo/48-vertical-align.ts index 35181d271c..123a784993 100644 --- a/demo/48-vertical-align.ts +++ b/demo/48-vertical-align.ts @@ -3,26 +3,28 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, SectionVerticalAlignValue, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: { - verticalAlign: SectionVerticalAlignValue.CENTER, - }, - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + properties: { + verticalAlign: SectionVerticalAlignValue.CENTER, + }, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, - }), - new TextRun({ - text: "\tGithub is the best", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + new TextRun({ + text: "\tGithub is the best", + bold: true, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/49-table-borders.ts b/demo/49-table-borders.ts index 75dd89c49f..aeccd73dd2 100644 --- a/demo/49-table-borders.ts +++ b/demo/49-table-borders.ts @@ -15,8 +15,6 @@ import { VerticalAlign, } from "../build"; -const doc = new Document(); - const table = new Table({ rows: [ new TableRow({ @@ -160,7 +158,9 @@ const noBorderTable = new Table({ ], }); -doc.addSection({ children: [table, new Paragraph("Hello"), noBorderTable] }); +const doc = new Document({ + sections: [{ children: [table, new Paragraph("Hello"), noBorderTable] }], +}); Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); diff --git a/demo/5-images.ts b/demo/5-images.ts index 137565d167..3d1da1633c 100644 --- a/demo/5-images.ts +++ b/demo/5-images.ts @@ -12,120 +12,122 @@ import { VerticalPositionRelativeFrom, } from "../build"; -const doc = new Document(); - -doc.addSection({ - children: [ - new Paragraph("Hello World"), - new Paragraph({ +const doc = new Document({ + sections: [ + { children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/image1.jpeg"), - transformation: { - width: 100, - height: 100, - }, + new Paragraph("Hello World"), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/image1.jpeg"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/dog.png").toString("base64"), + transformation: { + width: 100, + height: 100, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/cat.jpg"), + transformation: { + width: 100, + height: 100, + flip: { + vertical: true, + }, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/parrots.bmp"), + transformation: { + width: 150, + height: 150, + flip: { + horizontal: true, + }, + rotation: 225, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 200, + height: 200, + flip: { + horizontal: true, + vertical: true, + }, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 200, + height: 200, + rotation: 45, + }, + floating: { + zIndex: 10, + horizontalPosition: { + offset: 1014400, + }, + verticalPosition: { + offset: 1014400, + }, + }, + }), + ], + }), + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/cat.jpg"), + transformation: { + width: 200, + height: 200, + }, + floating: { + zIndex: 5, + horizontalPosition: { + relative: HorizontalPositionRelativeFrom.PAGE, + align: HorizontalPositionAlign.RIGHT, + }, + verticalPosition: { + relative: VerticalPositionRelativeFrom.PAGE, + align: VerticalPositionAlign.BOTTOM, + }, + }, + }), + ], }), ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/dog.png").toString("base64"), - transformation: { - width: 100, - height: 100, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/cat.jpg"), - transformation: { - width: 100, - height: 100, - flip: { - vertical: true, - }, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/parrots.bmp"), - transformation: { - width: 150, - height: 150, - flip: { - horizontal: true, - }, - rotation: 225, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 200, - height: 200, - flip: { - horizontal: true, - vertical: true, - }, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 200, - height: 200, - rotation: 45, - }, - floating: { - zIndex: 10, - horizontalPosition: { - offset: 1014400, - }, - verticalPosition: { - offset: 1014400, - }, - }, - }), - ], - }), - new Paragraph({ - children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/cat.jpg"), - transformation: { - width: 200, - height: 200, - }, - floating: { - zIndex: 5, - horizontalPosition: { - relative: HorizontalPositionRelativeFrom.PAGE, - align: HorizontalPositionAlign.RIGHT, - }, - verticalPosition: { - relative: VerticalPositionRelativeFrom.PAGE, - align: VerticalPositionAlign.BOTTOM, - }, - }, - }), - ], - }), + }, ], }); diff --git a/demo/50-readme-demo.ts b/demo/50-readme-demo.ts index c1bc1e3eda..440a230a4f 100644 --- a/demo/50-readme-demo.ts +++ b/demo/50-readme-demo.ts @@ -3,8 +3,6 @@ import * as fs from "fs"; import { Document, HeadingLevel, ImageRun, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign } from "../build"; -const doc = new Document(); - const table = new Table({ rows: [ new TableRow({ @@ -66,24 +64,28 @@ const table = new Table({ ], }); -doc.addSection({ - children: [ - new Paragraph({ - text: "Hello World", - heading: HeadingLevel.HEADING_1, - }), - table, - new Paragraph({ +const doc = new Document({ + sections: [ + { children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 100, - height: 100, - }, + new Paragraph({ + text: "Hello World", + heading: HeadingLevel.HEADING_1, + }), + table, + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 100, + height: 100, + }, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/51-character-styles.ts b/demo/51-character-styles.ts index a6064a4ddc..8209ef705f 100644 --- a/demo/51-character-styles.ts +++ b/demo/51-character-styles.ts @@ -25,30 +25,31 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - children: [ - new Paragraph({ + sections: [ + { children: [ - new TextRun({ - text: "Foo bar", - style: "myRedStyle", + new Paragraph({ + children: [ + new TextRun({ + text: "Foo bar", + style: "myRedStyle", + }), + ], + }), + new Paragraph({ + children: [ + new TextRun({ + text: "First Word", + style: "strong", + }), + new TextRun({ + 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.", + }), + ], }), ], - }), - new Paragraph({ - children: [ - new TextRun({ - text: "First Word", - style: "strong", - }), - new TextRun({ - 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.", - }), - ], - }), + }, ], }); diff --git a/demo/52-japanese.ts b/demo/52-japanese.ts index 5339c4b3a9..4bbf5225bd 100644 --- a/demo/52-japanese.ts +++ b/demo/52-japanese.ts @@ -18,17 +18,18 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - children: [ - new Paragraph({ - text: "KFCを食べるのが好き", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph({ - text: "こんにちは", - }), + sections: [ + { + children: [ + new Paragraph({ + text: "KFCを食べるのが好き", + heading: HeadingLevel.HEADING_1, + }), + new Paragraph({ + text: "こんにちは", + }), + ], + }, ], }); diff --git a/demo/53-chinese.ts b/demo/53-chinese.ts index 39e686a95a..f32feab14d 100644 --- a/demo/53-chinese.ts +++ b/demo/53-chinese.ts @@ -41,47 +41,48 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - children: [ - new Paragraph({ - text: "中文和英文 Chinese and English", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph({ - text: "中文和英文 Chinese and English", - }), - new Paragraph({ + sections: [ + { children: [ - new TextRun({ + new Paragraph({ text: "中文和英文 Chinese and English", - font: { eastAsia: "SimSun" }, // set eastAsia to "SimSun". - // The ascii characters will use the default font ("Times") specified in paragraphStyles + heading: HeadingLevel.HEADING_1, + }), + new Paragraph({ + text: "中文和英文 Chinese and English", + }), + new Paragraph({ + children: [ + new TextRun({ + text: "中文和英文 Chinese and English", + font: { eastAsia: "SimSun" }, // set eastAsia to "SimSun". + // The ascii characters will use the default font ("Times") specified in paragraphStyles + }), + ], + }), + new Paragraph({ + text: `店様付旅母正役基社発破班。発治治京岡局本因追意金紀統責郵滴尽。立功日庁重天富評界明済不着法返祉経正引行。載区列防新目治日群付無人道言越名。界安無死専体風木人教録覧訃。女問堂容県作先芸便効証帯債土全極的日。能山中岡仕関攻井季接店線幌温後率事阜止。成間路行究今式歌像祉帯式媛読徹。安行息古入出文侵監株登部席内文第珍鶴問用。 + + 編竹入俳多応日氏陸福芸北供動。情績拠僧肺軍油能認郷翌南対韓短東食束兵晶。政予任習口佐所当止市告号。悩申無式立医毎部観潟訴菱権。発臼背郎上予配光記芸注然出。梨場株以政囲下球品材県動政押婚面見。米共試使落帳遅毅間三子開。問与大八地芸第線体架辺今死。押構草齢戦重最変社豪記目盗連報準周込。系貸劇様重鷲始能質村異社学動導勤。 + + 残様的荻仲刺局標績供質球就雄。考和母問者役尊紙推新経革個事編安観。益学北日著楽車山勢流泉四犯投台戒設対臨百。危謄初治穴委済本索労刊回。月写政覧女事棋違年終索情響白子泉転企堀社。江遊著西高開面毎分芸責成創査全判調目止懇。原育会夏作売望人転乱融抜心。制川供健水示囲阜厚高右断季人役。持面必日暮気管像冬間影図健行格阪。 + + 来入工速会健評下町慮大貸社一見園外申憶。服豊舞入面沢交使奥見記写意岩。北観提女刊液城共五擦相売田合是由読。回歳売苦定記点郎意増賛治北渡本応。受送文携村陸情静了申注際。順負子研済老上変女産第内無費携投展達。東初乗回動合語学待聴暮沢流全場導敷記賞次。更物中備著後渡売第部時禁新田木下昨。備護起織服久権意全海馬適応。 + + 幸速団供地信討川安矛場消学年。去茂玉東今要出約元教負限載始今簡。編掲証的情仕渡室手映法部始。紙反語清阪曜税受知選謝個印観聞。教人超死準無者参生撤技真価椿景破市見国。左需臓道力趣暮際丁高会近部敗岡力当社。壊態毒期波超長写島断兆国世行共絞私報。反野番点図択女撃脱案情王援。減属考論月院催者門料約覧市戦。 + + 山部午金査投立集争教殺巣作世動北部応。通負考隙問粛中帯閉要程規化小。橋棋互界時引就現省竹去子無系容米。竹転起日本新田強済購書区庁集作容同会窓教。文公神転待究挙登投川選囲針能楽路断出歌。祥新現寄公史真玉属元始員金抜関前百衝能。国眺暮囲世嘘面外営国内報夜九掲事春葉。来月刊台先良理著自供法投。通渡請当月学安首一押実展介況。 + + 法優分独込右得里記域目順供進乗。憂婚転峰大写医投社曲題任能務熱県展覧港専。栄余歯真著改追事作果石芸。青感将南便再転領鈴投提鉄索競虎師体物想。牲打迎達携度開技書催掲安去変念座。革混疑生採就枢当住回県組北意寛。爆新和級掲交象温十賛展木開有結日。新海辺小除京物興量写界裁上。文師建関面取質也底沼画者図空医心無季。 + + 高館湯転名気業以際置講詰方活礎組調軽際。発変東作訪乃小化理利提目気。極季本問号面帯勤戦行新禁情堀郎携。座所転再祥業必変昌今歩佐王立抗後養崩。支物猪躍芸整縦焦供防続相護治時語朝分刊定。綿田幸崎責本欲間載神調崎答志政報与速美載。飯治止稿原先明画森群進見情幕。女民館終調質並伝車慌供科。支田国傷自動献疑討医足公成公主断的望。 + + 責開児食福実帰治師今策今。水重寺圧医観送連東者秒途。選央力律式開作掲写様階組戦写型紙。式国販時天遣国出難共前次領体康稲住転。査見保重議原速群者内月正連。爾天膨装芸別巨平運数準三浜念載創県奈飛提。素京発揮田談気党示見象定電類代級。善返跡国有話権入猛府週亡辞馬脳。関残主祐雪塚去集村完海関文受載表川護照立。 + + 発闘美慎健育旅布容広互無秋認天歌媛芸。転器合読県増認会賞倒点系。食気母服絵知去祝芸車報熱勝。能貿月更障文的欠賞現覇声敏施会。懲病写昼法族業律記聡生開緊楽暮護。東地二員者説盟治害討面提。第北乗査庭年近的禁疑報方店記必迷都流通。聞有力前愛院梨野関業前訳本清滋補。蒲読火死勝広保会婚際気二由保国。用君込村需起相点選紙拡氏訃不。`, }), ], - }), - new Paragraph({ - text: `店様付旅母正役基社発破班。発治治京岡局本因追意金紀統責郵滴尽。立功日庁重天富評界明済不着法返祉経正引行。載区列防新目治日群付無人道言越名。界安無死専体風木人教録覧訃。女問堂容県作先芸便効証帯債土全極的日。能山中岡仕関攻井季接店線幌温後率事阜止。成間路行究今式歌像祉帯式媛読徹。安行息古入出文侵監株登部席内文第珍鶴問用。 - - 編竹入俳多応日氏陸福芸北供動。情績拠僧肺軍油能認郷翌南対韓短東食束兵晶。政予任習口佐所当止市告号。悩申無式立医毎部観潟訴菱権。発臼背郎上予配光記芸注然出。梨場株以政囲下球品材県動政押婚面見。米共試使落帳遅毅間三子開。問与大八地芸第線体架辺今死。押構草齢戦重最変社豪記目盗連報準周込。系貸劇様重鷲始能質村異社学動導勤。 - - 残様的荻仲刺局標績供質球就雄。考和母問者役尊紙推新経革個事編安観。益学北日著楽車山勢流泉四犯投台戒設対臨百。危謄初治穴委済本索労刊回。月写政覧女事棋違年終索情響白子泉転企堀社。江遊著西高開面毎分芸責成創査全判調目止懇。原育会夏作売望人転乱融抜心。制川供健水示囲阜厚高右断季人役。持面必日暮気管像冬間影図健行格阪。 - - 来入工速会健評下町慮大貸社一見園外申憶。服豊舞入面沢交使奥見記写意岩。北観提女刊液城共五擦相売田合是由読。回歳売苦定記点郎意増賛治北渡本応。受送文携村陸情静了申注際。順負子研済老上変女産第内無費携投展達。東初乗回動合語学待聴暮沢流全場導敷記賞次。更物中備著後渡売第部時禁新田木下昨。備護起織服久権意全海馬適応。 - - 幸速団供地信討川安矛場消学年。去茂玉東今要出約元教負限載始今簡。編掲証的情仕渡室手映法部始。紙反語清阪曜税受知選謝個印観聞。教人超死準無者参生撤技真価椿景破市見国。左需臓道力趣暮際丁高会近部敗岡力当社。壊態毒期波超長写島断兆国世行共絞私報。反野番点図択女撃脱案情王援。減属考論月院催者門料約覧市戦。 - - 山部午金査投立集争教殺巣作世動北部応。通負考隙問粛中帯閉要程規化小。橋棋互界時引就現省竹去子無系容米。竹転起日本新田強済購書区庁集作容同会窓教。文公神転待究挙登投川選囲針能楽路断出歌。祥新現寄公史真玉属元始員金抜関前百衝能。国眺暮囲世嘘面外営国内報夜九掲事春葉。来月刊台先良理著自供法投。通渡請当月学安首一押実展介況。 - - 法優分独込右得里記域目順供進乗。憂婚転峰大写医投社曲題任能務熱県展覧港専。栄余歯真著改追事作果石芸。青感将南便再転領鈴投提鉄索競虎師体物想。牲打迎達携度開技書催掲安去変念座。革混疑生採就枢当住回県組北意寛。爆新和級掲交象温十賛展木開有結日。新海辺小除京物興量写界裁上。文師建関面取質也底沼画者図空医心無季。 - - 高館湯転名気業以際置講詰方活礎組調軽際。発変東作訪乃小化理利提目気。極季本問号面帯勤戦行新禁情堀郎携。座所転再祥業必変昌今歩佐王立抗後養崩。支物猪躍芸整縦焦供防続相護治時語朝分刊定。綿田幸崎責本欲間載神調崎答志政報与速美載。飯治止稿原先明画森群進見情幕。女民館終調質並伝車慌供科。支田国傷自動献疑討医足公成公主断的望。 - - 責開児食福実帰治師今策今。水重寺圧医観送連東者秒途。選央力律式開作掲写様階組戦写型紙。式国販時天遣国出難共前次領体康稲住転。査見保重議原速群者内月正連。爾天膨装芸別巨平運数準三浜念載創県奈飛提。素京発揮田談気党示見象定電類代級。善返跡国有話権入猛府週亡辞馬脳。関残主祐雪塚去集村完海関文受載表川護照立。 - - 発闘美慎健育旅布容広互無秋認天歌媛芸。転器合読県増認会賞倒点系。食気母服絵知去祝芸車報熱勝。能貿月更障文的欠賞現覇声敏施会。懲病写昼法族業律記聡生開緊楽暮護。東地二員者説盟治害討面提。第北乗査庭年近的禁疑報方店記必迷都流通。聞有力前愛院梨野関業前訳本清滋補。蒲読火死勝広保会婚際気二由保国。用君込村需起相点選紙拡氏訃不。`, - }), + }, ], }); diff --git a/demo/55-math.ts b/demo/55-math.ts index 2a240986d0..e818d13e3b 100644 --- a/demo/55-math.ts +++ b/demo/55-math.ts @@ -22,270 +22,272 @@ import { TextRun, } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: {}, - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + properties: {}, children: [ - new Math({ + new Paragraph({ children: [ - new MathRun("2+2"), - new MathFraction({ - numerator: [new MathRun("hi")], - denominator: [new MathRun("2")], - }), - ], - }), - new TextRun({ - text: "Foo Bar", - bold: true, - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathFraction({ - numerator: [ - new MathRun("1"), - new MathRadical({ - children: [new MathRun("2")], - }), - ], - denominator: [new MathRun("2")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathSum({ - children: [new MathRun("test")], - }), - new MathSum({ - children: [ - new MathSuperScript({ - children: [new MathRun("e")], - superScript: [new MathRun("2")], - }), - ], - subScript: [new MathRun("i")], - }), - new MathSum({ - children: [ - new MathRadical({ - children: [new MathRun("i")], - }), - ], - subScript: [new MathRun("i")], - superScript: [new MathRun("10")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathSuperScript({ - children: [new MathRun("test")], - superScript: [new MathRun("hello")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathSubScript({ - children: [new MathRun("test")], - subScript: [new MathRun("hello")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathSubScript({ - children: [new MathRun("x")], - subScript: [ - new MathSuperScript({ - children: [new MathRun("y")], - superScript: [new MathRun("2")], - }), - ], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathSubSuperScript({ - children: [new MathRun("test")], - superScript: [new MathRun("hello")], - subScript: [new MathRun("world")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathPreSubSuperScript({ - children: [new MathRun("test")], - superScript: [new MathRun("hello")], - subScript: [new MathRun("world")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathSubScript({ + new Math({ children: [ + new MathRun("2+2"), new MathFraction({ - numerator: [new MathRun("1")], + numerator: [new MathRun("hi")], denominator: [new MathRun("2")], }), ], - subScript: [new MathRun("4")], + }), + new TextRun({ + text: "Foo Bar", + bold: true, }), ], }), - ], - }), - new Paragraph({ - children: [ - new Math({ + new Paragraph({ children: [ - new MathSubScript({ + new Math({ children: [ - new MathRadical({ + new MathFraction({ + numerator: [ + new MathRun("1"), + new MathRadical({ + children: [new MathRun("2")], + }), + ], + denominator: [new MathRun("2")], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSum({ + children: [new MathRun("test")], + }), + new MathSum({ + children: [ + new MathSuperScript({ + children: [new MathRun("e")], + superScript: [new MathRun("2")], + }), + ], + subScript: [new MathRun("i")], + }), + new MathSum({ + children: [ + new MathRadical({ + children: [new MathRun("i")], + }), + ], + subScript: [new MathRun("i")], + superScript: [new MathRun("10")], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSuperScript({ + children: [new MathRun("test")], + superScript: [new MathRun("hello")], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubScript({ + children: [new MathRun("test")], + subScript: [new MathRun("hello")], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubScript({ + children: [new MathRun("x")], + subScript: [ + new MathSuperScript({ + children: [new MathRun("y")], + superScript: [new MathRun("2")], + }), + ], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubSuperScript({ + children: [new MathRun("test")], + superScript: [new MathRun("hello")], + subScript: [new MathRun("world")], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathPreSubSuperScript({ + children: [new MathRun("test")], + superScript: [new MathRun("hello")], + subScript: [new MathRun("world")], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathSubScript({ children: [ new MathFraction({ numerator: [new MathRun("1")], denominator: [new MathRun("2")], }), ], - degree: [new MathRun("4")], - }), - ], - subScript: [new MathRun("x")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathRadical({ - children: [new MathRun("4")], - }), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathFunction({ - name: [ - new MathSuperScript({ - children: [new MathRun("cos")], - superScript: [new MathRun("-1")], - }), - ], - children: [new MathRun("100")], - }), - new MathRun("×"), - new MathFunction({ - name: [new MathRun("sin")], - children: [new MathRun("360")], - }), - new MathRun("= x"), - ], - }), - ], - }), - new Paragraph({ - children: [ - new Math({ - children: [ - new MathRoundBrackets({ - children: [ - new MathFraction({ - numerator: [new MathRun("1")], - denominator: [new MathRun("2")], - }), - ], - }), - new MathSquareBrackets({ - children: [ - new MathFraction({ - numerator: [new MathRun("1")], - denominator: [new MathRun("2")], - }), - ], - }), - new MathCurlyBrackets({ - children: [ - new MathFraction({ - numerator: [new MathRun("1")], - denominator: [new MathRun("2")], - }), - ], - }), - new MathAngledBrackets({ - children: [ - new MathFraction({ - numerator: [new MathRun("1")], - denominator: [new MathRun("2")], + subScript: [new MathRun("4")], }), ], }), ], }), - ], - }), - new Paragraph({ - children: [ - new Math({ + new Paragraph({ children: [ - new MathFraction({ - numerator: [ + new Math({ + children: [ + new MathSubScript({ + children: [ + new MathRadical({ + children: [ + new MathFraction({ + numerator: [new MathRun("1")], + denominator: [new MathRun("2")], + }), + ], + degree: [new MathRun("4")], + }), + ], + subScript: [new MathRun("x")], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ new MathRadical({ children: [new MathRun("4")], }), ], - denominator: [new MathRun("2a")], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathFunction({ + name: [ + new MathSuperScript({ + children: [new MathRun("cos")], + superScript: [new MathRun("-1")], + }), + ], + children: [new MathRun("100")], + }), + new MathRun("×"), + new MathFunction({ + name: [new MathRun("sin")], + children: [new MathRun("360")], + }), + new MathRun("= x"), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathRoundBrackets({ + children: [ + new MathFraction({ + numerator: [new MathRun("1")], + denominator: [new MathRun("2")], + }), + ], + }), + new MathSquareBrackets({ + children: [ + new MathFraction({ + numerator: [new MathRun("1")], + denominator: [new MathRun("2")], + }), + ], + }), + new MathCurlyBrackets({ + children: [ + new MathFraction({ + numerator: [new MathRun("1")], + denominator: [new MathRun("2")], + }), + ], + }), + new MathAngledBrackets({ + children: [ + new MathFraction({ + numerator: [new MathRun("1")], + denominator: [new MathRun("2")], + }), + ], + }), + ], + }), + ], + }), + new Paragraph({ + children: [ + new Math({ + children: [ + new MathFraction({ + numerator: [ + new MathRadical({ + children: [new MathRun("4")], + }), + ], + denominator: [new MathRun("2a")], + }), + ], }), ], }), ], - }), + }, ], }); diff --git a/demo/56-background-color.ts b/demo/56-background-color.ts index 6e8658fd00..23eb077cfd 100644 --- a/demo/56-background-color.ts +++ b/demo/56-background-color.ts @@ -7,24 +7,25 @@ const doc = new Document({ background: { color: "C45911", }, -}); - -doc.addSection({ - properties: {}, - children: [ - new Paragraph({ + sections: [ + { + properties: {}, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, - }), - new TextRun({ - text: "\tGithub is the best", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + new TextRun({ + text: "\tGithub is the best", + bold: true, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/57-add-parent-numbered-lists.ts b/demo/57-add-parent-numbered-lists.ts index e35cd06a27..4287de886e 100644 --- a/demo/57-add-parent-numbered-lists.ts +++ b/demo/57-add-parent-numbered-lists.ts @@ -40,46 +40,47 @@ const doc = new Document({ }, ], }, -}); - -doc.addSection({ - children: [ - new Paragraph({ - text: "How to make cake", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph({ - text: "Step 1 - Add sugar", - numbering: { - reference: "my-number-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "Step 2 - Add wheat", - numbering: { - reference: "my-number-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "Step 2a - Stir the wheat in a circle", - numbering: { - reference: "my-number-numbering-reference", - level: 1, - }, - }), - new Paragraph({ - text: "Step 3 - Put in oven", - numbering: { - reference: "my-number-numbering-reference", - level: 0, - }, - }), - new Paragraph({ - text: "How to make cake", - heading: HeadingLevel.HEADING_1, - }), + sections: [ + { + children: [ + new Paragraph({ + text: "How to make cake", + heading: HeadingLevel.HEADING_1, + }), + new Paragraph({ + text: "Step 1 - Add sugar", + numbering: { + reference: "my-number-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "Step 2 - Add wheat", + numbering: { + reference: "my-number-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "Step 2a - Stir the wheat in a circle", + numbering: { + reference: "my-number-numbering-reference", + level: 1, + }, + }), + new Paragraph({ + text: "Step 3 - Put in oven", + numbering: { + reference: "my-number-numbering-reference", + level: 0, + }, + }), + new Paragraph({ + text: "How to make cake", + heading: HeadingLevel.HEADING_1, + }), + ], + }, ], }); diff --git a/demo/58-section-types.ts b/demo/58-section-types.ts index 6c16b44600..011ec9a0b8 100644 --- a/demo/58-section-types.ts +++ b/demo/58-section-types.ts @@ -3,88 +3,86 @@ import * as fs from "fs"; import { Document, Packer, Paragraph, TextRun, SectionType } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: {}, - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + properties: {}, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + ], }), ], - }), - ], -}); - -doc.addSection({ - properties: { - type: SectionType.CONTINUOUS, - }, - children: [ - new Paragraph({ + }, + { + properties: { + type: SectionType.CONTINUOUS, + }, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + ], }), ], - }), - ], -}); - -doc.addSection({ - properties: { - type: SectionType.ODD_PAGE, - }, - children: [ - new Paragraph({ + }, + { + properties: { + type: SectionType.ODD_PAGE, + }, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + ], }), ], - }), - ], -}); - -doc.addSection({ - properties: { - type: SectionType.EVEN_PAGE, - }, - children: [ - new Paragraph({ + }, + { + properties: { + type: SectionType.EVEN_PAGE, + }, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + ], }), ], - }), - ], -}); - -doc.addSection({ - properties: { - type: SectionType.NEXT_PAGE, - }, - children: [ - new Paragraph({ + }, + { + properties: { + type: SectionType.NEXT_PAGE, + }, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/59-header-footer-margins.ts b/demo/59-header-footer-margins.ts index 35b1992fc5..d5807b680f 100644 --- a/demo/59-header-footer-margins.ts +++ b/demo/59-header-footer-margins.ts @@ -3,44 +3,46 @@ import * as fs from "fs"; import { Document, Footer, Header, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: { - header: 100, - footer: 50, - }, - headers: { - default: new Header({ - children: [ - new Paragraph({ - text: "Header text", - indent: { - left: -400, - }, +const doc = new Document({ + sections: [ + { + properties: { + header: 100, + footer: 50, + }, + headers: { + default: new Header({ + children: [ + new Paragraph({ + text: "Header text", + indent: { + left: -400, + }, + }), + new Paragraph({ + text: "Some more header text", + indent: { + left: -600, + }, + }), + ], }), - new Paragraph({ - text: "Some more header text", - indent: { - left: -600, - }, + }, + footers: { + default: new Footer({ + children: [ + new Paragraph({ + text: "Footer text", + indent: { + left: -400, + }, + }), + ], }), - ], - }), - }, - footers: { - default: new Footer({ - children: [ - new Paragraph({ - text: "Footer text", - indent: { - left: -400, - }, - }), - ], - }), - }, - children: [new Paragraph("Hello World")], + }, + children: [new Paragraph("Hello World")], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/6-page-borders.ts b/demo/6-page-borders.ts index e83f18233b..999c5011e5 100644 --- a/demo/6-page-borders.ts +++ b/demo/6-page-borders.ts @@ -3,35 +3,41 @@ import * as fs from "fs"; import { Document, HeadingLevel, Packer, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - margins: { - top: 0, - right: 0, - bottom: 0, - left: 0, - }, - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + properties: { + page: { + margin: { + top: 0, + right: 0, + bottom: 0, + left: 0, + }, + }, + }, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo bar", - bold: true, + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo bar", + bold: true, + }), + new TextRun({ + text: "\tGithub is the best", + bold: true, + }), + ], }), - new TextRun({ - text: "\tGithub is the best", - bold: true, + new Paragraph({ + text: "Hello World", + heading: HeadingLevel.HEADING_1, }), + new Paragraph("Foo bar"), + new Paragraph("Github is the best"), ], - }), - new Paragraph({ - text: "Hello World", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph("Foo bar"), - new Paragraph("Github is the best"), + }, ], }); diff --git a/demo/60-track-revisions.ts b/demo/60-track-revisions.ts index 5ed136e27e..81a68d4f7d 100644 --- a/demo/60-track-revisions.ts +++ b/demo/60-track-revisions.ts @@ -26,6 +26,27 @@ import { Note that this setting enables to track *new changes* after teh file is generated, so this example will still show inserted and deleted text runs when you remove it. */ +const paragraph = new Paragraph({ + children: [ + new TextRun("This is a simple demo "), + new TextRun({ + text: "on how to ", + }), + new InsertedTextRun({ + text: "mark a text as an insertion ", + id: 0, + author: "Firstname Lastname", + date: "2020-10-06T09:00:00Z", + }), + new DeletedTextRun({ + text: "or a deletion.", + id: 1, + author: "Firstname Lastname", + date: "2020-10-06T09:00:00Z", + }), + ], +}); + const doc = new Document({ footnotes: { 1: { @@ -53,96 +74,76 @@ const doc = new Document({ features: { trackRevisions: true, }, -}); - -const paragraph = new Paragraph({ - children: [ - new TextRun("This is a simple demo "), - new TextRun({ - text: "on how to ", - }), - new InsertedTextRun({ - text: "mark a text as an insertion ", - id: 0, - author: "Firstname Lastname", - date: "2020-10-06T09:00:00Z", - }), - new DeletedTextRun({ - text: "or a deletion.", - id: 1, - author: "Firstname Lastname", - date: "2020-10-06T09:00:00Z", - }), - ], -}); - -doc.addSection({ - properties: {}, - children: [ - paragraph, - new Paragraph({ - children: [ - new TextRun("This is a demo "), - new DeletedTextRun({ - break: 1, - text: "in order", - color: "red", - bold: true, - size: 24, - font: { - name: "Garamond", - }, - shading: { - type: ShadingType.REVERSE_DIAGONAL_STRIPE, - color: "00FFFF", - fill: "FF0000", - }, - id: 2, - author: "Firstname Lastname", - date: "2020-10-06T09:00:00Z", - }), - new InsertedTextRun({ - text: "to show how to ", - bold: false, - id: 3, - author: "Firstname Lastname", - date: "2020-10-06T09:05:00Z", - }), - new TextRun({ - bold: true, - children: ["\tuse Inserted and Deleted TextRuns.", new FootnoteReferenceRun(1)], - }), - ], - }), - ], - footers: { - default: new Footer({ + sections: [ + { + properties: {}, children: [ + paragraph, new Paragraph({ - alignment: AlignmentType.CENTER, children: [ - new TextRun("Awesome LLC"), - new TextRun({ - children: ["Page Number: ", PageNumber.CURRENT], - }), + new TextRun("This is a demo "), new DeletedTextRun({ - children: [" to ", PageNumber.TOTAL_PAGES], - id: 4, + break: 1, + text: "in order", + color: "red", + bold: true, + size: 24, + font: { + name: "Garamond", + }, + shading: { + type: ShadingType.REVERSE_DIAGONAL_STRIPE, + color: "00FFFF", + fill: "FF0000", + }, + id: 2, author: "Firstname Lastname", - date: "2020-10-06T09:05:00Z", + date: "2020-10-06T09:00:00Z", }), new InsertedTextRun({ - children: [" from ", PageNumber.TOTAL_PAGES], - bold: true, - id: 5, + text: "to show how to ", + bold: false, + id: 3, author: "Firstname Lastname", date: "2020-10-06T09:05:00Z", }), + new TextRun({ + bold: true, + children: ["\tuse Inserted and Deleted TextRuns.", new FootnoteReferenceRun(1)], + }), ], }), ], - }), - }, + footers: { + default: new Footer({ + children: [ + new Paragraph({ + alignment: AlignmentType.CENTER, + children: [ + new TextRun("Awesome LLC"), + new TextRun({ + children: ["Page Number: ", PageNumber.CURRENT], + }), + new DeletedTextRun({ + children: [" to ", PageNumber.TOTAL_PAGES], + id: 4, + author: "Firstname Lastname", + date: "2020-10-06T09:05:00Z", + }), + new InsertedTextRun({ + children: [" from ", PageNumber.TOTAL_PAGES], + bold: true, + id: 5, + author: "Firstname Lastname", + date: "2020-10-06T09:05:00Z", + }), + ], + }), + ], + }), + }, + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/61-text-frame.ts b/demo/61-text-frame.ts index 0759a4e6c7..e10e3b6f3c 100644 --- a/demo/61-text-frame.ts +++ b/demo/61-text-frame.ts @@ -3,66 +3,68 @@ import * as fs from "fs"; import { Document, FrameAnchorType, HorizontalPositionAlign, Packer, Paragraph, TextRun, VerticalPositionAlign } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: {}, - children: [ - new Paragraph({ - frame: { - position: { - x: 1000, - y: 3000, - }, - width: 4000, - height: 1000, - anchor: { - horizontal: FrameAnchorType.MARGIN, - vertical: FrameAnchorType.MARGIN, - }, - alignment: { - x: HorizontalPositionAlign.CENTER, - y: VerticalPositionAlign.TOP, - }, - }, - border: { - top: { - color: "auto", - space: 1, - value: "single", - size: 6, - }, - bottom: { - color: "auto", - space: 1, - value: "single", - size: 6, - }, - left: { - color: "auto", - space: 1, - value: "single", - size: 6, - }, - right: { - color: "auto", - space: 1, - value: "single", - size: 6, - }, - }, +const doc = new Document({ + sections: [ + { + properties: {}, children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, - }), - new TextRun({ - text: "\tGithub is the best", - bold: true, + new Paragraph({ + frame: { + position: { + x: 1000, + y: 3000, + }, + width: 4000, + height: 1000, + anchor: { + horizontal: FrameAnchorType.MARGIN, + vertical: FrameAnchorType.MARGIN, + }, + alignment: { + x: HorizontalPositionAlign.CENTER, + y: VerticalPositionAlign.TOP, + }, + }, + border: { + top: { + color: "auto", + space: 1, + value: "single", + size: 6, + }, + bottom: { + color: "auto", + space: 1, + value: "single", + size: 6, + }, + left: { + color: "auto", + space: 1, + value: "single", + size: 6, + }, + right: { + color: "auto", + space: 1, + value: "single", + size: 6, + }, + }, + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + new TextRun({ + text: "\tGithub is the best", + bold: true, + }), + ], }), ], - }), + }, ], }); diff --git a/demo/62-paragraph-spacing.ts b/demo/62-paragraph-spacing.ts index 0322a1729e..9d17a1be30 100644 --- a/demo/62-paragraph-spacing.ts +++ b/demo/62-paragraph-spacing.ts @@ -3,29 +3,31 @@ import * as fs from "fs"; import { Document, LineRuleType, Packer, Paragraph, TextRun } from "../build"; -const doc = new Document(); - -doc.addSection({ - properties: {}, - children: [ - new Paragraph({ - spacing: { - after: 5000, - before: 5000, - }, - children: [new TextRun("Hello World")], - }), - new Paragraph({ - spacing: { - line: 2000, - lineRule: LineRuleType.AUTO, - }, +const doc = new Document({ + sections: [ + { + properties: {}, children: [ - new TextRun( - "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis ex, aliquet et faucibus quis, euismod in odio. Fusce gravida tempor nunc sed lacinia. Nulla sed dolor fringilla, fermentum libero ut, egestas ex. Donec pellentesque metus non orci lacinia bibendum. Cras porta ex et mollis hendrerit. Suspendisse id lectus suscipit, elementum lacus eu, convallis felis. Fusce sed bibendum dolor, id posuere ligula. Aliquam eu elit ut urna eleifend vestibulum. Praesent condimentum at turpis sed scelerisque. Suspendisse porttitor metus nec vestibulum egestas. Sed in eros sapien. Morbi efficitur placerat est a consequat. Nunc bibendum porttitor mi nec tempus. Morbi dictum augue porttitor nisi sodales sodales.", - ), + new Paragraph({ + spacing: { + after: 5000, + before: 5000, + }, + children: [new TextRun("Hello World")], + }), + new Paragraph({ + spacing: { + line: 2000, + lineRule: LineRuleType.AUTO, + }, + children: [ + new TextRun( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis ex, aliquet et faucibus quis, euismod in odio. Fusce gravida tempor nunc sed lacinia. Nulla sed dolor fringilla, fermentum libero ut, egestas ex. Donec pellentesque metus non orci lacinia bibendum. Cras porta ex et mollis hendrerit. Suspendisse id lectus suscipit, elementum lacus eu, convallis felis. Fusce sed bibendum dolor, id posuere ligula. Aliquam eu elit ut urna eleifend vestibulum. Praesent condimentum at turpis sed scelerisque. Suspendisse porttitor metus nec vestibulum egestas. Sed in eros sapien. Morbi efficitur placerat est a consequat. Nunc bibendum porttitor mi nec tempus. Morbi dictum augue porttitor nisi sodales sodales.", + ), + ], + }), ], - }), + }, ], }); diff --git a/demo/63-odd-even-header-footer.ts b/demo/63-odd-even-header-footer.ts index 4582e14455..c2e7723922 100644 --- a/demo/63-odd-even-header-footer.ts +++ b/demo/63-odd-even-header-footer.ts @@ -5,63 +5,64 @@ import { Document, Footer, Header, Packer, PageBreak, Paragraph, TextRun } from const doc = new Document({ evenAndOddHeaderAndFooters: true, -}); - -doc.addSection({ - headers: { - default: new Header({ + sections: [ + { + headers: { + default: new Header({ + children: [ + new Paragraph({ + text: "Odd Header text", + }), + new Paragraph({ + text: "Odd - Some more header text", + }), + ], + }), + even: new Header({ + children: [ + new Paragraph({ + text: "Even header text", + }), + new Paragraph({ + text: "Even - Some more header text", + }), + ], + }), + }, + footers: { + default: new Footer({ + children: [ + new Paragraph({ + text: "Odd Footer text", + }), + ], + }), + even: new Footer({ + children: [ + new Paragraph({ + text: "Even Cool Footer text", + }), + ], + }), + }, children: [ new Paragraph({ - text: "Odd Header text", + children: [new TextRun("Hello World 1"), new PageBreak()], }), new Paragraph({ - text: "Odd - Some more header text", + children: [new TextRun("Hello World 2"), new PageBreak()], + }), + new Paragraph({ + children: [new TextRun("Hello World 3"), new PageBreak()], + }), + new Paragraph({ + children: [new TextRun("Hello World 4"), new PageBreak()], + }), + new Paragraph({ + children: [new TextRun("Hello World 5"), new PageBreak()], }), ], - }), - even: new Header({ - children: [ - new Paragraph({ - text: "Even header text", - }), - new Paragraph({ - text: "Even - Some more header text", - }), - ], - }), - }, - footers: { - default: new Footer({ - children: [ - new Paragraph({ - text: "Odd Footer text", - }), - ], - }), - even: new Footer({ - children: [ - new Paragraph({ - text: "Even Cool Footer text", - }), - ], - }), - }, - children: [ - new Paragraph({ - children: [new TextRun("Hello World 1"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 2"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 3"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 4"), new PageBreak()], - }), - new Paragraph({ - children: [new TextRun("Hello World 5"), new PageBreak()], - }), + }, ], }); diff --git a/demo/7-landscape.ts b/demo/7-landscape.ts index fa3e551728..253b4eec5d 100644 --- a/demo/7-landscape.ts +++ b/demo/7-landscape.ts @@ -3,13 +3,19 @@ import * as fs from "fs"; import { Document, Packer, PageOrientation, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - size: { - orientation: PageOrientation.LANDSCAPE, - }, - children: [new Paragraph("Hello World")], +const doc = new Document({ + sections: [ + { + properties: { + page: { + size: { + orientation: PageOrientation.LANDSCAPE, + }, + }, + }, + children: [new Paragraph("Hello World")], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/8-header-footer.ts b/demo/8-header-footer.ts index 6455d94390..a546aa9754 100644 --- a/demo/8-header-footer.ts +++ b/demo/8-header-footer.ts @@ -3,20 +3,22 @@ import * as fs from "fs"; import { Document, Footer, Header, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - headers: { - default: new Header({ - children: [new Paragraph("Header text")], - }), - }, - footers: { - default: new Footer({ - children: [new Paragraph("Footer text")], - }), - }, - children: [new Paragraph("Hello World")], +const doc = new Document({ + sections: [ + { + headers: { + default: new Header({ + children: [new Paragraph("Header text")], + }), + }, + footers: { + default: new Footer({ + children: [new Paragraph("Footer text")], + }), + }, + children: [new Paragraph("Hello World")], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/9-images-in-header-and-footer.ts b/demo/9-images-in-header-and-footer.ts index 4256956e19..c435d38060 100644 --- a/demo/9-images-in-header-and-footer.ts +++ b/demo/9-images-in-header-and-footer.ts @@ -3,44 +3,46 @@ import * as fs from "fs"; import { Document, Footer, Header, ImageRun, Packer, Paragraph } from "../build"; -const doc = new Document(); - -doc.addSection({ - headers: { - default: new Header({ - children: [ - new Paragraph({ +const doc = new Document({ + sections: [ + { + headers: { + default: new Header({ children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 100, - height: 100, - }, + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 100, + height: 100, + }, + }), + ], }), ], }), - ], - }), - }, - footers: { - default: new Footer({ - children: [ - new Paragraph({ + }, + footers: { + default: new Footer({ children: [ - new ImageRun({ - data: fs.readFileSync("./demo/images/pizza.gif"), - transformation: { - width: 100, - height: 100, - }, + new Paragraph({ + children: [ + new ImageRun({ + data: fs.readFileSync("./demo/images/pizza.gif"), + transformation: { + width: 100, + height: 100, + }, + }), + ], }), ], }), - ], - }), - }, - children: [new Paragraph("Hello World")], + }, + children: [new Paragraph("Hello World")], + }, + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/browser-demo.html b/demo/browser-demo.html index ecd7f47701..4ad88c3d73 100644 --- a/demo/browser-demo.html +++ b/demo/browser-demo.html @@ -14,26 +14,28 @@ function generate() { const doc = new docx.Document(); - doc.addSection({ - children: [ - new docx.Paragraph({ + const doc = new Document({ + sections: [ + { children: [ - new docx.TextRun("Hello World"), - new docx.TextRun({ - text: "Foo Bar", - bold: true, - }), - new docx.TextRun({ - text: "\tGithub is the best", - bold: true, + new docx.Paragraph({ + children: [ + new docx.TextRun("Hello World"), + new docx.TextRun({ + text: "Foo Bar", + bold: true, + }), + new docx.TextRun({ + text: "\tGithub is the best", + bold: true, + }), + ], }), ], - }), + }, ], }); - - docx.Packer.toBlob(doc).then((blob) => { console.log(blob); saveAs(blob, "example.docx"); diff --git a/docs/README.md b/docs/README.md index 9d2150a441..64a6a85225 100644 --- a/docs/README.md +++ b/docs/README.md @@ -24,28 +24,27 @@ import { ... } from "docx"; import * as fs from "fs"; import { Document, Packer, Paragraph, TextRun } from "docx"; -// Create document -const doc = new Document(); - // Documents contain sections, you can have multiple sections per document, go here to learn more about sections // This simple example will only contain one section -doc.addSection({ - properties: {}, - children: [ - new Paragraph({ - children: [ - new TextRun("Hello World"), - new TextRun({ - text: "Foo Bar", - bold: true, - }), - new TextRun({ - text: "\tGithub is the best", - bold: true, - }), - ], - }), - ], +const doc = new Document({ + sections: [{ + properties: {}, + children: [ + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ + text: "Foo Bar", + bold: true, + }), + new TextRun({ + text: "\tGithub is the best", + bold: true, + }), + ], + }), + ], + }]; }); // Used to export the file into a .docx file diff --git a/docs/contribution-guidelines.md b/docs/contribution-guidelines.md index f9963bd644..cec4f4a80b 100644 --- a/docs/contribution-guidelines.md +++ b/docs/contribution-guidelines.md @@ -57,12 +57,14 @@ text.contents = "Hello World"; **Do** ```ts -doc.addSection({ - children: [ - new Paragraph({ - children: [new TextRun("Hello World")], - }), - ], +const doc = new Document({ + sections: [{ + children: [ + new Paragraph({ + children: [new TextRun("Hello World")], + }), + ], + }]; }); ``` diff --git a/docs/usage/bullet-points.md b/docs/usage/bullet-points.md index cf2c5bda54..102df87673 100644 --- a/docs/usage/bullet-points.md +++ b/docs/usage/bullet-points.md @@ -6,21 +6,23 @@ To make a bullet point, simply make a paragraph into a bullet point: ```ts -doc.addSection({ - children: [ - new Paragraph({ - text: "Bullet points", - bullet: { - level: 0 //How deep you want the bullet to be - } - }), - new Paragraph({ - text: "Are awesome", - bullet: { - level: 0 - } - }) - ], +const doc = new Document({ + sections: [{ + children: [ + new Paragraph({ + text: "Bullet points", + bullet: { + level: 0 //How deep you want the bullet to be + } + }), + new Paragraph({ + text: "Are awesome", + bullet: { + level: 0 + } + }) + ], + }]; }); ``` diff --git a/docs/usage/headers-and-footers.md b/docs/usage/headers-and-footers.md index 2bae4b4c71..acf3e6caa0 100644 --- a/docs/usage/headers-and-footers.md +++ b/docs/usage/headers-and-footers.md @@ -5,30 +5,32 @@ Every Section has a sections which you can define its Headers and Footers: ```ts -doc.addSection({ - headers: { - default: new Header({ // The standard default header - children: [], - }), - first: new Header({ // The first header - children: [], - }), - even: new Header({ // The header on every other page - children: [], - }), - }, - footers: { - default: new Footer({ // The standard default footer - children: [], - }), - first: new Footer({ // The first footer - children: [], - }), - even: new Footer({ // The footer on every other page - children: [], - }), - }, - children: [], +const doc = new Document({ + sections: [{ + headers: { + default: new Header({ // The standard default header + children: [], + }), + first: new Header({ // The first header + children: [], + }), + even: new Header({ // The header on every other page + children: [], + }), + }, + footers: { + default: new Footer({ // The standard default footer + children: [], + }), + first: new Footer({ // The first footer + children: [], + }), + even: new Footer({ // The footer on every other page + children: [], + }), + }, + children: [], + }]; }); ``` diff --git a/docs/usage/images.md b/docs/usage/images.md index 2b098d073b..8b3a6d6353 100644 --- a/docs/usage/images.md +++ b/docs/usage/images.md @@ -37,20 +37,14 @@ const image = new ImageRun({ Add it into the document by adding the image into a paragraph: ```ts -doc.addSection({ - children: [new Paragraph(image)], -}); -``` - -Or: - -```ts -doc.addSection({ - children: [ - new Paragraph({ - children: [image], - }), - ], +const doc = new Document({ + sections: [{ + children: [ + new Paragraph({ + children: [image], + }), + ], + }]; }); ``` @@ -59,16 +53,22 @@ doc.addSection({ Adding images can be easily done by creating an instance of `ImageRun`. This can be added in a `Paragraph` or `Hyperlink`: ```ts -const image = new ImageRun({ - data: [IMAGE_BUFFER], - transformation: { - width: [IMAGE_SIZE], - height: [IMAGE_SIZE], - }, -}); - -doc.addSection({ - children: [new Paragraph(image)], +const doc = new Document({ + sections: [{ + children: [ + new Paragraph({ + children: [ + new ImageRun({ + data: [IMAGE_BUFFER], + transformation: { + width: [IMAGE_SIZE], + height: [IMAGE_SIZE], + }, + }), + ], + }), + ], + }]; }); ``` diff --git a/docs/usage/paragraph.md b/docs/usage/paragraph.md index e0b4e56599..9b846a94d9 100644 --- a/docs/usage/paragraph.md +++ b/docs/usage/paragraph.md @@ -35,20 +35,24 @@ const paragraph = new Paragraph({ After you create the paragraph, you must add the paragraph into a `section`: ```ts -doc.addSection({ - children: [paragraph], +const doc = new Document({ + sections: [{ + children: [paragraph], + }]; }); ``` Or the preferred convension, define the paragraph inside the section and remove the usage of variables: ```ts -doc.addSection({ - children: [ - new Paragraph({ - children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World")], - }), - ], +const doc = new Document({ + sections: [{ + children: [ + new Paragraph({ + children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World")], + }), + ], + }]; }); ``` diff --git a/docs/usage/sections.md b/docs/usage/sections.md index daabb02294..79c8f4c27a 100644 --- a/docs/usage/sections.md +++ b/docs/usage/sections.md @@ -11,12 +11,14 @@ For example, you could have one section which is portrait with a header and foot This creates a simple section in a document with one paragraph inside: ```ts -doc.addSection({ - children: [ - new Paragraph({ - children: [new TextRun("Hello World")], - }), - ], +const doc = new Document({ + sections: [{ + children: [ + new Paragraph({ + children: [new TextRun("Hello World")], + }), + ], + }]; }); ``` @@ -35,14 +37,16 @@ Setting the section type determines how the contents of the section will be plac - `ODD_PAGE` ```ts -doc.addSection({ - properties: { - type: SectionType.CONTINUOUS, - } - children: [ - new Paragraph({ - children: [new TextRun("Hello World")], - }), - ], +const doc = new Document({ + sections: [{ + properties: { + type: SectionType.CONTINUOUS, + } + children: [ + new Paragraph({ + children: [new TextRun("Hello World")], + }), + ], + }]; }); ``` diff --git a/docs/usage/tables.md b/docs/usage/tables.md index 16aa8d4f7b..c3259d8e13 100644 --- a/docs/usage/tables.md +++ b/docs/usage/tables.md @@ -19,8 +19,10 @@ const table = new Table({ Then add the table in the `section` ```ts -doc.addSection({ - children: [table], +const doc = new Document({ + sections: [{ + children: [table], + }]; }); ``` diff --git a/src/export/packer/image-replacer.spec.ts b/src/export/packer/image-replacer.spec.ts index 0c503d9f4b..b71c5a028f 100644 --- a/src/export/packer/image-replacer.spec.ts +++ b/src/export/packer/image-replacer.spec.ts @@ -1,38 +1,10 @@ import { expect } from "chai"; -import { File, HeadingLevel, Media, Paragraph } from "file"; +import { Media } from "file"; import { ImageReplacer } from "./image-replacer"; describe("ImageReplacer", () => { - let file: File; - - beforeEach(() => { - file = new File({ - creator: "Dolan Miu", - revision: "1", - lastModifiedBy: "Dolan Miu", - }); - - file.addSection({ - children: [ - new Paragraph({ - text: "title", - heading: HeadingLevel.TITLE, - }), - new Paragraph({ - text: "Hello world", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph({ - text: "heading 2", - heading: HeadingLevel.HEADING_2, - }), - new Paragraph("test text"), - ], - }); - }); - describe("#replace()", () => { it("should replace properly", () => { const imageReplacer = new ImageReplacer(); diff --git a/src/export/packer/next-compiler.spec.ts b/src/export/packer/next-compiler.spec.ts index d0dfbf08ae..df81e057c4 100644 --- a/src/export/packer/next-compiler.spec.ts +++ b/src/export/packer/next-compiler.spec.ts @@ -8,16 +8,17 @@ import { Compiler } from "./next-compiler"; describe("Compiler", () => { let compiler: Compiler; - let file: File; beforeEach(() => { - file = new File(); compiler = new Compiler(); }); describe("#compile()", () => { it("should pack all the content", async function () { this.timeout(99999999); + const file = new File({ + sections: [], + }); const zipFile = compiler.compile(file); const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name); @@ -38,24 +39,27 @@ describe("Compiler", () => { }); it("should pack all additional headers and footers", async function () { - file.addSection({ - headers: { - default: new Header(), - }, - footers: { - default: new Footer(), - }, - children: [], - }); - - file.addSection({ - headers: { - default: new Header(), - }, - footers: { - default: new Footer(), - }, - children: [], + const file = new File({ + sections: [ + { + headers: { + default: new Header(), + }, + footers: { + default: new Footer(), + }, + children: [], + }, + { + headers: { + default: new Header(), + }, + footers: { + default: new Footer(), + }, + children: [], + }, + ], }); this.timeout(99999999); @@ -80,12 +84,15 @@ describe("Compiler", () => { // This test is required because before, there was a case where Document was formatted twice, which was inefficient // This also caused issues such as running prepForXml multiple times as format() was ran multiple times. const paragraph = new Paragraph(""); - const doc = new File(); - - doc.addSection({ - properties: {}, - children: [paragraph], + const file = new File({ + sections: [ + { + properties: {}, + children: [paragraph], + }, + ], }); + // tslint:disable-next-line: no-string-literal const spy = sinon.spy(compiler["formatter"], "format"); diff --git a/src/export/packer/packer.spec.ts b/src/export/packer/packer.spec.ts index e40c9cb5ea..b7eed87187 100644 --- a/src/export/packer/packer.spec.ts +++ b/src/export/packer/packer.spec.ts @@ -14,23 +14,24 @@ describe("Packer", () => { creator: "Dolan Miu", revision: "1", lastModifiedBy: "Dolan Miu", - }); - - file.addSection({ - children: [ - new Paragraph({ - text: "title", - heading: HeadingLevel.TITLE, - }), - new Paragraph({ - text: "Hello world", - heading: HeadingLevel.HEADING_1, - }), - new Paragraph({ - text: "heading 2", - heading: HeadingLevel.HEADING_2, - }), - new Paragraph("test text"), + sections: [ + { + children: [ + new Paragraph({ + text: "title", + heading: HeadingLevel.TITLE, + }), + new Paragraph({ + text: "Hello world", + heading: HeadingLevel.HEADING_1, + }), + new Paragraph({ + text: "heading 2", + heading: HeadingLevel.HEADING_2, + }), + new Paragraph("test text"), + ], + }, ], }); }); diff --git a/src/file/core-properties/properties.ts b/src/file/core-properties/properties.ts index 91ced5bb53..3128b08517 100644 --- a/src/file/core-properties/properties.ts +++ b/src/file/core-properties/properties.ts @@ -3,12 +3,14 @@ import { ICustomPropertyOptions } from "../custom-properties"; import { IDocumentBackgroundOptions } from "../document"; import { DocumentAttributes } from "../document/document-attributes"; +import { ISectionOptions } from "../file"; import { INumberingOptions } from "../numbering"; import { Paragraph } from "../paragraph"; import { IStylesOptions } from "../styles"; import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components"; export interface IPropertiesOptions { + readonly sections: ISectionOptions[]; readonly title?: string; readonly subject?: string; readonly creator?: string; @@ -34,7 +36,7 @@ export interface IPropertiesOptions { } export class CoreProperties extends XmlComponent { - constructor(options: IPropertiesOptions) { + constructor(options: Omit) { super("cp:coreProperties"); this.root.push( new DocumentAttributes({ diff --git a/src/file/document/body/body.spec.ts b/src/file/document/body/body.spec.ts index d9e536242b..63f7cf03ba 100644 --- a/src/file/document/body/body.spec.ts +++ b/src/file/document/body/body.spec.ts @@ -14,8 +14,12 @@ describe("Body", () => { describe("#addSection", () => { it("should add section with default parameters", () => { body.addSection({ - width: 10000, - height: 10000, + page: { + size: { + width: 10000, + height: 10000, + }, + }, }); const tree = new Formatter().format(body); diff --git a/src/file/document/body/body.ts b/src/file/document/body/body.ts index 6b965ab268..ff56962d12 100644 --- a/src/file/document/body/body.ts +++ b/src/file/document/body/body.ts @@ -1,7 +1,7 @@ import { IContext, IXmlableObject, XmlComponent } from "file/xml-components"; import { Paragraph, ParagraphProperties, TableOfContents } from "../.."; -import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties"; +import { ISectionPropertiesOptions, SectionProperties } from "./section-properties/section-properties"; export class Body extends XmlComponent { private readonly sections: SectionProperties[] = []; @@ -18,7 +18,7 @@ export class Body extends XmlComponent { * - last section should be direct child of body * @param options new section options */ - public addSection(options: SectionPropertiesOptions): void { + public addSection(options: ISectionPropertiesOptions): void { const currentSection = this.sections.pop() as SectionProperties; this.root.push(this.createSectionParagraph(currentSection)); diff --git a/src/file/document/body/section-properties/line-number/line-number.ts b/src/file/document/body/section-properties/line-number/line-number.ts index 86e3fbb8a2..fdc0515244 100644 --- a/src/file/document/body/section-properties/line-number/line-number.ts +++ b/src/file/document/body/section-properties/line-number/line-number.ts @@ -8,18 +8,18 @@ export enum LineNumberRestartFormat { } export interface ILineNumberAttributes { - readonly lineNumberCountBy?: number; - readonly lineNumberStart?: number; - readonly lineNumberRestart?: LineNumberRestartFormat; - readonly lineNumberDistance?: number; + readonly countBy?: number; + readonly start?: number; + readonly restart?: LineNumberRestartFormat; + readonly distance?: number; } export class LineNumberAttributes extends XmlAttributeComponent { protected readonly xmlKeys = { - lineNumberCountBy: "w:countBy", - lineNumberStart: "w:start", - lineNumberRestart: "w:restart", - lineNumberDistance: "w:distance", + countBy: "w:countBy", + start: "w:start", + restart: "w:restart", + distance: "w:distance", }; } @@ -28,10 +28,10 @@ export class LineNumberType extends XmlComponent { super("w:lnNumType"); this.root.push( new LineNumberAttributes({ - lineNumberCountBy: countBy, - lineNumberStart: start, - lineNumberRestart: restart, - lineNumberDistance: dist, + countBy: countBy, + start: start, + restart: restart, + distance: dist, }), ); } diff --git a/src/file/document/body/section-properties/page-number/page-number.ts b/src/file/document/body/section-properties/page-number/page-number.ts index f536a6a935..e6a12101a7 100644 --- a/src/file/document/body/section-properties/page-number/page-number.ts +++ b/src/file/document/body/section-properties/page-number/page-number.ts @@ -26,16 +26,16 @@ export enum PageNumberSeparator { } export interface IPageNumberTypeAttributes { - readonly pageNumberStart?: number; - readonly pageNumberFormatType?: PageNumberFormat; - readonly pageNumberSeparator?: PageNumberSeparator; + readonly start?: number; + readonly formatType?: PageNumberFormat; + readonly separator?: PageNumberSeparator; } export class PageNumberTypeAttributes extends XmlAttributeComponent { protected readonly xmlKeys = { - pageNumberStart: "w:start", - pageNumberFormatType: "w:fmt", - pageNumberSeparator: "w:chapSep", + start: "w:start", + formatType: "w:fmt", + separator: "w:chapSep", }; } @@ -44,9 +44,9 @@ export class PageNumberType extends XmlComponent { super("w:pgNumType"); this.root.push( new PageNumberTypeAttributes({ - pageNumberStart: start, - pageNumberFormatType: numberFormat, - pageNumberSeparator: separator, + start: start, + formatType: numberFormat, + separator: separator, }), ); } diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index 9bd13aa078..3472772411 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -19,34 +19,46 @@ describe("SectionProperties", () => { const media = new Media(); const properties = new SectionProperties({ - width: 11906, - height: 16838, - top: convertInchesToTwip(1), - right: convertInchesToTwip(1), - bottom: convertInchesToTwip(1), - left: convertInchesToTwip(1), - header: 708, - footer: 708, - gutter: 0, - mirror: false, + page: { + size: { + width: 11906, + height: 16838, + }, + margin: { + top: convertInchesToTwip(1), + right: convertInchesToTwip(1), + bottom: convertInchesToTwip(1), + left: convertInchesToTwip(1), + header: 708, + footer: 708, + gutter: 0, + mirror: false, + }, + pageNumbers: { + start: 10, + formatType: PageNumberFormat.CARDINAL_TEXT, + }, + }, column: { space: 708, count: 1, separate: true, }, - linePitch: convertInchesToTwip(0.25), - headers: { + grid: { + linePitch: convertInchesToTwip(0.25), + }, + headerWrapperGroup: { default: new HeaderWrapper(media, 100), }, - footers: { + footerWrapperGroup: { even: new FooterWrapper(media, 200), }, - pageNumberStart: 10, - pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT, titlePage: true, verticalAlign: SectionVerticalAlignValue.TOP, }); + const tree = new Formatter().format(properties); + expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); expect(tree["w:sectPr"]).to.be.an.instanceof(Array); expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } }); @@ -98,7 +110,11 @@ describe("SectionProperties", () => { it("should create section properties with changed options", () => { const properties = new SectionProperties({ - top: 0, + page: { + margin: { + top: 0, + }, + }, }); const tree = new Formatter().format(properties); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); @@ -122,7 +138,11 @@ describe("SectionProperties", () => { it("should create section properties with changed options", () => { const properties = new SectionProperties({ - bottom: 0, + page: { + margin: { + bottom: 0, + }, + }, }); const tree = new Formatter().format(properties); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); @@ -146,8 +166,12 @@ describe("SectionProperties", () => { it("should create section properties with changed options", () => { const properties = new SectionProperties({ - width: 0, - height: 0, + page: { + size: { + width: 0, + height: 0, + }, + }, }); const tree = new Formatter().format(properties); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); @@ -171,8 +195,12 @@ describe("SectionProperties", () => { it("should create section properties with page borders", () => { const properties = new SectionProperties({ - pageBorders: { - offsetFrom: PageBorderOffsetFrom.PAGE, + page: { + borders: { + pageBorders: { + offsetFrom: PageBorderOffsetFrom.PAGE, + }, + }, }, }); const tree = new Formatter().format(properties); @@ -185,7 +213,11 @@ describe("SectionProperties", () => { it("should create section properties with page number type, but without start attribute", () => { const properties = new SectionProperties({ - pageNumberFormatType: PageNumberFormat.UPPER_ROMAN, + page: { + pageNumbers: { + formatType: PageNumberFormat.UPPER_ROMAN, + }, + }, }); const tree = new Formatter().format(properties); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); @@ -217,10 +249,12 @@ describe("SectionProperties", () => { it("should create section properties line number type", () => { const properties = new SectionProperties({ - lineNumberCountBy: 2, - lineNumberStart: 2, - lineNumberRestart: LineNumberRestartFormat.CONTINUOUS, - lineNumberDistance: 4, + lineNumbers: { + countBy: 2, + start: 2, + restart: LineNumberRestartFormat.CONTINUOUS, + distance: 4, + }, }); const tree = new Formatter().format(properties); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 1e0af54191..fd5a061218 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -1,4 +1,6 @@ // http://officeopenxml.com/WPsection.php +// tslint:disable: no-unnecessary-initializer + import { convertInchesToTwip } from "convenience-functions"; import { FooterWrapper } from "file/footer-wrapper"; import { HeaderWrapper } from "file/header-wrapper"; @@ -21,7 +23,7 @@ import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attr import { TitlePage } from "./title-page/title-page"; import { Type } from "./type/section-type"; import { SectionType } from "./type/section-type-attributes"; -import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align"; +import { SectionVerticalAlign, SectionVerticalAlignValue } from "./vertical-align"; export interface IHeaderFooterGroup { readonly default?: T; @@ -29,89 +31,71 @@ export interface IHeaderFooterGroup { readonly even?: T; } -interface IHeadersOptions { - readonly headers?: IHeaderFooterGroup; -} - -interface IFootersOptions { - readonly footers?: IHeaderFooterGroup; -} - -interface ITitlePageOptions { - readonly titlePage?: boolean; -} - -export type SectionPropertiesOptions = IPageSizeAttributes & - IPageMarginAttributes & - IDocGridAttributesProperties & - IHeadersOptions & - IFootersOptions & - IPageNumberTypeAttributes & - ILineNumberAttributes & - IPageBordersOptions & - ITitlePageOptions & - ISectionVerticalAlignAttributes & { - readonly column?: { - readonly space?: number; - readonly count?: number; - readonly separate?: boolean; - }; - readonly type?: SectionType; +export interface ISectionPropertiesOptions { + readonly page?: { + readonly size?: IPageSizeAttributes; + readonly margin?: IPageMarginAttributes; + readonly pageNumbers?: IPageNumberTypeAttributes; + readonly borders?: IPageBordersOptions; }; -// Need to decouple this from the attributes - + readonly grid?: IDocGridAttributesProperties; + readonly headerWrapperGroup?: IHeaderFooterGroup; + readonly footerWrapperGroup?: IHeaderFooterGroup; + readonly lineNumbers?: ILineNumberAttributes; + readonly titlePage?: boolean; + readonly verticalAlign?: SectionVerticalAlignValue; + readonly column?: { + readonly space?: number; + readonly count?: number; + readonly separate?: boolean; + }; + readonly type?: SectionType; +} export class SectionProperties extends XmlComponent { - public readonly width: number; - public readonly rightMargin: number; - public readonly leftMargin: number; - - constructor( - { - width = 11906, - height = 16838, - top = convertInchesToTwip(1), - right = convertInchesToTwip(1), - bottom = convertInchesToTwip(1), - left = convertInchesToTwip(1), - header = 708, - footer = 708, - gutter = 0, - mirror = false, - column = {}, - linePitch = 360, - orientation = PageOrientation.PORTRAIT, - headers, - footers, - pageNumberFormatType, - pageNumberStart, - pageNumberSeparator, - lineNumberCountBy, - lineNumberStart, - lineNumberRestart, - lineNumberDistance, - pageBorders, - pageBorderTop, - pageBorderRight, - pageBorderBottom, - pageBorderLeft, - titlePage = false, - verticalAlign, - type, - }: SectionPropertiesOptions = { column: {} }, - ) { + constructor({ + page: { + size: { width = 11906, height = 16838, orientation = PageOrientation.PORTRAIT } = {}, + margin: { + top = convertInchesToTwip(1), + right = convertInchesToTwip(1), + bottom = convertInchesToTwip(1), + left = convertInchesToTwip(1), + header = 708, + footer = 708, + gutter = 0, + mirror = false, + } = {}, + pageNumbers: { + start: pageNumberStart = undefined, + formatType: pageNumberFormatType = undefined, + separator: pageNumberSeparator = undefined, + } = {}, + borders: { + pageBorders = undefined, + pageBorderTop = undefined, + pageBorderRight = undefined, + pageBorderBottom = undefined, + pageBorderLeft = undefined, + } = {}, + } = {}, + grid: { linePitch = 360 } = {}, + headerWrapperGroup = {}, + footerWrapperGroup = {}, + lineNumbers: { countBy: lineNumberCountBy, start: lineNumberStart, restart: lineNumberRestart, distance: lineNumberDistance } = {}, + titlePage = false, + verticalAlign, + column: { space = 708, count = 1, separate = false } = {}, + type, + }: ISectionPropertiesOptions = {}) { super("w:sectPr"); - this.leftMargin = left; - this.rightMargin = right; - this.width = width; - this.root.push(new PageSize(width, height, orientation)); this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror)); - this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1, column.separate ?? false)); + this.root.push(new Columns(space, count, separate)); this.root.push(new DocumentGrid(linePitch)); - this.addHeaders(headers); - this.addFooters(footers); + this.addHeaders(headerWrapperGroup); + this.addFooters(footerWrapperGroup); this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType, pageNumberSeparator)); @@ -144,65 +128,61 @@ export class SectionProperties extends XmlComponent { } } - private addHeaders(headers?: IHeaderFooterGroup): void { - if (headers) { - if (headers.default) { - this.root.push( - new HeaderReference({ - headerType: HeaderReferenceType.DEFAULT, - headerId: headers.default.View.ReferenceId, - }), - ); - } + private addHeaders(headers: IHeaderFooterGroup): void { + if (headers.default) { + this.root.push( + new HeaderReference({ + headerType: HeaderReferenceType.DEFAULT, + headerId: headers.default.View.ReferenceId, + }), + ); + } - if (headers.first) { - this.root.push( - new HeaderReference({ - headerType: HeaderReferenceType.FIRST, - headerId: headers.first.View.ReferenceId, - }), - ); - } + if (headers.first) { + this.root.push( + new HeaderReference({ + headerType: HeaderReferenceType.FIRST, + headerId: headers.first.View.ReferenceId, + }), + ); + } - if (headers.even) { - this.root.push( - new HeaderReference({ - headerType: HeaderReferenceType.EVEN, - headerId: headers.even.View.ReferenceId, - }), - ); - } + if (headers.even) { + this.root.push( + new HeaderReference({ + headerType: HeaderReferenceType.EVEN, + headerId: headers.even.View.ReferenceId, + }), + ); } } - private addFooters(footers?: IHeaderFooterGroup): void { - if (footers) { - if (footers.default) { - this.root.push( - new FooterReference({ - footerType: FooterReferenceType.DEFAULT, - footerId: footers.default.View.ReferenceId, - }), - ); - } + private addFooters(footers: IHeaderFooterGroup): void { + if (footers.default) { + this.root.push( + new FooterReference({ + footerType: FooterReferenceType.DEFAULT, + footerId: footers.default.View.ReferenceId, + }), + ); + } - if (footers.first) { - this.root.push( - new FooterReference({ - footerType: FooterReferenceType.FIRST, - footerId: footers.first.View.ReferenceId, - }), - ); - } + if (footers.first) { + this.root.push( + new FooterReference({ + footerType: FooterReferenceType.FIRST, + footerId: footers.first.View.ReferenceId, + }), + ); + } - if (footers.even) { - this.root.push( - new FooterReference({ - footerType: FooterReferenceType.EVEN, - footerId: footers.even.View.ReferenceId, - }), - ); - } + if (footers.even) { + this.root.push( + new FooterReference({ + footerType: FooterReferenceType.EVEN, + footerId: footers.even.View.ReferenceId, + }), + ); } } } diff --git a/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts index fa09712a00..477bb448b4 100644 --- a/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts +++ b/src/file/document/body/section-properties/vertical-align/vertical-align-attributes.ts @@ -1,11 +1,9 @@ import { XmlAttributeComponent } from "file/xml-components"; import { SectionVerticalAlignValue } from "./vertical-align"; -export interface ISectionVerticalAlignAttributes { +export class SectionVerticalAlignAttributes extends XmlAttributeComponent<{ readonly verticalAlign?: SectionVerticalAlignValue; -} - -export class SectionVerticalAlignAttributes extends XmlAttributeComponent { +}> { protected readonly xmlKeys = { verticalAlign: "w:val", }; diff --git a/src/file/file.spec.ts b/src/file/file.spec.ts index f749bfe818..808407c6d9 100644 --- a/src/file/file.spec.ts +++ b/src/file/file.spec.ts @@ -1,40 +1,26 @@ import { expect } from "chai"; -import * as sinon from "sinon"; import { Formatter } from "export/formatter"; import { File } from "./file"; import { Footer, Header } from "./header"; import { Paragraph } from "./paragraph"; -import { Table, TableCell, TableRow } from "./table"; -import { TableOfContents } from "./table-of-contents"; describe("File", () => { describe("#constructor", () => { - it("should create with correct headers and footers by default", () => { - const doc = new File(); - - doc.addSection({ - children: [], - }); - - const tree = new Formatter().format(doc.Document.View.Body); - - expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default"); - expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default"); - }); - it("should create with correct headers and footers", () => { - const doc = new File(); - - doc.addSection({ - headers: { - default: new Header(), - }, - footers: { - default: new Footer(), - }, - children: [], + const doc = new File({ + sections: [ + { + headers: { + default: new Header(), + }, + footers: { + default: new Footer(), + }, + children: [], + }, + ], }); const tree = new Formatter().format(doc.Document.View.Body); @@ -44,16 +30,18 @@ describe("File", () => { }); it("should create with first headers and footers", () => { - const doc = new File(); - - doc.addSection({ - headers: { - first: new Header(), - }, - footers: { - first: new Footer(), - }, - children: [], + const doc = new File({ + sections: [ + { + headers: { + first: new Header(), + }, + footers: { + first: new Footer(), + }, + children: [], + }, + ], }); const tree = new Formatter().format(doc.Document.View.Body); @@ -62,20 +50,22 @@ describe("File", () => { }); it("should create with correct headers", () => { - const doc = new File(); - - doc.addSection({ - headers: { - default: new Header(), - first: new Header(), - even: new Header(), - }, - footers: { - default: new Footer(), - first: new Footer(), - even: new Footer(), - }, - children: [], + const doc = new File({ + sections: [ + { + headers: { + default: new Header(), + first: new Header(), + even: new Header(), + }, + footers: { + default: new Footer(), + first: new Footer(), + even: new Footer(), + }, + children: [], + }, + ], }); const tree = new Formatter().format(doc.Document.View.Body); @@ -90,11 +80,13 @@ describe("File", () => { }); it("should add child", () => { - const doc = new File(undefined, undefined, [ - { - children: [new Paragraph("test")], - }, - ]); + const doc = new File({ + sections: [ + { + children: [new Paragraph("test")], + }, + ], + }); const tree = new Formatter().format(doc.Document.View.Body); @@ -171,69 +163,13 @@ describe("File", () => { }); }); - describe("#addSection", () => { - it("should call the underlying document's add a Paragraph", () => { - const file = new File(); - const spy = sinon.spy(file.Document.View, "add"); - file.addSection({ - children: [new Paragraph({})], - }); - - expect(spy.called).to.equal(true); - }); - - it("should call the underlying document's add when adding a Table", () => { - const file = new File(); - const spy = sinon.spy(file.Document.View, "add"); - file.addSection({ - children: [ - new Table({ - rows: [ - new TableRow({ - children: [ - new TableCell({ - children: [new Paragraph("hello")], - }), - ], - }), - ], - }), - ], - }); - - expect(spy.called).to.equal(true); - }); - - it("should call the underlying document's add when adding an Image (paragraph)", () => { - const file = new File(); - const spy = sinon.spy(file.Document.View, "add"); - // tslint:disable-next-line:no-any - file.addSection({ - children: [new Paragraph("")], - }); - - expect(spy.called).to.equal(true); - }); - }); - - describe("#addSection", () => { - it("should call the underlying document's add", () => { - const file = new File(); - const spy = sinon.spy(file.Document.View, "add"); - file.addSection({ - children: [new TableOfContents()], - }); - - expect(spy.called).to.equal(true); - }); - }); - describe("#addTrackRevisionsFeature", () => { it("should call the underlying document's add", () => { const file = new File({ features: { trackRevisions: true, }, + sections: [], }); // tslint:disable-next-line: no-unused-expression no-string-literal @@ -249,6 +185,7 @@ describe("File", () => { children: [new Paragraph("hello")], }, }, + sections: [], }); const tree = new Formatter().format(wrapper.FootNotes.View); @@ -435,6 +372,7 @@ describe("File", () => { styles: { default: {}, }, + sections: [], }); const tree = new Formatter().format(doc.Styles); @@ -454,6 +392,7 @@ describe("File", () => { it("should create with even and odd headers and footers", () => { const doc = new File({ evenAndOddHeaderAndFooters: true, + sections: [], }); const tree = new Formatter().format(doc.Settings); diff --git a/src/file/file.ts b/src/file/file.ts index 540122cde4..66d4d84b51 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -3,13 +3,7 @@ import { ContentTypes } from "./content-types/content-types"; import { CoreProperties, IPropertiesOptions } from "./core-properties"; import { CustomProperties } from "./custom-properties"; import { DocumentWrapper } from "./document-wrapper"; -import { - FooterReferenceType, - HeaderReferenceType, - IPageSizeAttributes, - SectionPropertiesOptions, -} from "./document/body/section-properties"; -import { IPageMarginAttributes } from "./document/body/section-properties/page-margin/page-margin-attributes"; +import { FooterReferenceType, HeaderReferenceType, ISectionPropertiesOptions } from "./document/body/section-properties"; import { IFileProperties } from "./file-properties"; import { FooterWrapper, IDocumentFooter } from "./footer-wrapper"; import { FootnotesWrapper } from "./footnotes-wrapper"; @@ -37,9 +31,7 @@ export interface ISectionOptions { readonly first?: Footer; readonly even?: Footer; }; - readonly size?: IPageSizeAttributes; - readonly margins?: IPageMarginAttributes; - readonly properties?: SectionPropertiesOptions; + readonly properties?: ISectionPropertiesOptions; readonly children: (Paragraph | Table | TableOfContents)[]; } @@ -61,16 +53,14 @@ export class File { private readonly appProperties: AppProperties; private readonly styles: Styles; - constructor( - options: IPropertiesOptions = { - creator: "Un-named", - revision: "1", - lastModifiedBy: "Un-named", - }, - fileProperties: IFileProperties = {}, - sections: ISectionOptions[] = [], - ) { - this.coreProperties = new CoreProperties(options); + constructor(options: IPropertiesOptions, fileProperties: IFileProperties = {}) { + this.coreProperties = new CoreProperties({ + ...options, + creator: options.creator ?? "Un-named", + revision: options.revision ?? "1", + lastModifiedBy: options.lastModifiedBy ?? "Un-named", + }); + this.numbering = new Numbering( options.numbering ? options.numbering @@ -78,6 +68,7 @@ export class File { config: [], }, ); + this.fileRelationships = new Relationships(); this.customProperties = new CustomProperties(options.customProperties ?? []); this.appProperties = new AppProperties(); @@ -131,12 +122,8 @@ export class File { } } - for (const section of sections) { - this.documentWrapper.View.Body.addSection(section.properties ? section.properties : {}); - - for (const child of section.children) { - this.documentWrapper.View.add(child); - } + for (const section of options.sections) { + this.addSection(section); } if (options.footnotes) { @@ -153,28 +140,25 @@ export class File { } } - public addSection({ - headers = { default: new Header() }, - footers = { default: new Header() }, - margins = {}, - size = {}, - properties, - children, - }: ISectionOptions): void { + public verifyUpdateFields(): void { + if (this.documentWrapper.View.getTablesOfContents().length) { + this.settings.addUpdateFields(); + } + } + + private addSection({ headers = {}, footers = {}, children, properties }: ISectionOptions): void { this.documentWrapper.View.Body.addSection({ ...properties, - headers: { + headerWrapperGroup: { default: headers.default ? this.createHeader(headers.default) : undefined, first: headers.first ? this.createHeader(headers.first) : undefined, even: headers.even ? this.createHeader(headers.even) : undefined, }, - footers: { + footerWrapperGroup: { default: footers.default ? this.createFooter(footers.default) : undefined, first: footers.first ? this.createFooter(footers.first) : undefined, even: footers.even ? this.createFooter(footers.even) : undefined, }, - ...margins, - ...size, }); for (const child of children) { @@ -182,12 +166,6 @@ export class File { } } - public verifyUpdateFields(): void { - if (this.documentWrapper.View.getTablesOfContents().length) { - this.settings.addUpdateFields(); - } - } - private createHeader(header: Header): HeaderWrapper { const wrapper = new HeaderWrapper(this.media, this.currentRelationshipId++); diff --git a/src/index.spec.ts b/src/index.spec.ts index 47633f9d22..dd687e7bf7 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -6,7 +6,11 @@ describe("Index", () => { describe("Document", () => { it("should instantiate the Document", () => { // tslint:disable-next-line: no-unused-expression - expect(new Document()).to.be.ok; + expect( + new Document({ + sections: [], + }), + ).to.be.ok; }); }); });