diff --git a/.editorconfig b/.editorconfig
index 9b7352176a..46c1eafce3 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -7,6 +7,7 @@ indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
+end_of_line = lf
[*.md]
max_line_length = off
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/.github/workflows/default.yml b/.github/workflows/default.yml
new file mode 100644
index 0000000000..a063d1ee1d
--- /dev/null
+++ b/.github/workflows/default.yml
@@ -0,0 +1,104 @@
+name: Default
+on:
+ push:
+ branches:
+ - master
+ pull_request:
+ branches:
+ - master
+
+jobs:
+ build:
+ name: Build
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout Repo
+ uses: actions/checkout@master
+ - name: Install Dependencies
+ run: npm ci
+ - name: Build
+ run: npm run build
+ - name: Archive Production Artifact
+ uses: actions/upload-artifact@master
+ with:
+ name: build
+ path: build
+ test:
+ name: Test
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout Repo
+ uses: actions/checkout@master
+ - name: Install Dependencies
+ run: npm ci
+ - name: Test
+ run: npm run test.coverage
+ lint:
+ name: Lint
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout Repo
+ uses: actions/checkout@master
+ - name: Install Dependencies
+ run: npm ci
+ - name: Lint
+ run: npm run lint
+ prettier:
+ name: Prettier
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout Repo
+ uses: actions/checkout@master
+ - name: Install Dependencies
+ run: npm ci
+ - name: Prettier
+ run: npm run style
+ demos:
+ name: Run Demos
+ needs: [build]
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout Repo
+ uses: actions/checkout@master
+ - name: Install Dependencies
+ run: npm ci
+ - name: Download Artifact
+ uses: actions/download-artifact@master
+ with:
+ name: build
+ path: build
+ - name: Run demos
+ run: |
+ npm run ts-node -- ./demo/1-basic.ts
+ npm run ts-node -- ./demo/2-declaritive-styles.ts
+ npm run ts-node -- ./demo/3-numbering-and-bullet-points.ts
+ npm run ts-node -- ./demo/4-basic-table.ts
+ npm run ts-node -- ./demo/5-images.ts
+ npm run ts-node -- ./demo/6-page-borders.ts
+ npm run ts-node -- ./demo/7-landscape.ts
+ npm run ts-node -- ./demo/8-header-footer.ts
+ npm run ts-node -- ./demo/9-images-in-header-and-footer.ts
+ npm run ts-node -- ./demo/10-my-cv.ts
+ npm run ts-node -- ./demo/11-declaritive-styles-2.ts
+ npm run ts-node -- ./demo/12-scaling-images.ts
+ npm run ts-node -- ./demo/13-xml-styles.ts
+ npm run ts-node -- ./demo/14-page-numbers.ts
+ npm run ts-node -- ./demo/15-page-break-before.ts
+ npm run ts-node -- ./demo/16-multiple-sections.ts
+ npm run ts-node -- ./demo/17-footnotes.ts
+ npm run ts-node -- ./demo/18-image-from-buffer.ts
+ npm run ts-node -- ./demo/19-export-to-base64.ts
+ npm run ts-node -- ./demo/20-table-cell-borders.ts
+ npm run ts-node -- ./demo/21-bookmarks.ts
+ npm run ts-node -- ./demo/22-right-to-left-text.ts
+ npm run ts-node -- ./demo/23-base64-images.ts
+ npm run ts-node -- ./demo/24-images-to-table-cell.ts
+ npm run ts-node -- ./demo/26-paragraph-borders.ts
+ npm run ts-node -- ./demo/27-declaritive-styles-3.ts
+ npm run ts-node -- ./demo/28-table-of-contents.ts
+ 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-and-shade-table-cells.ts
+ npm run ts-node -- ./demo/33-sequential-captions.ts
+ npm run ts-node -- ./demo/34-floating-tables.ts
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/.nvmrc b/.nvmrc
index 469d080845..e338b86593 100644
--- a/.nvmrc
+++ b/.nvmrc
@@ -1 +1 @@
-v8
\ No newline at end of file
+v10
diff --git a/.nycrc b/.nycrc
index d5f12ccbc3..3a897e4c79 100644
--- a/.nycrc
+++ b/.nycrc
@@ -1,14 +1,15 @@
{
"check-coverage": true,
- "lines": 92.35,
- "functions": 88.28,
- "branches": 84.64,
- "statements": 92.16,
+ "lines": 97.77,
+ "functions": 93.89,
+ "branches": 94.83,
+ "statements": 97.75,
"include": [
"src/**/*.ts"
],
"exclude": [
- "src/**/*.spec.ts"
+ "src/**/*.spec.ts",
+ "src/import-dotx/import-dotx.ts"
],
"reporter": [
"lcov",
diff --git a/.travis.yml b/.travis.yml
index 3bf1b47690..3d4166c482 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,6 @@
language: node_js
node_js:
- - 9
+ - 10
install:
- npm install
- npm install -g codecov
@@ -10,7 +10,7 @@ script:
- npm run style
- npm run build
- npm run ts-node -- ./demo/1-basic.ts
- - npm run e2e "My Document.docx"
+# - npm run e2e "My Document.docx"
- npm run ts-node -- ./demo/2-declaritive-styles.ts
- npm run ts-node -- ./demo/3-numbering-and-bullet-points.ts
- npm run ts-node -- ./demo/4-basic-table.ts
@@ -20,7 +20,7 @@ script:
- npm run ts-node -- ./demo/8-header-footer.ts
- npm run ts-node -- ./demo/9-images-in-header-and-footer.ts
- npm run ts-node -- ./demo/10-my-cv.ts
- - npm run e2e "My Document.docx"
+# - npm run e2e "My Document.docx"
- npm run ts-node -- ./demo/11-declaritive-styles-2.ts
- npm run ts-node -- ./demo/12-scaling-images.ts
- npm run ts-node -- ./demo/13-xml-styles.ts
diff --git a/README.md b/README.md
index 7cb5c93879..1df764e12c 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,7 @@
[![NPM version][npm-image]][npm-url]
[![Downloads per month][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
+[![GitHub Action Workflow Status][github-actions-workflow-image]][github-actions-workflow-url]
[![Dependency Status][daviddm-image]][daviddm-url]
[![Known Vulnerabilities][snky-image]][snky-url]
[![Chat on Gitter][gitter-image]][gitter-url]
@@ -18,7 +19,7 @@
[![codecov][codecov-image]][codecov-url]
-
+
# Demo
@@ -27,13 +28,22 @@
Here are examples of `docx` being used with basic `HTML/JS` in a browser environment:
-* https://codepen.io/anon/pen/dqoVgQ
-* https://jsfiddle.net/3xhezb5w/2
+* https://codepen.io/dolanmiu/pen/RwNeObg
+* https://jsfiddle.net/dolanmiu/kqxrj35u/1/
Here is an example of `docx` working in `Angular`:
* https://stackblitz.com/edit/angular-afvxtz
+Here is an example of `docx` working in `React`:
+
+* https://stackblitz.com/edit/react-ts-qq25sp
+* https://stackblitz.com/edit/react-ts-qdqu7z (adding images to Word Document)
+
+Here is an example of `docx` working in `Vue.js`:
+
+* https://stackblitz.com/edit/vuejs-docx
+
## Node
Press `endpoint` on the `RunKit` website:
@@ -50,7 +60,7 @@ Press `endpoint` on the `RunKit` website:
* https://runkit.com/dolanmiu/docx-demo8 - Header and Footer
* https://runkit.com/dolanmiu/docx-demo10 - **My CV generated with docx**
-More [here](https://docx.js.org/#/examples) and [here](https://github.com/dolanmiu/docx/tree/master/demo)
+More [here](https://github.com/dolanmiu/docx/tree/master/demo)
# How to use & Documentation
@@ -58,7 +68,7 @@ Please refer to the [documentation at https://docx.js.org/](https://docx.js.org/
# Examples
-Check the `examples` section in the [documentation](https://docx.js.org/#/examples) and the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for examples.
+Check the [demo folder](https://github.com/dolanmiu/docx/tree/master/demo) for examples.
# Contributing
@@ -73,6 +83,12 @@ Read the contribution guidelines [here](https://docx.js.org/#/contribution-guide
[
](https://www.dabblewriter.com/)
[
](https://turbopatent.com/)
[
](http://www.madisoncres.com/)
+[
](https://www.beekast.com/)
+[
](https://herraizsoto.com/)
+[
](http://www.ativer.com.br/)
+[
](https://www.arity.co/)
+[
](https://www.circadianrisk.com/)
+
...and many more!
@@ -89,6 +105,8 @@ Made with 💖
[downloads-url]: https://npmjs.org/package/docx
[travis-image]: https://travis-ci.org/dolanmiu/docx.svg?branch=master
[travis-url]: https://travis-ci.org/dolanmiu/docx
+[github-actions-workflow-image]: https://github.com/dolanmiu/docx/workflows/Default/badge.svg
+[github-actions-workflow-url]: https://github.com/dolanmiu/docx/actions
[daviddm-image]: https://david-dm.org/dolanmiu/docx.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/dolanmiu/docx
[snky-image]: https://snyk.io/test/github/dolanmiu/docx/badge.svg
diff --git a/demo/1-basic.ts b/demo/1-basic.ts
index 7db8870259..eb417f52b4 100644
--- a/demo/1-basic.ts
+++ b/demo/1-basic.ts
@@ -16,9 +16,9 @@ doc.addSection({
bold: true,
}),
new TextRun({
- text: "Github is the best",
+ text: "\tGithub is the best",
bold: true,
- }).tab(),
+ }),
],
}),
],
diff --git a/demo/10-my-cv.ts b/demo/10-my-cv.ts
index b3e7c55e01..484a9c249c 100644
--- a/demo/10-my-cv.ts
+++ b/demo/10-my-cv.ts
@@ -204,7 +204,10 @@ class DocumentCreator {
alignment: AlignmentType.CENTER,
children: [
new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`),
- new TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break(),
+ new TextRun({
+ text: "Address: 58 Elm Avenue, Kent ME4 6ER, UK",
+ break: 1,
+ }),
],
});
}
@@ -238,9 +241,9 @@ class DocumentCreator {
bold: true,
}),
new TextRun({
- text: dateText,
+ text: `\t${dateText}`,
bold: true,
- }).tab(),
+ }),
],
});
}
diff --git a/demo/11-declaritive-styles-2.ts b/demo/11-declaritive-styles-2.ts
index 3a0154a0fb..2f4ed78098 100644
--- a/demo/11-declaritive-styles-2.ts
+++ b/demo/11-declaritive-styles-2.ts
@@ -3,6 +3,7 @@
import * as fs from "fs";
import {
AlignmentType,
+ convertInchesToTwip,
Document,
Footer,
HeadingLevel,
@@ -18,13 +19,8 @@ import {
const doc = new Document({
styles: {
- paragraphStyles: [
- {
- id: "Heading1",
- name: "Heading 1",
- basedOn: "Normal",
- next: "Normal",
- quickFormat: true,
+ default: {
+ heading1: {
run: {
font: "Calibri",
size: 52,
@@ -40,12 +36,7 @@ const doc = new Document({
spacing: { line: 340 },
},
},
- {
- id: "Heading2",
- name: "Heading 2",
- basedOn: "Normal",
- next: "Normal",
- quickFormat: true,
+ heading2: {
run: {
font: "Calibri",
size: 26,
@@ -55,12 +46,7 @@ const doc = new Document({
spacing: { line: 340 },
},
},
- {
- id: "Heading3",
- name: "Heading 3",
- basedOn: "Normal",
- next: "Normal",
- quickFormat: true,
+ heading3: {
run: {
font: "Calibri",
size: 26,
@@ -70,12 +56,7 @@ const doc = new Document({
spacing: { line: 276 },
},
},
- {
- id: "Heading4",
- name: "Heading 4",
- basedOn: "Normal",
- next: "Normal",
- quickFormat: true,
+ heading4: {
run: {
font: "Calibri",
size: 26,
@@ -85,6 +66,8 @@ const doc = new Document({
alignment: AlignmentType.JUSTIFIED,
},
},
+ },
+ paragraphStyles: [
{
id: "normalPara",
name: "Normal Para",
@@ -128,7 +111,7 @@ const doc = new Document({
},
paragraph: {
spacing: { line: 276 },
- indent: { left: 720 },
+ indent: { left: convertInchesToTwip(0.5) },
},
},
{
@@ -139,12 +122,6 @@ const doc = new Document({
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
},
},
- {
- id: "ListParagraph",
- name: "List Paragraph",
- basedOn: "Normal",
- quickFormat: true,
- },
],
},
});
diff --git a/demo/14-page-numbers.ts b/demo/14-page-numbers.ts
index ae34300b5c..00b77cfe31 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, PageBreak, Paragraph, TextRun } from "../build";
+import { AlignmentType, Document, Header, Packer, PageBreak, PageNumber, Paragraph, TextRun } from "../build";
const doc = new Document();
@@ -11,7 +11,12 @@ doc.addSection({
children: [
new Paragraph({
alignment: AlignmentType.RIGHT,
- children: [new TextRun("My Title "), new TextRun("Page ").pageNumber()],
+ children: [
+ new TextRun("My Title "),
+ new TextRun({
+ children: ["Page ", PageNumber.CURRENT],
+ }),
+ ],
}),
],
}),
@@ -19,7 +24,12 @@ doc.addSection({
children: [
new Paragraph({
alignment: AlignmentType.RIGHT,
- children: [new TextRun("First Page Header "), new TextRun("Page ").pageNumber()],
+ children: [
+ new TextRun("First Page Header "),
+ new TextRun({
+ children: ["Page ", PageNumber.CURRENT],
+ }),
+ ],
}),
],
}),
diff --git a/demo/16-multiple-sections.ts b/demo/16-multiple-sections.ts
index 5518eb5416..6f2e350b23 100644
--- a/demo/16-multiple-sections.ts
+++ b/demo/16-multiple-sections.ts
@@ -1,7 +1,7 @@
// Multiple sections and headers
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
-import { Document, Footer, Header, Packer, PageNumberFormat, PageOrientation, Paragraph, TextRun } from "../build";
+import { Document, Footer, Header, Packer, PageNumber, PageNumberFormat, PageOrientation, Paragraph, TextRun } from "../build";
const doc = new Document();
@@ -53,7 +53,11 @@ doc.addSection({
default: new Header({
children: [
new Paragraph({
- children: [new TextRun("Page number: ").pageNumber()],
+ children: [
+ new TextRun({
+ children: ["Page number: ", PageNumber.CURRENT],
+ }),
+ ],
}),
],
}),
@@ -69,7 +73,11 @@ doc.addSection({
default: new Header({
children: [
new Paragraph({
- children: [new TextRun("Page number: ").pageNumber()],
+ children: [
+ new TextRun({
+ children: ["Page number: ", PageNumber.CURRENT],
+ }),
+ ],
}),
],
}),
@@ -90,7 +98,11 @@ doc.addSection({
default: new Header({
children: [
new Paragraph({
- children: [new TextRun("Page number: ").pageNumber()],
+ children: [
+ new TextRun({
+ children: ["Page number: ", PageNumber.CURRENT],
+ }),
+ ],
}),
],
}),
diff --git a/demo/17-footnotes.ts b/demo/17-footnotes.ts
index 3920d0dce8..a41f84a3c8 100644
--- a/demo/17-footnotes.ts
+++ b/demo/17-footnotes.ts
@@ -1,16 +1,54 @@
// 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, FootnoteReferenceRun, Packer, Paragraph, TextRun } from "../build";
-const doc = new Document();
-
-doc.addSection({
- children: [new Paragraph("Hello World").referenceFootnote(1), new Paragraph("Hello World").referenceFootnote(2)],
+const doc = new Document({
+ footnotes: [
+ new Paragraph("Foo"),
+ new Paragraph("Test"),
+ new Paragraph("My amazing reference"),
+ new Paragraph("Foo1"),
+ new Paragraph("Test1"),
+ new Paragraph("My amazing reference1"),
+ ],
});
-doc.createFootnote(new Paragraph("Test"));
-doc.createFootnote(new Paragraph("My amazing reference"));
+doc.addSection({
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun({
+ children: ["Hello", new FootnoteReferenceRun(1)],
+ }),
+ new TextRun({
+ children: [" World!", new FootnoteReferenceRun(2)],
+ }),
+ ],
+ }),
+ new Paragraph({
+ children: [new TextRun("Hello World"), new FootnoteReferenceRun(3)],
+ }),
+ ],
+});
+
+doc.addSection({
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun({
+ children: ["Hello", new FootnoteReferenceRun(4)],
+ }),
+ new TextRun({
+ children: [" World!", new FootnoteReferenceRun(5)],
+ }),
+ ],
+ }),
+ new Paragraph({
+ children: [new TextRun("Hello World"), new FootnoteReferenceRun(6)],
+ }),
+ ],
+});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
diff --git a/demo/19-export-to-base64.ts b/demo/19-export-to-base64.ts
index 676d844c8d..1d94cf5ff7 100644
--- a/demo/19-export-to-base64.ts
+++ b/demo/19-export-to-base64.ts
@@ -15,9 +15,9 @@ doc.addSection({
bold: true,
}),
new TextRun({
- text: "Bar",
+ text: "\tBar",
bold: true,
- }).tab(),
+ }),
],
}),
],
diff --git a/demo/2-declaritive-styles.ts b/demo/2-declaritive-styles.ts
index eaf39daffe..deada8198a 100644
--- a/demo/2-declaritive-styles.ts
+++ b/demo/2-declaritive-styles.ts
@@ -1,24 +1,30 @@
// 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, UnderlineType } from "../build";
+import {
+ AlignmentType,
+ convertInchesToTwip,
+ Document,
+ HeadingLevel,
+ LevelFormat,
+ 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,
+ default: {
+ heading1: {
run: {
size: 28,
bold: true,
italics: true,
+ color: "red",
},
paragraph: {
spacing: {
@@ -26,12 +32,7 @@ const doc = new Document({
},
},
},
- {
- id: "Heading2",
- name: "Heading 2",
- basedOn: "Normal",
- next: "Normal",
- quickFormat: true,
+ heading2: {
run: {
size: 26,
bold: true,
@@ -47,6 +48,13 @@ const doc = new Document({
},
},
},
+ listParagraph: {
+ run: {
+ color: "#FF0000",
+ },
+ },
+ },
+ paragraphStyles: [
{
id: "aside",
name: "Aside",
@@ -58,7 +66,7 @@ const doc = new Document({
},
paragraph: {
indent: {
- left: 720,
+ left: convertInchesToTwip(0.5),
},
spacing: {
line: 276,
@@ -74,23 +82,25 @@ const doc = new Document({
spacing: { line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 },
},
},
+ ],
+ },
+ numbering: {
+ config: [
{
- id: "ListParagraph",
- name: "List Paragraph",
- basedOn: "Normal",
- quickFormat: true,
+ reference: "my-crazy-numbering",
+ levels: [
+ {
+ level: 0,
+ format: LevelFormat.LOWER_LETTER,
+ text: "%1)",
+ alignment: AlignmentType.LEFT,
+ },
+ ],
},
],
},
});
-const numberedAbstract = doc.Numbering.createAbstractNumbering();
-numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");
-
-const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract);
-const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract);
-letterNumbering5.overrideLevel(0, 5);
-
doc.addSection({
children: [
new Paragraph({
@@ -105,21 +115,21 @@ doc.addSection({
new Paragraph({
text: "Option1",
numbering: {
- num: letterNumbering,
+ reference: "my-crazy-numbering",
level: 0,
},
}),
new Paragraph({
text: "Option5 -- override 2 to 5",
numbering: {
- num: letterNumbering,
+ reference: "my-crazy-numbering",
level: 0,
},
}),
new Paragraph({
text: "Option3",
numbering: {
- num: letterNumbering,
+ reference: "my-crazy-numbering",
level: 0,
},
}),
@@ -152,11 +162,26 @@ doc.addSection({
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.",
+ }),
+ ],
+ }),
],
});
diff --git a/demo/21-bookmarks.ts b/demo/21-bookmarks.ts
index cc0bac5517..632fcb212f 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, PageBreak, Paragraph } from "../build";
+import { Bookmark, Document, Footer, HeadingLevel, InternalHyperlink, Packer, PageBreak, Paragraph, TextRun } 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.";
@@ -12,17 +12,28 @@ const doc = new Document({
description: "A brief example of using docx with bookmarks and internal hyperlinks",
});
-const anchorId = "anchorID";
-
-// First create the bookmark
-const bookmark = doc.createBookmark(anchorId, "Lorem Ipsum");
-const hyperlink = doc.createInternalHyperLink(anchorId, `Click me!`);
-
doc.addSection({
+ 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: [bookmark],
+ children: [new Bookmark("myAnchorId", "Lorem Ipsum")],
}),
new Paragraph("\n"),
new Paragraph(LOREM_IPSUM),
@@ -30,7 +41,15 @@ doc.addSection({
children: [new PageBreak()],
}),
new Paragraph({
- children: [hyperlink],
+ 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 2c3984fb46..4ee344391c 100644
--- a/demo/22-right-to-left-text.ts
+++ b/demo/22-right-to-left-text.ts
@@ -1,7 +1,7 @@
// This demo shows right to left for special languages
// 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, Table, TableCell, TableRow, TextRun } from "../build";
const doc = new Document();
@@ -36,6 +36,31 @@ doc.addSection({
}),
],
}),
+ 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/27-declaritive-styles-3.ts b/demo/27-declaritive-styles-3.ts
index 8419467799..8737c69101 100644
--- a/demo/27-declaritive-styles-3.ts
+++ b/demo/27-declaritive-styles-3.ts
@@ -1,7 +1,7 @@
// 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, UnderlineType } from "../build";
+import { Document, convertInchesToTwip, HeadingLevel, Packer, Paragraph, UnderlineType } from "../build";
const doc = new Document({
styles: {
@@ -17,7 +17,7 @@ const doc = new Document({
},
paragraph: {
indent: {
- left: 720,
+ left: convertInchesToTwip(0.5),
},
spacing: {
line: 276,
diff --git a/demo/29-numbered-lists.ts b/demo/29-numbered-lists.ts
index 740320e5f8..77f50381d1 100644
--- a/demo/29-numbered-lists.ts
+++ b/demo/29-numbered-lists.ts
@@ -1,23 +1,69 @@
// Numbered lists
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
-import { Document, Numbering, Packer, Paragraph } from "../build";
+import { AlignmentType, convertInchesToTwip, Document, LevelFormat, Packer, Paragraph } from "../build";
-const doc = new Document();
-
-const numbering = new Numbering();
-
-const abstractNum = numbering.createAbstractNumbering();
-abstractNum.createLevel(0, "upperRoman", "%1", "start").indent({ left: 720, hanging: 260 });
-
-const concrete = numbering.createConcreteNumbering(abstractNum);
+const doc = new Document({
+ numbering: {
+ config: [
+ {
+ levels: [
+ {
+ level: 0,
+ format: LevelFormat.UPPER_ROMAN,
+ text: "%1",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.18) },
+ },
+ },
+ },
+ ],
+ reference: "my-crazy-reference",
+ },
+ {
+ levels: [
+ {
+ level: 0,
+ format: LevelFormat.DECIMAL,
+ text: "%1",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.18) },
+ },
+ },
+ },
+ ],
+ reference: "my-number-numbering-reference",
+ },
+ {
+ levels: [
+ {
+ level: 0,
+ format: LevelFormat.DECIMAL_ZERO,
+ text: "[%1]",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.18) },
+ },
+ },
+ },
+ ],
+ reference: "padded-numbering-reference",
+ },
+ ],
+ },
+});
doc.addSection({
children: [
new Paragraph({
text: "line with contextual spacing",
numbering: {
- num: concrete,
+ reference: "my-crazy-reference",
level: 0,
},
contextualSpacing: true,
@@ -28,7 +74,7 @@ doc.addSection({
new Paragraph({
text: "line with contextual spacing",
numbering: {
- num: concrete,
+ reference: "my-crazy-reference",
level: 0,
},
contextualSpacing: true,
@@ -39,7 +85,7 @@ doc.addSection({
new Paragraph({
text: "line without contextual spacing",
numbering: {
- num: concrete,
+ reference: "my-crazy-reference",
level: 0,
},
contextualSpacing: false,
@@ -50,7 +96,7 @@ doc.addSection({
new Paragraph({
text: "line without contextual spacing",
numbering: {
- num: concrete,
+ reference: "my-crazy-reference",
level: 0,
},
contextualSpacing: false,
@@ -58,6 +104,160 @@ doc.addSection({
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: "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,
+ },
+ }),
+ 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 e87e6616d2..375c1439a7 100644
--- a/demo/3-numbering-and-bullet-points.ts
+++ b/demo/3-numbering-and-bullet-points.ts
@@ -1,46 +1,91 @@
// Numbering and bullet points example
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
-import { Document, Numbering, Packer, Paragraph } from "../build";
+import { AlignmentType, convertInchesToTwip, Document, LevelFormat, Packer, Paragraph } from "../build";
-const doc = new Document();
-
-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: 2160, hanging: 1700 });
-
-const concrete = numbering.createConcreteNumbering(abstractNum);
+const doc = new Document({
+ numbering: {
+ config: [
+ {
+ reference: "my-crazy-numbering",
+ levels: [
+ {
+ level: 0,
+ format: LevelFormat.UPPER_ROMAN,
+ text: "%1",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.18) },
+ },
+ },
+ },
+ {
+ level: 1,
+ format: LevelFormat.DECIMAL,
+ text: "%2.",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: convertInchesToTwip(1), hanging: convertInchesToTwip(0.68) },
+ },
+ },
+ },
+ {
+ level: 2,
+ format: LevelFormat.LOWER_LETTER,
+ text: "%3)",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: convertInchesToTwip(1.5), hanging: convertInchesToTwip(1.18) },
+ },
+ },
+ },
+ {
+ level: 3,
+ format: LevelFormat.UPPER_LETTER,
+ text: "%4)",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: 2880, hanging: 2420 },
+ },
+ },
+ },
+ ],
+ },
+ ],
+ },
+});
doc.addSection({
children: [
new Paragraph({
text: "Hey you",
numbering: {
- num: concrete,
+ reference: "my-crazy-numbering",
level: 0,
},
}),
new Paragraph({
text: "What's up fam",
numbering: {
- num: concrete,
+ reference: "my-crazy-numbering",
level: 1,
},
}),
new Paragraph({
text: "Hello World 2",
numbering: {
- num: concrete,
+ reference: "my-crazy-numbering",
level: 1,
},
}),
new Paragraph({
text: "Yeah boi",
numbering: {
- num: concrete,
+ reference: "my-crazy-numbering",
level: 2,
},
}),
@@ -68,6 +113,27 @@ doc.addSection({
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,
+ },
+ }),
],
});
diff --git a/demo/31-tables.ts b/demo/31-tables.ts
index 7daec1506d..24fc6030f8 100644
--- a/demo/31-tables.ts
+++ b/demo/31-tables.ts
@@ -1,7 +1,7 @@
// 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, TableCell, TableRow, VerticalAlign } from "../build";
+import { Document, HeadingLevel, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign, TextDirection } from "../build";
const doc = new Document();
@@ -17,6 +17,14 @@ const table = new Table({
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({
@@ -38,6 +46,22 @@ const table = new Table({
],
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,
+ }),
],
}),
],
diff --git a/demo/32-merge-and-shade-table-cells.ts b/demo/32-merge-and-shade-table-cells.ts
index 77d6815d1e..284b456e05 100644
--- a/demo/32-merge-and-shade-table-cells.ts
+++ b/demo/32-merge-and-shade-table-cells.ts
@@ -1,7 +1,21 @@
// Example of how you would merge cells together (Rows and Columns) and apply shading
+// Also includes an example on how to center tables
// 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";
+import {
+ AlignmentType,
+ BorderStyle,
+ convertInchesToTwip,
+ Document,
+ HeadingLevel,
+ Packer,
+ Paragraph,
+ ShadingType,
+ Table,
+ TableCell,
+ TableRow,
+ WidthType,
+} from "../build";
const doc = new Document();
@@ -29,16 +43,17 @@ const table = new Table({
});
const table2 = new Table({
+ alignment: AlignmentType.CENTER,
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("World")],
margins: {
- top: 1000,
- bottom: 1000,
- left: 1000,
- right: 1000,
+ top: convertInchesToTwip(0.69),
+ bottom: convertInchesToTwip(0.69),
+ left: convertInchesToTwip(0.69),
+ right: convertInchesToTwip(0.69),
},
columnSpan: 3,
}),
@@ -62,10 +77,11 @@ const table2 = new Table({
size: 100,
type: WidthType.AUTO,
},
- columnWidths: [1000, 1000, 1000],
+ columnWidths: [convertInchesToTwip(0.69), convertInchesToTwip(0.69), convertInchesToTwip(0.69)],
});
const table3 = new Table({
+ alignment: AlignmentType.CENTER,
rows: [
new TableRow({
children: [
@@ -116,14 +132,14 @@ const table3 = new Table({
}),
],
width: {
- size: 7000,
+ size: convertInchesToTwip(4.86),
type: WidthType.DXA,
},
margins: {
- top: 400,
- bottom: 400,
- right: 400,
- left: 400,
+ top: convertInchesToTwip(0.27),
+ bottom: convertInchesToTwip(0.27),
+ right: convertInchesToTwip(0.27),
+ left: convertInchesToTwip(0.27),
},
});
@@ -181,7 +197,7 @@ const table5 = new Table({
new TableRow({
children: [
new TableCell({
- children: [],
+ children: [new Paragraph("1,0")],
}),
new TableCell({
children: [new Paragraph("1,2")],
@@ -192,10 +208,10 @@ const table5 = new Table({
new TableRow({
children: [
new TableCell({
- children: [],
+ children: [new Paragraph("2,0")],
}),
new TableCell({
- children: [],
+ children: [new Paragraph("2,1")],
}),
],
}),
@@ -206,6 +222,161 @@ const table5 = new Table({
},
});
+const borders = {
+ top: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+ bottom: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+ left: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+ right: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+};
+
+const table6 = new Table({
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({
+ borders,
+ children: [new Paragraph("0,0")],
+ rowSpan: 2,
+ }),
+ new TableCell({
+ borders,
+ children: [new Paragraph("0,1")],
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ borders,
+ children: [new Paragraph("1,1")],
+ rowSpan: 2,
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ borders,
+ children: [new Paragraph("2,0")],
+ }),
+ ],
+ }),
+ ],
+ width: {
+ size: 100,
+ type: WidthType.PERCENTAGE,
+ },
+});
+
+const table7 = new Table({
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({
+ children: [new Paragraph("0,0")],
+ }),
+ new TableCell({
+ children: [new Paragraph("0,1")],
+ }),
+ new TableCell({
+ children: [new Paragraph("0,2")],
+ rowSpan: 2,
+ }),
+ new TableCell({
+ children: [new Paragraph("0,3")],
+ rowSpan: 3,
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ children: [new Paragraph("1,0")],
+ columnSpan: 2,
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ children: [new Paragraph("2,0")],
+ columnSpan: 2,
+ }),
+ new TableCell({
+ children: [new Paragraph("2,2")],
+ rowSpan: 2,
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ children: [new Paragraph("3,0")],
+ }),
+ new TableCell({
+ children: [new Paragraph("3,1")],
+ }),
+ new TableCell({
+ children: [new Paragraph("3,3")],
+ }),
+ ],
+ }),
+ ],
+ width: {
+ size: 100,
+ type: WidthType.PERCENTAGE,
+ },
+});
+
+const table8 = new Table({
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({ children: [new Paragraph("1,1")] }),
+ new TableCell({ children: [new Paragraph("1,2")] }),
+ new TableCell({ children: [new Paragraph("1,3")] }),
+ new TableCell({ children: [new Paragraph("1,4")], rowSpan: 4, borders }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({ children: [new Paragraph("2,1")] }),
+ new TableCell({ children: [new Paragraph("2,2")] }),
+ new TableCell({ children: [new Paragraph("2,3")], rowSpan: 3 }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({ children: [new Paragraph("3,1")] }),
+ new TableCell({ children: [new Paragraph("3,2")], rowSpan: 2 }),
+ ],
+ }),
+ new TableRow({
+ children: [new TableCell({ children: [new Paragraph("4,1")] })],
+ }),
+ ],
+ width: {
+ size: 100,
+ type: WidthType.PERCENTAGE,
+ },
+});
+
doc.addSection({
children: [
table,
@@ -219,10 +390,16 @@ doc.addSection({
heading: HeadingLevel.HEADING_2,
}),
table3,
- new Paragraph("Merging columns"),
+ new Paragraph("Merging columns 1"),
table4,
- new Paragraph("More Merging columns"),
+ 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/34-floating-tables.ts b/demo/34-floating-tables.ts
index dadf80f9da..aa8128de7e 100644
--- a/demo/34-floating-tables.ts
+++ b/demo/34-floating-tables.ts
@@ -3,6 +3,7 @@
import * as fs from "fs";
import {
Document,
+ OverlapType,
Packer,
Paragraph,
RelativeHorizontalPosition,
@@ -43,6 +44,7 @@ const table = new Table({
verticalAnchor: TableAnchorType.MARGIN,
relativeHorizontalPosition: RelativeHorizontalPosition.RIGHT,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
+ overlap: OverlapType.NEVER,
},
width: {
size: 4535,
diff --git a/demo/35-hyperlinks.ts b/demo/35-hyperlinks.ts
index 6392299b84..3e45e4c342 100644
--- a/demo/35-hyperlinks.ts
+++ b/demo/35-hyperlinks.ts
@@ -1,15 +1,91 @@
// Example on how to add hyperlinks to websites
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
-import { Document, Packer, Paragraph } from "../build";
+import { Document, ExternalHyperlink, Footer, FootnoteReferenceRun, Media, Packer, Paragraph, TextRun } from "../build";
-const doc = new Document();
-const link = doc.createHyperlink("http://www.example.com", "Hyperlink");
+const doc = new Document({
+ footnotes: [
+ new Paragraph({
+ children: [
+ new TextRun("Click here for the "),
+ new ExternalHyperlink({
+ child: new TextRun({
+ text: "Footnotes external hyperlink",
+ style: "Hyperlink",
+ }),
+ link: "http://www.example.com",
+ }),
+ ],
+ }),
+ ],
+});
+
+const image1 = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
doc.addSection({
+ 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: [link],
+ 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: image1,
+ 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/39-page-numbers.ts b/demo/39-page-numbers.ts
index edc9c411f9..2ab61419ef 100644
--- a/demo/39-page-numbers.ts
+++ b/demo/39-page-numbers.ts
@@ -1,7 +1,7 @@
// Example how to display page numbers
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
-import { AlignmentType, Document, Footer, Header, Packer, PageNumberFormat, Paragraph, TextRun } from "../build";
+import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build";
const doc = new Document({});
@@ -9,9 +9,17 @@ doc.addSection({
headers: {
default: new Header({
children: [
- new Paragraph("Foo Bar corp. ")
- .addRun(new TextRun("Page Number ").pageNumber())
- .addRun(new TextRun(" to ").numberOfTotalPages()),
+ new Paragraph({
+ children: [
+ new TextRun("Foo Bar corp. "),
+ new TextRun({
+ children: ["Page Number ", PageNumber.CURRENT],
+ }),
+ new TextRun({
+ children: [" to ", PageNumber.TOTAL_PAGES],
+ }),
+ ],
+ }),
],
}),
},
@@ -19,11 +27,17 @@ doc.addSection({
default: new Footer({
children: [
new Paragraph({
- text: "Foo Bar corp. ",
alignment: AlignmentType.CENTER,
- })
- .addRun(new TextRun("Page Number: ").pageNumber())
- .addRun(new TextRun(" to ").numberOfTotalPages()),
+ children: [
+ new TextRun("Foo Bar corp. "),
+ new TextRun({
+ children: ["Page Number: ", PageNumber.CURRENT],
+ }),
+ new TextRun({
+ children: [" to ", PageNumber.TOTAL_PAGES],
+ }),
+ ],
+ }),
],
}),
},
@@ -32,11 +46,21 @@ doc.addSection({
pageNumberFormatType: PageNumberFormat.DECIMAL,
},
children: [
- new Paragraph("Hello World 1").pageBreak(),
- new Paragraph("Hello World 2").pageBreak(),
- new Paragraph("Hello World 3").pageBreak(),
- new Paragraph("Hello World 4").pageBreak(),
- new Paragraph("Hello World 5").pageBreak(),
+ 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 593fe821a5..287e54808a 100644
--- a/demo/4-basic-table.ts
+++ b/demo/4-basic-table.ts
@@ -1,11 +1,95 @@
// 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, TableCell, TableRow } from "../build";
+import { Document, Packer, Paragraph, Table, TableCell, TableRow, WidthType } from "../build";
const doc = new Document();
const table = new Table({
+ columnWidths: [3505, 5505],
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({
+ width: {
+ size: 3505,
+ type: WidthType.DXA,
+ },
+ children: [new Paragraph("Hello")],
+ }),
+ new TableCell({
+ width: {
+ size: 5505,
+ type: WidthType.DXA,
+ },
+ children: [],
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ width: {
+ size: 3505,
+ type: WidthType.DXA,
+ },
+ children: [],
+ }),
+ new TableCell({
+ width: {
+ size: 5505,
+ type: WidthType.DXA,
+ },
+ children: [new Paragraph("World")],
+ }),
+ ],
+ }),
+ ],
+});
+
+const table2 = new Table({
+ columnWidths: [4505, 4505],
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({
+ width: {
+ size: 4505,
+ type: WidthType.DXA,
+ },
+ children: [new Paragraph("Hello")],
+ }),
+ new TableCell({
+ width: {
+ size: 4505,
+ type: WidthType.DXA,
+ },
+ children: [],
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ width: {
+ size: 4505,
+ type: WidthType.DXA,
+ },
+ children: [],
+ }),
+ new TableCell({
+ width: {
+ size: 4505,
+ type: WidthType.DXA,
+ },
+ children: [new Paragraph("World")],
+ }),
+ ],
+ }),
+ ],
+});
+
+const table3 = new Table({
rows: [
new TableRow({
children: [
@@ -31,7 +115,14 @@ const table = new Table({
});
doc.addSection({
- children: [table],
+ 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,
+ ],
});
Packer.toBuffer(doc).then((buffer) => {
diff --git a/demo/44-multiple-columns.ts b/demo/44-multiple-columns.ts
index 3985486620..6e01e874e7 100644
--- a/demo/44-multiple-columns.ts
+++ b/demo/44-multiple-columns.ts
@@ -35,6 +35,22 @@ doc.addSection({
],
});
+doc.addSection({
+ 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.",
+ ),
+ ],
+});
+
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
diff --git a/demo/46-shading-text.ts b/demo/46-shading-text.ts
index 61f3f3d984..e662e6dffe 100644
--- a/demo/46-shading-text.ts
+++ b/demo/46-shading-text.ts
@@ -28,6 +28,18 @@ doc.addSection({
}),
],
}),
+ new Paragraph({
+ shading: {
+ type: ShadingType.DIAGONAL_CROSS,
+ color: "00FFFF",
+ fill: "FF0000",
+ },
+ children: [
+ new TextRun({
+ text: "Hello World for entire paragraph",
+ }),
+ ],
+ }),
],
}),
},
diff --git a/demo/47-number-of-total-pages-section.ts b/demo/47-number-of-total-pages-section.ts
index 988206e524..7e8cf91b2c 100644
--- a/demo/47-number-of-total-pages-section.ts
+++ b/demo/47-number-of-total-pages-section.ts
@@ -1,7 +1,7 @@
// 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";
+import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build";
const doc = new Document();
@@ -10,8 +10,12 @@ const header = new Header({
new Paragraph({
children: [
new TextRun("Header on another page"),
- new TextRun("Page Number: ").pageNumber(),
- new TextRun(" to ").numberOfTotalPagesSection(),
+ new TextRun({
+ children: ["Page number: ", PageNumber.CURRENT],
+ }),
+ new TextRun({
+ children: [" to ", PageNumber.TOTAL_PAGES_IN_SECTION],
+ }),
],
alignment: AlignmentType.CENTER,
}),
diff --git a/demo/48-vertical-align.ts b/demo/48-vertical-align.ts
new file mode 100644
index 0000000000..35181d271c
--- /dev/null
+++ b/demo/48-vertical-align.ts
@@ -0,0 +1,31 @@
+// Example of making content of section vertically aligned
+// Import from 'docx' rather than '../build' if you install from npm
+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({
+ children: [
+ new TextRun("Hello World"),
+ new TextRun({
+ text: "Foo Bar",
+ bold: true,
+ }),
+ new TextRun({
+ text: "\tGithub is the best",
+ bold: true,
+ }),
+ ],
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/49-table-borders.ts b/demo/49-table-borders.ts
new file mode 100644
index 0000000000..75dd89c49f
--- /dev/null
+++ b/demo/49-table-borders.ts
@@ -0,0 +1,167 @@
+// Add custom borders and no-borders to the table itself
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import {
+ BorderStyle,
+ Document,
+ HeadingLevel,
+ Packer,
+ Paragraph,
+ Table,
+ TableBorders,
+ TableCell,
+ TableRow,
+ TextDirection,
+ VerticalAlign,
+} from "../build";
+
+const doc = new Document();
+
+const table = new Table({
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({
+ borders: {
+ top: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+ bottom: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+ left: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+ right: {
+ style: BorderStyle.DASH_SMALL_GAP,
+ size: 1,
+ color: "red",
+ },
+ },
+ children: [new Paragraph("Hello")],
+ }),
+ new TableCell({
+ children: [],
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ children: [],
+ }),
+ new TableCell({
+ children: [new Paragraph("World")],
+ }),
+ ],
+ }),
+ ],
+});
+
+// Using the no-border convenience object. It is the same as writing this manually:
+// const borders = {
+// top: {
+// style: BorderStyle.NONE,
+// size: 0,
+// color: "auto",
+// },
+// bottom: {
+// style: BorderStyle.NONE,
+// size: 0,
+// color: "auto",
+// },
+// left: {
+// style: BorderStyle.NONE,
+// size: 0,
+// color: "auto",
+// },
+// right: {
+// style: BorderStyle.NONE,
+// size: 0,
+// color: "auto",
+// },
+// insideHorizontal: {
+// style: BorderStyle.NONE,
+// size: 0,
+// color: "auto",
+// },
+// insideVertical: {
+// style: BorderStyle.NONE,
+// size: 0,
+// color: "auto",
+// },
+// };
+const noBorderTable = new Table({
+ borders: TableBorders.NONE,
+ 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,
+ }),
+ ],
+ }),
+ ],
+});
+
+doc.addSection({ 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 260fd25f15..3c752d9a58 100644
--- a/demo/5-images.ts
+++ b/demo/5-images.ts
@@ -22,6 +22,7 @@ const image4 = Media.addImage(doc, fs.readFileSync("./demo/images/parrots.bmp"))
const image5 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
const image6 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
floating: {
+ zIndex: 10,
horizontalPosition: {
offset: 1014400,
},
@@ -33,6 +34,7 @@ const image6 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"), 2
const image7 = Media.addImage(doc, fs.readFileSync("./demo/images/cat.jpg"), 200, 200, {
floating: {
+ zIndex: 5,
horizontalPosition: {
relative: HorizontalPositionRelativeFrom.PAGE,
align: HorizontalPositionAlign.RIGHT,
diff --git a/demo/50-readme-demo.ts b/demo/50-readme-demo.ts
new file mode 100644
index 0000000000..6b1cc0ecab
--- /dev/null
+++ b/demo/50-readme-demo.ts
@@ -0,0 +1,61 @@
+// Simple example to add text to a document
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, HeadingLevel, Media, Packer, Paragraph, Table, TableCell, TableRow, VerticalAlign } from "../build";
+
+const doc = new Document();
+
+const image1 = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
+const image2 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
+
+const table = new Table({
+ rows: [
+ new TableRow({
+ children: [
+ new TableCell({
+ children: [new Paragraph(image1)],
+ verticalAlign: VerticalAlign.CENTER,
+ }),
+ new TableCell({
+ children: [
+ new Paragraph({
+ text: "Hello",
+ heading: HeadingLevel.HEADING_1,
+ }),
+ ],
+ verticalAlign: VerticalAlign.CENTER,
+ }),
+ ],
+ }),
+ new TableRow({
+ children: [
+ new TableCell({
+ children: [
+ new Paragraph({
+ text: "World",
+ heading: HeadingLevel.HEADING_1,
+ }),
+ ],
+ }),
+ new TableCell({
+ children: [new Paragraph(image1)],
+ }),
+ ],
+ }),
+ ],
+});
+
+doc.addSection({
+ children: [
+ new Paragraph({
+ text: "Hello World",
+ heading: HeadingLevel.HEADING_1,
+ }),
+ table,
+ new Paragraph(image2),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/51-character-styles.ts b/demo/51-character-styles.ts
new file mode 100644
index 0000000000..a6064a4ddc
--- /dev/null
+++ b/demo/51-character-styles.ts
@@ -0,0 +1,57 @@
+// Custom character styles using JavaScript configuration
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document({
+ styles: {
+ characterStyles: [
+ {
+ id: "myRedStyle",
+ name: "My Wonky Style",
+ basedOn: "Normal",
+ run: {
+ color: "990000",
+ italics: true,
+ },
+ },
+ {
+ id: "strong",
+ name: "Strong",
+ basedOn: "Normal",
+ run: {
+ bold: true,
+ },
+ },
+ ],
+ },
+});
+
+doc.addSection({
+ children: [
+ 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.",
+ }),
+ ],
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/52-japanese.ts b/demo/52-japanese.ts
new file mode 100644
index 0000000000..5339c4b3a9
--- /dev/null
+++ b/demo/52-japanese.ts
@@ -0,0 +1,37 @@
+// Japanese text - Need to use a Japanese font
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, HeadingLevel, Packer, Paragraph } from "../build";
+
+const doc = new Document({
+ styles: {
+ paragraphStyles: [
+ {
+ id: "Normal",
+ name: "Normal",
+ basedOn: "Normal",
+ next: "Normal",
+ quickFormat: true,
+ run: {
+ font: "MS Gothic",
+ },
+ },
+ ],
+ },
+});
+
+doc.addSection({
+ children: [
+ new Paragraph({
+ text: "KFCを食べるのが好き",
+ heading: HeadingLevel.HEADING_1,
+ }),
+ new Paragraph({
+ text: "こんにちは",
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/53-chinese.ts b/demo/53-chinese.ts
new file mode 100644
index 0000000000..eee6eb9032
--- /dev/null
+++ b/demo/53-chinese.ts
@@ -0,0 +1,55 @@
+// Chinese text - Chinese text need to use a Chinese font. And ascii text need to use a ascii font.
+// Different from the `52-japanese.ts`.
+// `52-japanese.ts` will set all characters to use Japanese font.
+// `53-chinese.ts` will set Chinese characters to use Chinese font, and set ascii characters to use ascii font.
+
+// Note that if the OS have not install `KaiTi` font, this demo doesn't work.
+
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, HeadingLevel, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document({
+ styles: {
+ paragraphStyles: [
+ {
+ id: "Normal",
+ name: "Normal",
+ basedOn: "Normal",
+ next: "Normal",
+ quickFormat: true,
+ run: {
+ font: {
+ ascii: "Times",
+ eastAsia: "KaiTi",
+ },
+ },
+ },
+ ],
+ },
+});
+
+doc.addSection({
+ children: [
+ new Paragraph({
+ text: "中文和英文 Chinese and English",
+ 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
+ }),
+ ],
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/54-custom-properties.ts b/demo/54-custom-properties.ts
new file mode 100644
index 0000000000..4aad3864c9
--- /dev/null
+++ b/demo/54-custom-properties.ts
@@ -0,0 +1,30 @@
+// Custom Properties
+// Custom properties are incredibly useful if you want to be able to apply quick parts or custom cover pages
+// to the document in Word after the document has been generated. Standard properties (such as creator, title
+// and subject) cover typical use cases, but sometimes custom properties are required.
+
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer } from "../build";
+
+const doc = new Document(
+ // Standard properties
+ {
+ creator: "Creator",
+ title: "Title",
+ subject: "Subject",
+ description: "Description",
+ customProperties: [
+ { name: "Subtitle", value: "Subtitle" },
+ { name: "Address", value: "Address" },
+ ],
+ },
+ // No file properties
+ {},
+ // No sections
+ [],
+);
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/54-track-revisions.ts b/demo/54-track-revisions.ts
new file mode 100644
index 0000000000..e035fbb18a
--- /dev/null
+++ b/demo/54-track-revisions.ts
@@ -0,0 +1,146 @@
+// Track Revisions aka. "Track Changes"
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import {
+ AlignmentType,
+ DeletedTextRun,
+ Document,
+ Footer,
+ FootnoteReferenceRun,
+ InsertedTextRun,
+ Packer,
+ PageNumber,
+ Paragraph,
+ ShadingType,
+ TextRun,
+} from "../build";
+
+/*
+ For reference, see
+ - https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.insertedrun
+ - https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.deletedrun
+
+ The method `addTrackRevisions()` adds an element `` to the `settings.xml` file. This specifies that the application shall track *new* revisions made to the existing document.
+ See also https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.trackrevisions
+
+ 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 doc = new Document({
+ footnotes: [
+ new Paragraph({
+ children: [
+ new TextRun("This is a footnote"),
+ new DeletedTextRun({
+ text: " with some extra text which was deleted",
+ id: 0,
+ author: "Firstname Lastname",
+ date: "2020-10-06T09:05:00Z",
+ }),
+ new InsertedTextRun({
+ text: " and new content",
+ id: 1,
+ author: "Firstname Lastname",
+ date: "2020-10-06T09:05:00Z",
+ }),
+ ],
+ }),
+ ],
+ 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({
+ 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) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/55-math.ts b/demo/55-math.ts
new file mode 100644
index 0000000000..2a240986d0
--- /dev/null
+++ b/demo/55-math.ts
@@ -0,0 +1,294 @@
+// Simple example to add text to a document
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import {
+ Document,
+ Math,
+ MathAngledBrackets,
+ MathCurlyBrackets,
+ MathFraction,
+ MathFunction,
+ MathPreSubSuperScript,
+ MathRadical,
+ MathRoundBrackets,
+ MathRun,
+ MathSquareBrackets,
+ MathSubScript,
+ MathSubSuperScript,
+ MathSum,
+ MathSuperScript,
+ Packer,
+ Paragraph,
+ TextRun,
+} from "../build";
+
+const doc = new Document();
+
+doc.addSection({
+ properties: {},
+ children: [
+ new Paragraph({
+ children: [
+ new Math({
+ 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({
+ children: [
+ new MathFraction({
+ numerator: [new MathRun("1")],
+ denominator: [new MathRun("2")],
+ }),
+ ],
+ subScript: [new MathRun("4")],
+ }),
+ ],
+ }),
+ ],
+ }),
+ new Paragraph({
+ children: [
+ 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")],
+ }),
+ ],
+ }),
+ ],
+ }),
+ 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")],
+ }),
+ ],
+ }),
+ ],
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/56-background-color.ts b/demo/56-background-color.ts
new file mode 100644
index 0000000000..6e8658fd00
--- /dev/null
+++ b/demo/56-background-color.ts
@@ -0,0 +1,33 @@
+// Change background colour of whole document
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun } from "../build";
+
+const doc = new Document({
+ background: {
+ color: "C45911",
+ },
+});
+
+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,
+ }),
+ ],
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/57-add-parent-numbered-lists.ts b/demo/57-add-parent-numbered-lists.ts
new file mode 100644
index 0000000000..e35cd06a27
--- /dev/null
+++ b/demo/57-add-parent-numbered-lists.ts
@@ -0,0 +1,88 @@
+// Numbered lists - Add parent number in sub number
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { AlignmentType, convertInchesToTwip, Document, HeadingLevel, LevelFormat, Packer, Paragraph } from "../build";
+
+const doc = new Document({
+ numbering: {
+ config: [
+ {
+ levels: [
+ {
+ level: 0,
+ format: LevelFormat.DECIMAL,
+ text: "%1",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: convertInchesToTwip(0.5), hanging: 260 },
+ },
+ },
+ },
+ {
+ level: 1,
+ format: LevelFormat.DECIMAL,
+ text: "%1.%2",
+ alignment: AlignmentType.START,
+ style: {
+ paragraph: {
+ indent: { left: 1.25 * convertInchesToTwip(0.5), hanging: 1.25 * 260 },
+ },
+ run: {
+ bold: true,
+ size: 18,
+ font: "Times New Roman",
+ },
+ },
+ },
+ ],
+ reference: "my-number-numbering-reference",
+ },
+ ],
+ },
+});
+
+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,
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/58-section-types.ts b/demo/58-section-types.ts
new file mode 100644
index 0000000000..6c16b44600
--- /dev/null
+++ b/demo/58-section-types.ts
@@ -0,0 +1,93 @@
+// Usage of different Section Types
+// Import from 'docx' rather than '../build' if you install from npm
+import * as fs from "fs";
+import { Document, Packer, Paragraph, TextRun, SectionType } from "../build";
+
+const doc = new Document();
+
+doc.addSection({
+ properties: {},
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun("Hello World"),
+ new TextRun({
+ text: "Foo Bar",
+ bold: true,
+ }),
+ ],
+ }),
+ ],
+});
+
+doc.addSection({
+ properties: {
+ type: SectionType.CONTINUOUS,
+ },
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun("Hello World"),
+ new TextRun({
+ text: "Foo Bar",
+ bold: true,
+ }),
+ ],
+ }),
+ ],
+});
+
+doc.addSection({
+ properties: {
+ type: SectionType.ODD_PAGE,
+ },
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun("Hello World"),
+ new TextRun({
+ text: "Foo Bar",
+ bold: true,
+ }),
+ ],
+ }),
+ ],
+});
+
+doc.addSection({
+ properties: {
+ type: SectionType.EVEN_PAGE,
+ },
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun("Hello World"),
+ new TextRun({
+ text: "Foo Bar",
+ bold: true,
+ }),
+ ],
+ }),
+ ],
+});
+
+doc.addSection({
+ properties: {
+ type: SectionType.NEXT_PAGE,
+ },
+ children: [
+ new Paragraph({
+ children: [
+ new TextRun("Hello World"),
+ new TextRun({
+ text: "Foo Bar",
+ bold: true,
+ }),
+ ],
+ }),
+ ],
+});
+
+Packer.toBuffer(doc).then((buffer) => {
+ fs.writeFileSync("My Document.docx", buffer);
+});
diff --git a/demo/6-page-borders.ts b/demo/6-page-borders.ts
index d54aac9a94..e83f18233b 100644
--- a/demo/6-page-borders.ts
+++ b/demo/6-page-borders.ts
@@ -21,9 +21,9 @@ doc.addSection({
bold: true,
}),
new TextRun({
- text: "Github is the best",
+ text: "\tGithub is the best",
bold: true,
- }).tab(),
+ }),
],
}),
new Paragraph({
diff --git a/demo/browser-demo.html b/demo/browser-demo.html
index 22c7c821a5..ecd7f47701 100644
--- a/demo/browser-demo.html
+++ b/demo/browser-demo.html
@@ -12,21 +12,21 @@
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+