diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000000..c91cc33aeb --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,12 @@ +# These are supported funding model platforms + +github: dolanmiu +patreon: dolanmiu +open_collective: # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.gitignore b/.gitignore index 8a7c8baf67..7b65d85792 100644 --- a/.gitignore +++ b/.gitignore @@ -52,7 +52,6 @@ docs/.nojekyll .idea # Lock files -package-lock.json yarn.lock # Documents diff --git a/.nycrc b/.nycrc index e02a0d16f8..d5f12ccbc3 100644 --- a/.nycrc +++ b/.nycrc @@ -1,9 +1,9 @@ { "check-coverage": true, - "lines": 87.54, - "functions": 83.61, - "branches": 72.57, - "statements": 87.32, + "lines": 92.35, + "functions": 88.28, + "branches": 84.64, + "statements": 92.16, "include": [ "src/**/*.ts" ], diff --git a/.travis.yml b/.travis.yml index 3c015ad296..3bf1b47690 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,7 +42,7 @@ script: - npm run ts-node -- ./demo/29-numbered-lists.ts - npm run ts-node -- ./demo/30-template-document.ts - npm run ts-node -- ./demo/31-tables.ts - - npm run ts-node -- ./demo/32-merge-table-cells.ts + - npm run ts-node -- ./demo/32-merge-and-shade-table-cells.ts # - npm run e2e "My Document.docx" // Need to fix - npm run ts-node -- ./demo/33-sequential-captions.ts - npm run ts-node -- ./demo/34-floating-tables.ts diff --git a/demo/10-my-cv.ts b/demo/10-my-cv.ts index 6c60be4f48..b3e7c55e01 100644 --- a/demo/10-my-cv.ts +++ b/demo/10-my-cv.ts @@ -1,7 +1,7 @@ // Generate a CV // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { AlignmentType, Document, HeadingLevel, Packer, Paragraph, TextRun } from "../build"; +import { AlignmentType, Document, HeadingLevel, Packer, Paragraph, TabStopPosition, TabStopType, TextRun } from "../build"; // tslint:disable:no-shadowed-variable @@ -226,9 +226,12 @@ class DocumentCreator { public createInstitutionHeader(institutionName: string, dateText: string): Paragraph { return new Paragraph({ - tabStop: { - maxRight: {}, - }, + tabStops: [ + { + type: TabStopType.RIGHT, + position: TabStopPosition.MAX, + }, + ], children: [ new TextRun({ text: institutionName, diff --git a/demo/11-declaritive-styles-2.ts b/demo/11-declaritive-styles-2.ts index bb6428d518..3a0154a0fb 100644 --- a/demo/11-declaritive-styles-2.ts +++ b/demo/11-declaritive-styles-2.ts @@ -1,93 +1,188 @@ // Setting styles with JavaScript configuration // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { AlignmentType, Document, Footer, HeadingLevel, Media, Packer, Paragraph, Table } from "../build"; +import { + AlignmentType, + Document, + Footer, + HeadingLevel, + Media, + Packer, + Paragraph, + Table, + TableCell, + TableRow, + TabStopPosition, + UnderlineType, +} from "../build"; -const doc = new Document(); - -doc.Styles.createParagraphStyle("Heading1", "Heading 1") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .font("Calibri") - .size(52) - .center() - .bold() - .color("000000") - .spacing({ line: 340 }) - .underline("single", "000000"); - -doc.Styles.createParagraphStyle("Heading2", "Heading 2") - .basedOn("Normal") - .next("Normal") - .font("Calibri") - .quickFormat() - .size(26) - .bold() - .spacing({ line: 340 }); - -doc.Styles.createParagraphStyle("Heading3", "Heading 3") - .basedOn("Normal") - .next("Normal") - .font("Calibri") - .quickFormat() - .size(26) - .bold() - .spacing({ line: 276 }); - -doc.Styles.createParagraphStyle("Heading4", "Heading 4") - .basedOn("Normal") - .next("Normal") - .justified() - .font("Calibri") - .size(26) - .bold(); - -doc.Styles.createParagraphStyle("normalPara", "Normal Para") - .basedOn("Normal") - .next("Normal") - .font("Calibri") - .quickFormat() - .leftTabStop(453.543307087) - .maxRightTabStop() - .size(26) - .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); - -doc.Styles.createParagraphStyle("normalPara2", "Normal Para2") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .font("Calibri") - .size(26) - .justified() - .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); - -doc.Styles.createParagraphStyle("aside", "Aside") - .basedOn("Normal") - .next("Normal") - .color("999999") - .italics() - .indent({ left: 720 }) - .spacing({ line: 276 }); - -doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") - .basedOn("Normal") - .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); - -doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph") - .quickFormat() - .basedOn("Normal"); +const doc = new Document({ + styles: { + paragraphStyles: [ + { + id: "Heading1", + name: "Heading 1", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 52, + bold: true, + color: "000000", + underline: { + type: UnderlineType.SINGLE, + color: "000000", + }, + }, + paragraph: { + alignment: AlignmentType.CENTER, + spacing: { line: 340 }, + }, + }, + { + id: "Heading2", + name: "Heading 2", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 340 }, + }, + }, + { + id: "Heading3", + name: "Heading 3", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 276 }, + }, + }, + { + id: "Heading4", + name: "Heading 4", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + alignment: AlignmentType.JUSTIFIED, + }, + }, + { + id: "normalPara", + name: "Normal Para", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + bold: true, + }, + paragraph: { + spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, + rightTabStop: TabStopPosition.MAX, + leftTabStop: 453.543307087, + }, + }, + { + id: "normalPara2", + name: "Normal Para2", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + font: "Calibri", + size: 26, + }, + paragraph: { + alignment: AlignmentType.JUSTIFIED, + spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, + }, + }, + { + id: "aside", + name: "Aside", + basedOn: "Normal", + next: "Normal", + run: { + color: "999999", + italics: true, + }, + paragraph: { + spacing: { line: 276 }, + indent: { left: 720 }, + }, + }, + { + id: "wellSpaced", + name: "Well Spaced", + basedOn: "Normal", + paragraph: { + spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, + }, + }, + { + id: "ListParagraph", + name: "List Paragraph", + basedOn: "Normal", + quickFormat: true, + }, + ], + }, +}); const image = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif")); const table = new Table({ - rows: 4, - columns: 4, + 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.")], + }), + ], + }), + ], }); -table - .getRow(0) - .getCell(0) - .add(new Paragraph("Pole No.")); const image1 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif")); const image2 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif")); diff --git a/demo/14-page-numbers.ts b/demo/14-page-numbers.ts index fc9a20169d..ae34300b5c 100644 --- a/demo/14-page-numbers.ts +++ b/demo/14-page-numbers.ts @@ -1,7 +1,7 @@ // Page numbers // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { AlignmentType, Document, Header, Packer, Paragraph, TextRun } from "../build"; +import { AlignmentType, Document, Header, Packer, PageBreak, Paragraph, TextRun } from "../build"; const doc = new Document(); @@ -26,8 +26,8 @@ doc.addSection({ }, children: [ new Paragraph({ - text: "First Page", - }).pageBreak(), + children: [new TextRun("First Page"), new PageBreak()], + }), new Paragraph("Second Page"), ], }); diff --git a/demo/16-multiple-sections.ts b/demo/16-multiple-sections.ts index 10e2098704..5518eb5416 100644 --- a/demo/16-multiple-sections.ts +++ b/demo/16-multiple-sections.ts @@ -6,7 +6,7 @@ import { Document, Footer, Header, Packer, PageNumberFormat, PageOrientation, Pa const doc = new Document(); doc.addSection({ - children: [new Paragraph("Hello World").pageBreak()], + children: [new Paragraph("Hello World")], }); doc.addSection({ diff --git a/demo/17-footnotes.ts b/demo/17-footnotes.ts index 3920d0dce8..bf3f11eb91 100644 --- a/demo/17-footnotes.ts +++ b/demo/17-footnotes.ts @@ -1,14 +1,20 @@ // Footnotes // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph } from "../build"; +import { Document, Packer, Paragraph, TextRun } from "../build"; const doc = new Document(); doc.addSection({ - children: [new Paragraph("Hello World").referenceFootnote(1), new Paragraph("Hello World").referenceFootnote(2)], + children: [ + new Paragraph({ + children: [new TextRun("Hello").referenceFootnote(1), new TextRun(" World!").referenceFootnote(2)], + }), + new Paragraph("Hello World").referenceFootnote(3), + ], }); +doc.createFootnote(new Paragraph("Foo")); doc.createFootnote(new Paragraph("Test")); doc.createFootnote(new Paragraph("My amazing reference")); diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts index a854b04546..34d7e19e70 100644 --- a/demo/2-declaritive-styles.ts +++ b/demo/2-declaritive-styles.ts @@ -1,48 +1,90 @@ // Example on how to customise the look at feel using Styles // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, HeadingLevel, Packer, Paragraph, TextRun } from "../build"; +import { Document, HeadingLevel, Packer, Paragraph, TextRun, UnderlineType } from "../build"; const doc = new Document({ creator: "Clippy", title: "Sample Document", description: "A brief example of using docx", + styles: { + paragraphStyles: [ + { + id: "Heading1", + name: "Heading 1", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + size: 28, + bold: true, + italics: true, + color: "red", + }, + paragraph: { + spacing: { + after: 120, + }, + }, + }, + { + id: "Heading2", + name: "Heading 2", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + size: 26, + bold: true, + underline: { + type: UnderlineType.DOUBLE, + color: "FF0000", + }, + }, + paragraph: { + spacing: { + before: 240, + after: 120, + }, + }, + }, + { + id: "aside", + name: "Aside", + basedOn: "Normal", + next: "Normal", + run: { + color: "999999", + italics: true, + }, + paragraph: { + indent: { + left: 720, + }, + spacing: { + line: 276, + }, + }, + }, + { + id: "wellSpaced", + name: "Well Spaced", + basedOn: "Normal", + quickFormat: true, + paragraph: { + spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }, + }, + }, + { + id: "ListParagraph", + name: "List Paragraph", + basedOn: "Normal", + quickFormat: true, + }, + ], + }, }); -doc.Styles.createParagraphStyle("Heading1", "Heading 1") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(28) - .bold() - .italics() - .spacing({ after: 120 }); - -doc.Styles.createParagraphStyle("Heading2", "Heading 2") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(26) - .bold() - .underline("double", "FF0000") - .spacing({ before: 240, after: 120 }); - -doc.Styles.createParagraphStyle("aside", "Aside") - .basedOn("Normal") - .next("Normal") - .color("999999") - .italics() - .indent({ left: 720 }) - .spacing({ line: 276 }); - -doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") - .basedOn("Normal") - .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); - -doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph") - .quickFormat() - .basedOn("Normal"); - const numberedAbstract = doc.Numbering.createAbstractNumbering(); numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left"); @@ -82,14 +124,16 @@ doc.addSection({ level: 0, }, }), - new Paragraph({}).addRun( - new TextRun({ - text: "Some monospaced content", - font: { - name: "Monospace", - }, - }), - ), + new Paragraph({ + children: [ + new TextRun({ + text: "Some monospaced content", + font: { + name: "Monospace", + }, + }), + ], + }), new Paragraph({ text: "An aside, in light gray italics and indented", style: "aside", diff --git a/demo/20-table-cell-borders.ts b/demo/20-table-cell-borders.ts index 13809a6164..028d9fdb04 100644 --- a/demo/20-table-cell-borders.ts +++ b/demo/20-table-cell-borders.ts @@ -1,23 +1,102 @@ // Add custom borders to table cell // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { BorderStyle, Document, Packer, Paragraph, Table } from "../build"; +import { BorderStyle, Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; const doc = new Document(); const table = new Table({ - rows: 4, - columns: 4, + 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: [], + }), + ], + }), + ], }); doc.addSection({ children: [table] }); -table - .getCell(2, 2) - .add(new Paragraph("Hello")) - .Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red") - .addBottomBorder(BorderStyle.DOUBLE, 3, "blue") - .addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green") - .addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); diff --git a/demo/21-bookmarks.ts b/demo/21-bookmarks.ts index 7a8e7b523b..cc0bac5517 100644 --- a/demo/21-bookmarks.ts +++ b/demo/21-bookmarks.ts @@ -1,7 +1,7 @@ // This demo shows how to create bookmarks then link to them with internal hyperlinks // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, HeadingLevel, Packer, Paragraph } from "../build"; +import { Document, HeadingLevel, Packer, PageBreak, Paragraph } from "../build"; const LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante."; @@ -22,11 +22,16 @@ doc.addSection({ children: [ new Paragraph({ heading: HeadingLevel.HEADING_1, - }).addBookmark(bookmark), + children: [bookmark], + }), new Paragraph("\n"), new Paragraph(LOREM_IPSUM), - new Paragraph({}).pageBreak(), - new Paragraph({}).addHyperLink(hyperlink), + new Paragraph({ + children: [new PageBreak()], + }), + new Paragraph({ + children: [hyperlink], + }), ], }); diff --git a/demo/24-images-to-table-cell.ts b/demo/24-images-to-table-cell.ts index 114be5491e..e37d1aa812 100644 --- a/demo/24-images-to-table-cell.ts +++ b/demo/24-images-to-table-cell.ts @@ -1,24 +1,85 @@ // Add image to table cell // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Media, Packer, Paragraph, Table } from "../build"; +import { Document, Media, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; const doc = new Document(); +const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); + const table = new Table({ - rows: 4, - columns: 4, + 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(image)], + }), + 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], }); -table.getCell(2, 2).add(new Paragraph("Hello")); - -const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); -table.getCell(1, 1).add(new Paragraph(image)); - Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); }); diff --git a/demo/27-declaritive-styles-3.ts b/demo/27-declaritive-styles-3.ts index 31994dfe4a..8419467799 100644 --- a/demo/27-declaritive-styles-3.ts +++ b/demo/27-declaritive-styles-3.ts @@ -1,28 +1,53 @@ // Custom styles using JavaScript configuration // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, HeadingLevel, Packer, Paragraph } from "../build"; +import { Document, HeadingLevel, Packer, Paragraph, UnderlineType } from "../build"; -const doc = new Document(); - -// The first argument is an ID you use to apply the style to paragraphs -// The second argument is a human-friendly name to show in the UI -doc.Styles.createParagraphStyle("myWonkyStyle", "My Wonky Style") - .basedOn("Normal") - .next("Normal") - .color("990000") - .italics() - .indent({ left: 720 }) // 720 TWIP === 720 / 20 pt === .5 in - .spacing({ line: 276 }); // 276 / 240 = 1.15x line spacing - -doc.Styles.createParagraphStyle("Heading2", "Heading 2") - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(26) // 26 half-points === 13pt font - .bold() - .underline("double", "FF0000") - .spacing({ before: 240, after: 120 }); // TWIP for both +const doc = new Document({ + styles: { + paragraphStyles: [ + { + id: "myWonkyStyle", + name: "My Wonky Style", + basedOn: "Normal", + next: "Normal", + run: { + color: "990000", + italics: true, + }, + paragraph: { + indent: { + left: 720, + }, + spacing: { + line: 276, + }, + }, + }, + { + id: "Heading2", + name: "Heading 2", + basedOn: "Normal", + next: "Normal", + quickFormat: true, + run: { + bold: true, + size: 26, + underline: { + type: UnderlineType.DOUBLE, + color: "FF0000", + }, + }, + paragraph: { + spacing: { + before: 240, + after: 120, + }, + }, + }, + ], + }, +}); doc.addSection({ children: [ diff --git a/demo/28-table-of-contents.ts b/demo/28-table-of-contents.ts index 83160b0177..76789ee256 100644 --- a/demo/28-table-of-contents.ts +++ b/demo/28-table-of-contents.ts @@ -3,15 +3,23 @@ import * as fs from "fs"; import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "../build"; -const doc = new File(); - -// The first argument is an ID you use to apply the style to paragraphs -// The second argument is a human-friendly name to show in the UI -doc.Styles.createParagraphStyle("MySpectacularStyle", "My Spectacular Style") - .basedOn("Heading1") - .next("Heading1") - .color("990000") - .italics(); +const doc = new File({ + styles: { + paragraphStyles: [ + { + id: "MySpectacularStyle", + name: "My Spectacular Style", + basedOn: "Heading1", + next: "Heading1", + quickFormat: true, + run: { + italics: true, + color: "990000", + }, + }, + ], + }, +}); // WordprocessingML docs for TableOfContents can be found here: // http://officeopenxml.com/WPtableOfContents.php diff --git a/demo/3-numbering-and-bullet-points.ts b/demo/3-numbering-and-bullet-points.ts index 94d5665568..e87e6616d2 100644 --- a/demo/3-numbering-and-bullet-points.ts +++ b/demo/3-numbering-and-bullet-points.ts @@ -10,7 +10,7 @@ const numbering = new Numbering(); const abstractNum = numbering.createAbstractNumbering(); abstractNum.createLevel(0, "upperRoman", "%1", "start").indent({ left: 720, hanging: 260 }); abstractNum.createLevel(1, "decimal", "%2.", "start").indent({ left: 1440, hanging: 980 }); -abstractNum.createLevel(2, "lowerLetter", "%3)", "start").indent({ left: 14402160, hanging: 1700 }); +abstractNum.createLevel(2, "lowerLetter", "%3)", "start").indent({ left: 2160, hanging: 1700 }); const concrete = numbering.createConcreteNumbering(abstractNum); diff --git a/demo/31-tables.ts b/demo/31-tables.ts index 050403bb99..7daec1506d 100644 --- a/demo/31-tables.ts +++ b/demo/31-tables.ts @@ -1,28 +1,48 @@ // Example of how you would create a table and add data to it // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, HeadingLevel, Packer, Paragraph, Table, VerticalAlign } from "../build"; +import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign } from "../build"; const doc = new Document(); const table = new Table({ - rows: 2, - columns: 2, + 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 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, + }), + ], + }), + ], }); -table - .getCell(1, 1) - .add(new Paragraph("This text should be in the middle of the cell")) - .setVerticalAlign(VerticalAlign.CENTER); - -table.getCell(1, 0).add( - 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, - }), -); - doc.addSection({ children: [table], }); diff --git a/demo/32-merge-and-shade-table-cells.ts b/demo/32-merge-and-shade-table-cells.ts new file mode 100644 index 0000000000..77d6815d1e --- /dev/null +++ b/demo/32-merge-and-shade-table-cells.ts @@ -0,0 +1,231 @@ +// Example of how you would merge cells together (Rows and Columns) and apply shading +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, HeadingLevel, Packer, Paragraph, ShadingType, Table, TableCell, TableRow, WidthType } from "../build"; + +const doc = new Document(); + +const table = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Hello")], + columnSpan: 2, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], +}); + +const table2 = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("World")], + margins: { + top: 1000, + bottom: 1000, + left: 1000, + right: 1000, + }, + columnSpan: 3, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], + width: { + size: 100, + type: WidthType.AUTO, + }, + columnWidths: [1000, 1000, 1000], +}); + +const table3 = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Foo")], + }), + new TableCell({ + children: [new Paragraph("v")], + columnSpan: 3, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Bar1")], + shading: { + fill: "b79c2f", + val: ShadingType.REVERSE_DIAGONAL_STRIPE, + color: "auto", + }, + }), + new TableCell({ + children: [new Paragraph("Bar2")], + shading: { + fill: "42c5f4", + val: ShadingType.PERCENT_95, + color: "auto", + }, + }), + new TableCell({ + children: [new Paragraph("Bar3")], + shading: { + fill: "880aa8", + val: ShadingType.PERCENT_10, + color: "e2df0b", + }, + }), + new TableCell({ + children: [new Paragraph("Bar4")], + shading: { + fill: "FF0000", + val: ShadingType.CLEAR, + color: "auto", + }, + }), + ], + }), + ], + width: { + size: 7000, + type: WidthType.DXA, + }, + margins: { + top: 400, + bottom: 400, + right: 400, + left: 400, + }, +}); + +const table4 = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("0,0")], + columnSpan: 2, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("1,0")], + }), + new TableCell({ + children: [new Paragraph("1,1")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("2,0")], + columnSpan: 2, + }), + ], + }), + ], + width: { + size: 100, + type: WidthType.PERCENTAGE, + }, +}); + +const table5 = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("0,0")], + }), + new TableCell({ + children: [new Paragraph("0,1")], + rowSpan: 2, + }), + new TableCell({ + children: [new Paragraph("0,2")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [new Paragraph("1,2")], + rowSpan: 2, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], + width: { + size: 100, + 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"), + table4, + new Paragraph("More Merging columns"), + table5, + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/32-merge-table-cells.ts b/demo/32-merge-table-cells.ts deleted file mode 100644 index cd850067e5..0000000000 --- a/demo/32-merge-table-cells.ts +++ /dev/null @@ -1,113 +0,0 @@ -// Example of how you would merge cells together -// Import from 'docx' rather than '../build' if you install from npm -import * as fs from "fs"; -import { Document, HeadingLevel, Packer, Paragraph, ShadingType, Table, WidthType } from "../build"; - -const doc = new Document(); - -const table = new Table({ - rows: 2, - columns: 2, -}); - -table.getCell(0, 0).add(new Paragraph("Hello")); -table.getRow(0).mergeCells(0, 1); - -const table2 = new Table({ - rows: 2, - columns: 3, - width: 100, - widthUnitType: WidthType.AUTO, - columnWidths: [1000, 1000, 1000], -}); - -table2 - .getCell(0, 0) - .add(new Paragraph("World")) - .setMargins({ - top: 1000, - bottom: 1000, - left: 1000, - right: 1000, - }); -table.getRow(0).mergeCells(0, 2); - -const table3 = new Table({ - rows: 2, - columns: 4, - width: 7000, - widthUnitType: WidthType.DXA, - margins: { - top: 400, - bottom: 400, - right: 400, - left: 400, - }, -}); - -table3.getCell(0, 0).add(new Paragraph("Foo")); -table3.getCell(0, 1).add(new Paragraph("v")); - -table3 - .getCell(1, 0) - .add(new Paragraph("Bar1")) - .setShading({ - fill: "b79c2f", - val: ShadingType.REVERSE_DIAGONAL_STRIPE, - color: "auto", - }); -table3 - .getCell(1, 1) - .add(new Paragraph("Bar2")) - .setShading({ - fill: "42c5f4", - val: ShadingType.PERCENT_95, - color: "auto", - }); -table3 - .getCell(1, 2) - .add(new Paragraph("Bar3")) - .setShading({ - fill: "880aa8", - val: ShadingType.PERCENT_10, - color: "e2df0b", - }); -table3 - .getCell(1, 3) - .add(new Paragraph("Bar4")) - .setShading({ - fill: "FF0000", - val: ShadingType.CLEAR, - color: "auto", - }); - -table3.getRow(0).mergeCells(0, 3); - -const table4 = new Table({ - rows: 2, - columns: 2, - width: 100, - widthUnitType: 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("hi"), - table4, - ], -}); - -Packer.toBuffer(doc).then((buffer) => { - fs.writeFileSync("My Document.docx", buffer); -}); diff --git a/demo/33-sequential-captions.ts b/demo/33-sequential-captions.ts index c06c3aa1cc..aed113d0fa 100644 --- a/demo/33-sequential-captions.ts +++ b/demo/33-sequential-captions.ts @@ -1,28 +1,44 @@ // Sequential Captions // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph, TextRun } from "../build"; +import { Document, Packer, Paragraph, SequentialIdentifier, TextRun } from "../build"; const doc = new Document(); doc.addSection({ children: [ - new Paragraph("Hello World 1->") - .addSequentialIdentifier("Caption") - .addRun(new TextRun(" text after sequencial caption 2->")) - .addSequentialIdentifier("Caption"), - new Paragraph("Hello World 1->") - .addSequentialIdentifier("Label") - .addRun(new TextRun(" text after sequencial caption 2->")) - .addSequentialIdentifier("Label"), - new Paragraph("Hello World 1->") - .addSequentialIdentifier("Another") - .addRun(new TextRun(" text after sequencial caption 3->")) - .addSequentialIdentifier("Label"), - new Paragraph("Hello World 2->") - .addSequentialIdentifier("Another") - .addRun(new TextRun(" text after sequencial caption 4->")) - .addSequentialIdentifier("Label"), + 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"), + ], + }), ], }); diff --git a/demo/34-floating-tables.ts b/demo/34-floating-tables.ts index c5b8a18cf8..dadf80f9da 100644 --- a/demo/34-floating-tables.ts +++ b/demo/34-floating-tables.ts @@ -9,29 +9,48 @@ import { RelativeVerticalPosition, Table, TableAnchorType, + TableCell, TableLayoutType, + TableRow, WidthType, } from "../build"; const doc = new Document(); const table = new Table({ - rows: 2, - columns: 2, + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Hello")], + columnSpan: 2, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], float: { horizontalAnchor: TableAnchorType.MARGIN, verticalAnchor: TableAnchorType.MARGIN, relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT, relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, }, - width: 4535, - widthUnitType: WidthType.DXA, + width: { + size: 4535, + type: WidthType.DXA, + }, layout: TableLayoutType.FIXED, }); -table.getCell(0, 0).add(new Paragraph("Hello")); -table.getRow(0).mergeCells(0, 1); - doc.addSection({ children: [table], }); diff --git a/demo/35-hyperlinks.ts b/demo/35-hyperlinks.ts index c547d93b36..6392299b84 100644 --- a/demo/35-hyperlinks.ts +++ b/demo/35-hyperlinks.ts @@ -4,12 +4,14 @@ import * as fs from "fs"; import { Document, Packer, Paragraph } from "../build"; const doc = new Document(); -const paragraph = new Paragraph({}); const link = doc.createHyperlink("http://www.example.com", "Hyperlink"); -paragraph.addHyperLink(link); doc.addSection({ - children: [paragraph], + children: [ + new Paragraph({ + children: [link], + }), + ], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/36-image-to-table-cell.ts b/demo/36-image-to-table-cell.ts index 1c7897c558..a0d87230a2 100644 --- a/demo/36-image-to-table-cell.ts +++ b/demo/36-image-to-table-cell.ts @@ -1,16 +1,61 @@ -// Add image to table cell +// Add image to table cell in a header and body // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Header, Media, Packer, Paragraph, Table } from "../build"; +import { Document, Header, Media, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; const doc = new Document(); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg")); const table = new Table({ - rows: 2, - columns: 2, + 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(image)], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], }); -table.getCell(1, 1).add(new Paragraph(image)); // Adding same table in the body and in the header doc.addSection({ diff --git a/demo/4-basic-table.ts b/demo/4-basic-table.ts index 78367caab9..593fe821a5 100644 --- a/demo/4-basic-table.ts +++ b/demo/4-basic-table.ts @@ -1,17 +1,35 @@ // Example of how you would create a table and add data to it // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph, Table } from "../build"; +import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; const doc = new Document(); const table = new Table({ - rows: 4, - columns: 4, + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("Hello")], + }), + new TableCell({ + children: [], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [new Paragraph("World")], + }), + ], + }), + ], }); -table.getCell(2, 2).add(new Paragraph("Hello")); - doc.addSection({ children: [table], }); diff --git a/demo/41-merge-table-cells-2.ts b/demo/41-merge-table-cells-2.ts index 1801eb4a6e..94e7bffe1d 100644 --- a/demo/41-merge-table-cells-2.ts +++ b/demo/41-merge-table-cells-2.ts @@ -1,52 +1,259 @@ -// Multiple cells merging in the same table +// Multiple cells merging in the same table - Rows and Columns // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph, Table } from "../build"; +import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; const doc = new Document(); const table = new Table({ - rows: 13, - columns: 6, + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("0,0")], + }), + new TableCell({ + children: [new Paragraph("0,1")], + columnSpan: 2, + }), + new TableCell({ + children: [new Paragraph("0,3")], + }), + new TableCell({ + children: [new Paragraph("0,4")], + columnSpan: 2, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("1,0")], + columnSpan: 2, + }), + new TableCell({ + children: [new Paragraph("1,2")], + columnSpan: 2, + }), + new TableCell({ + children: [new Paragraph("1,4")], + columnSpan: 2, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("2,0")], + }), + new TableCell({ + children: [new Paragraph("2,1")], + columnSpan: 2, + }), + new TableCell({ + children: [new Paragraph("2,3")], + }), + new TableCell({ + children: [new Paragraph("2,4")], + columnSpan: 2, + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("3,0")], + }), + new TableCell({ + children: [new Paragraph("3,1")], + }), + new TableCell({ + children: [new Paragraph("3,2")], + }), + new TableCell({ + children: [new Paragraph("3,3")], + }), + new TableCell({ + children: [new Paragraph("3,4")], + }), + new TableCell({ + children: [new Paragraph("3,5")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("4,0")], + columnSpan: 5, + }), + new TableCell({ + children: [new Paragraph("4,5")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], }); -let row = 0; -table.getCell(row, 0).add(new Paragraph("0,0")); -table.getCell(row, 1).add(new Paragraph("0,1")); -table.getCell(row, 3).add(new Paragraph("0,3")); -table.getCell(row, 4).add(new Paragraph("0,4")); -table.getRow(row).mergeCells(4, 5); -table.getRow(row).mergeCells(1, 2); -row = 1; -table.getCell(row, 0).add(new Paragraph("1,0")); -table.getCell(row, 2).add(new Paragraph("1,2")); -table.getCell(row, 4).add(new Paragraph("1,4")); -table.getRow(row).mergeCells(4, 5); -table.getRow(row).mergeCells(2, 3); -table.getRow(row).mergeCells(0, 1); - -row = 2; -table.getCell(row, 0).add(new Paragraph("2,0")); -table.getCell(row, 1).add(new Paragraph("2,1")); -table.getCell(row, 2).add(new Paragraph("2,2")); -table.getCell(row, 3).add(new Paragraph("2,3")); -table.getCell(row, 4).add(new Paragraph("2,4")); -table.getRow(row).mergeCells(4, 5); -table.getRow(row).mergeCells(1, 2); -row = 3; -table.getCell(row, 0).add(new Paragraph("3,0")); -table.getCell(row, 1).add(new Paragraph("3,1")); -table.getCell(row, 2).add(new Paragraph("3,2")); -table.getCell(row, 3).add(new Paragraph("3,3")); -table.getCell(row, 4).add(new Paragraph("3,4")); -table.getCell(row, 5).add(new Paragraph("3,5")); -row = 4; -table.getCell(row, 0).add(new Paragraph("4,0")); -table.getCell(row, 5).add(new Paragraph("4,5")); -table.getRow(row).mergeCells(0, 4); +const table2 = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("0,0")], + }), + new TableCell({ + children: [new Paragraph("0,1")], + rowSpan: 2, + }), + new TableCell({ + children: [new Paragraph("0,2")], + }), + new TableCell({ + children: [new Paragraph("0,3")], + }), + new TableCell({ + children: [new Paragraph("0,4")], + }), + new TableCell({ + children: [new Paragraph("0,5")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("1,0")], + }), + new TableCell({ + children: [new Paragraph("1,2")], + }), + new TableCell({ + children: [new Paragraph("1,3")], + }), + new TableCell({ + children: [new Paragraph("1,4")], + }), + new TableCell({ + children: [new Paragraph("1,5")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("2,0")], + }), + new TableCell({ + children: [new Paragraph("2,1")], + }), + new TableCell({ + children: [new Paragraph("2,2")], + }), + new TableCell({ + children: [new Paragraph("2,3")], + }), + new TableCell({ + children: [new Paragraph("2,4")], + }), + new TableCell({ + children: [new Paragraph("2,5")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("3,0")], + }), + new TableCell({ + children: [new Paragraph("3,1")], + }), + new TableCell({ + children: [new Paragraph("3,2")], + }), + new TableCell({ + children: [new Paragraph("3,3")], + }), + new TableCell({ + children: [new Paragraph("3,4")], + }), + new TableCell({ + children: [new Paragraph("3,5")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("4,0")], + }), + new TableCell({ + children: [new Paragraph("4,1")], + }), + new TableCell({ + children: [new Paragraph("4,2")], + }), + new TableCell({ + children: [new Paragraph("4,3")], + }), + new TableCell({ + children: [new Paragraph("4,4")], + }), + new TableCell({ + children: [new Paragraph("4,5")], + }), + ], + }), + new TableRow({ + children: [ + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + new TableCell({ + children: [], + }), + ], + }), + ], +}); doc.addSection({ - children: [table], + children: [table, new Paragraph(""), table2], }); Packer.toBuffer(doc).then((buffer) => { diff --git a/demo/43-images-to-table-cell-2.ts b/demo/43-images-to-table-cell-2.ts index 90d6322bb7..ab950d284c 100644 --- a/demo/43-images-to-table-cell-2.ts +++ b/demo/43-images-to-table-cell-2.ts @@ -1,18 +1,80 @@ // Add image to table cell // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph, Table } from "../build"; +import { Document, Packer, Paragraph, Table, TableCell, TableRow } from "../build"; const doc = new Document(); const table = new Table({ - rows: 4, - columns: 4, + 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 TableCell({ + children: [], + }), + new TableCell({ + children: [], + rowSpan: 2, + }), + ], + }), + 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: [], + }), + ], + }), + ], }); -table.getCell(2, 2).add(new Paragraph("Hello")); -table.getColumn(3).mergeCells(1, 2); - doc.addSection({ children: [table], }); diff --git a/demo/44-multiple-columns.ts b/demo/44-multiple-columns.ts index 3fdf328461..3985486620 100644 --- a/demo/44-multiple-columns.ts +++ b/demo/44-multiple-columns.ts @@ -8,7 +8,7 @@ const doc = new Document(); doc.addSection({ properties: { column: { - width: 708, + space: 708, count: 2, }, }, @@ -23,7 +23,7 @@ doc.addSection({ doc.addSection({ properties: { column: { - width: 708, + space: 708, count: 3, }, }, diff --git a/demo/47-number-of-total-pages-section.ts b/demo/47-number-of-total-pages-section.ts new file mode 100644 index 0000000000..988206e524 --- /dev/null +++ b/demo/47-number-of-total-pages-section.ts @@ -0,0 +1,63 @@ +// Multiple sections with total number of pages in each section +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { AlignmentType, Document, Packer, PageNumberFormat, TextRun, Header, Paragraph, Footer, PageBreak } from "../build"; + +const doc = new Document(); + +const header = new Header({ + children: [ + new Paragraph({ + children: [ + new TextRun("Header on another page"), + new TextRun("Page Number: ").pageNumber(), + new TextRun(" to ").numberOfTotalPagesSection(), + ], + alignment: AlignmentType.CENTER, + }), + ], +}); + +const 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()], + }), + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/browser-demo.html b/demo/browser-demo.html index 22c7c821a5..82f064deb2 100644 --- a/demo/browser-demo.html +++ b/demo/browser-demo.html @@ -12,18 +12,18 @@