Deploy dolanmiu/docx to github.com/dolanmiu/docx.git:gh-pages
This commit is contained in:
@ -4,15 +4,15 @@
|
||||
|
||||
To make a bullet point, simply make a paragraph into a bullet point:
|
||||
|
||||
```js
|
||||
var text = new docx.TextRun("Bullet points");
|
||||
var paragraph = new docx.Paragraph(text).bullet();
|
||||
```ts
|
||||
const text = new docx.TextRun("Bullet points");
|
||||
const paragraph = new docx.Paragraph(text).bullet();
|
||||
|
||||
var text2 = new docx.TextRun("Are awesome");
|
||||
var paragraph2 = new docx.Paragraph(text2).bullet();
|
||||
const text2 = new docx.TextRun("Are awesome");
|
||||
const paragraph2 = new docx.Paragraph(text2).bullet();
|
||||
|
||||
doc.addParagraph(paragraph);
|
||||
doc.addParagraph(paragraph2);
|
||||
doc.add(paragraph);
|
||||
doc.add(paragraph2);
|
||||
```
|
||||
|
||||
### This will produce:
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
To create a new document, it is very easy:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const doc = new docx.Document();
|
||||
```
|
||||
|
||||
@ -12,7 +12,7 @@ const doc = new docx.Document();
|
||||
|
||||
You can add properties to the Word document by specifying options, for example:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const doc = new docx.Document({
|
||||
creator: "Dolan Miu",
|
||||
description: "My extremely interesting document",
|
||||
@ -33,7 +33,7 @@ const doc = new docx.Document({
|
||||
|
||||
You can mix and match whatever properties you want, or provide no properties.
|
||||
|
||||
### units for positioning
|
||||
### Units for positioning
|
||||
|
||||
Various parts of the API require positioning arguments. The units are "20ths of a point" from the [OOXML](http://officeopenxml.com/index.php) specification.
|
||||
See [Lars Corneliussen's blog post](https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/) for more information and how to convert units.
|
||||
See [Lars Corneliussen's blog post](https://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/) for more information and how to convert units.
|
||||
|
@ -1,24 +1,26 @@
|
||||
# Headers and Footers
|
||||
|
||||
!> Headers and Footers requires an understanding of [Sections](usage/sections.md).
|
||||
|
||||
## Example
|
||||
|
||||
Creating Headers and footers is simple. Access the `Header` and `Footer` by doing so like this:
|
||||
|
||||
```js
|
||||
```ts
|
||||
doc.Header;
|
||||
doc.Footer;
|
||||
```
|
||||
|
||||
You can call the same methods as you would with a `File`:
|
||||
|
||||
```js
|
||||
```ts
|
||||
doc.Header.createParagraph("Header text");
|
||||
doc.Footer.createParagraph("Footer text");
|
||||
```
|
||||
|
||||
Even add images:
|
||||
|
||||
```js
|
||||
```ts
|
||||
doc.Header.createImage([BUFFER_OF_YOUR_IMAGE]);
|
||||
doc.Footer.createImage([BUFFER_OF_YOUR_IMAGE]);
|
||||
```
|
||||
@ -31,7 +33,7 @@ Also all the supported section properties are implemented according to: http://o
|
||||
|
||||
### Example
|
||||
|
||||
```js
|
||||
```ts
|
||||
const header = this.document.createHeader();
|
||||
const footer = this.document.createFooter();
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
# Images
|
||||
|
||||
!> Images requires an understanding of [Sections](usage/sections.md) and [Paragraphs](usage/paragraph.md).
|
||||
|
||||
To create a `floating` image on top of text:
|
||||
|
||||
```ts
|
||||
doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
floating: {
|
||||
horizontalPosition: {
|
||||
offset: 1014400,
|
||||
@ -18,14 +20,27 @@ doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
By default with no arguments, its an `inline` image:
|
||||
|
||||
```ts
|
||||
doc.createImage(fs.readFileSync("./demo/images/parrots.bmp"));
|
||||
const image = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
|
||||
```
|
||||
|
||||
You can also create images manually and add them later:
|
||||
Add it into the document by adding the image into a paragraph:
|
||||
|
||||
```ts
|
||||
const image = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
|
||||
doc.addImage(image);
|
||||
doc.addSection({
|
||||
children: [new Paragraph(image)],
|
||||
});
|
||||
```
|
||||
|
||||
Or:
|
||||
|
||||
```ts
|
||||
doc.addSection({
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [image],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
## Intro
|
||||
@ -34,15 +49,17 @@ Adding images can be done in two ways:
|
||||
|
||||
1. Call the `createImage` method to add the image directly into the `document`:
|
||||
|
||||
```js
|
||||
doc.createImage([IMAGE_BUFFER], [WIDTH], [HEIGHT], [POSITION_OPTIONS]);
|
||||
```ts
|
||||
Media.addImage(doc, [IMAGE_BUFFER], [WIDTH], [HEIGHT], [POSITION_OPTIONS]);
|
||||
```
|
||||
|
||||
2. Create an `image` first, then add it into the `document`:
|
||||
|
||||
```ts
|
||||
const image = Media.addImage(doc, [IMAGE_BUFFER]);
|
||||
doc.addImage(image);
|
||||
doc.addSection({
|
||||
children: [new Paragraph(image)],
|
||||
});
|
||||
```
|
||||
|
||||
`docx` supports `jpeg`, `jpg`, `bmp`, `gif` and `png`
|
||||
@ -141,7 +158,7 @@ wrap: {
|
||||
For example:
|
||||
|
||||
```ts
|
||||
doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
floating: {
|
||||
horizontalPosition: {
|
||||
offset: 2014400,
|
||||
@ -184,7 +201,7 @@ margins: {
|
||||
For example:
|
||||
|
||||
```ts
|
||||
doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
floating: {
|
||||
horizontalPosition: {
|
||||
offset: 2014400,
|
||||
@ -210,7 +227,7 @@ doc.createImage(fs.readFileSync("./demo/images/pizza.gif"), 200, 200, {
|
||||
|
||||
Importing Images from file system path
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo5.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo5.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_
|
||||
|
||||
@ -218,7 +235,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo5.ts_
|
||||
|
||||
Example showing how to add image to headers and footers
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo9.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo9.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_
|
||||
|
||||
@ -226,6 +243,6 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo9.ts_
|
||||
|
||||
Example showing how to float images on top of text and optimally give a `margin`
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo38.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo38.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo38.ts_
|
||||
|
@ -67,7 +67,7 @@ First you need to create a new numbering container class and use it to
|
||||
create your abstract numbering style, define your levels, and create
|
||||
your concrete numbering style:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const numbering = new docx.Numbering();
|
||||
|
||||
const abstractNum = numbering.createAbstractNumbering();
|
||||
@ -81,7 +81,7 @@ const concrete = numbering.createConcreteNumbering(abstractNum);
|
||||
You can then apply your concrete style to paragraphs using the
|
||||
`setNumbering` method:
|
||||
|
||||
```js
|
||||
```ts
|
||||
topLevelP.setNumbering(concrete, 0);
|
||||
subP.setNumbering(concrete, 1);
|
||||
subSubP.setNumbering(concrete, 2);
|
||||
@ -90,7 +90,7 @@ subSubP.setNumbering(concrete, 2);
|
||||
Finally, you need to let your exporter know about your numbering
|
||||
styles when you're ready to render the document:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const packer = new Packer(doc, undefined, undefined, numbering);
|
||||
packer.pack(myOutput);
|
||||
```
|
||||
|
@ -10,7 +10,7 @@ Packers in `version 4` and above are now one single `Packer`. It works in both a
|
||||
|
||||
This will return a NodeJS `Buffer`. If this is used in the browser, it will return a `UInt8Array` instead.
|
||||
|
||||
```js
|
||||
```ts
|
||||
const packer = new docx.Packer();
|
||||
|
||||
packer.toBuffer(doc).then((buffer) => {
|
||||
@ -20,7 +20,7 @@ packer.toBuffer(doc).then((buffer) => {
|
||||
|
||||
### Export as a `base64` string
|
||||
|
||||
```js
|
||||
```ts
|
||||
const packer = new docx.Packer();
|
||||
|
||||
packer.toBase64String(doc).then((string) => {
|
||||
@ -32,7 +32,7 @@ packer.toBase64String(doc).then((string) => {
|
||||
|
||||
This is useful if you want to send it as an downloadable in a browser environment.
|
||||
|
||||
```js
|
||||
```ts
|
||||
const packer = new docx.Packer();
|
||||
|
||||
packer.toBlob(doc).then((blob) => {
|
||||
@ -45,7 +45,7 @@ packer.toBlob(doc).then((blob) => {
|
||||
|
||||
### File System Packer
|
||||
|
||||
```js
|
||||
```ts
|
||||
const docx = require("docx");
|
||||
|
||||
const doc = new docx.Document();
|
||||
@ -56,7 +56,7 @@ exporter.pack("My Document");
|
||||
|
||||
### Buffer Packer
|
||||
|
||||
```js
|
||||
```ts
|
||||
const docx = require("docx");
|
||||
|
||||
const doc = new docx.Document();
|
||||
@ -68,7 +68,7 @@ const buffer = exporter.pack();
|
||||
|
||||
Creates a `node` `Readable` stream
|
||||
|
||||
```js
|
||||
```ts
|
||||
const docx = require("docx");
|
||||
|
||||
const doc = new docx.Document();
|
||||
@ -88,7 +88,7 @@ I used the express exporter in my [website](http://www.dolan.bio).
|
||||
|
||||
The recommended way is to use the `StreamPacker` and handle the `express` magic outside of the library:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const docx = require("docx");
|
||||
|
||||
const doc = new docx.Document();
|
||||
@ -107,7 +107,7 @@ where `res` is the response object obtained through the Express router. It is th
|
||||
|
||||
You can export your word document as a PDF file like so:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const exporter = new docx.LocalPacker(doc);
|
||||
exporter.packPdf("My Document");
|
||||
|
||||
|
@ -2,28 +2,180 @@
|
||||
|
||||
> Everything (text, images, graphs etc) in OpenXML is organised in paragraphs.
|
||||
|
||||
## Example
|
||||
!> Paragraphs requires an understanding of [Sections](usage/sections.md).
|
||||
|
||||
You can add more text to the paragraph by doing this:
|
||||
You can create `Paragraphs` in the following ways:
|
||||
|
||||
```js
|
||||
var paragraph = new docx.Paragraph(),
|
||||
### Shorthand
|
||||
|
||||
```ts
|
||||
import { Paragraph } from "docx";
|
||||
|
||||
const paragraph = new Paragraph("Short hand Hello World");
|
||||
```
|
||||
|
||||
```js
|
||||
var text = new docx.TextRun("Lorem Ipsum Foo Bar");
|
||||
var paragraph = new docx.Paragraph();
|
||||
paragraph.addRun(text);
|
||||
### Children Method
|
||||
|
||||
This method is useful for adding different `text` with different styles or adding `images` inline.
|
||||
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World")],
|
||||
});
|
||||
```
|
||||
|
||||
```js
|
||||
var paragraph = new docx.Paragraph("Short hand notation for adding text.");
|
||||
### Explicit
|
||||
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
text: "Short hand notation for adding text.",
|
||||
});
|
||||
```
|
||||
|
||||
After you create the paragraph, you must add the paragraph into the `document`:
|
||||
After you create the paragraph, you must add the paragraph into the `document's section`. Learn more about `sections` here:
|
||||
|
||||
```js
|
||||
doc.addParagraph(paragraph);
|
||||
```ts
|
||||
doc.addSection({
|
||||
children: [paragraph],
|
||||
});
|
||||
```
|
||||
|
||||
Or the preferred convension, define the paragraph inside the section and remove the usage of variables:
|
||||
|
||||
```ts
|
||||
doc.addSection({
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World")],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
This is the list of options for a paragraph. A detailed explanation is below:
|
||||
|
||||
| Property | Type | Mandatory? | Possible Values |
|
||||
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------------- |
|
||||
| [text](#text) | `string` | Optional | |
|
||||
| [heading](#heading) | `HeadingLevel` | Optional | `HEADING_1`, `HEADING_2`, `HEADING_3`, `HEADING_4`, `HEADING_5`, `HEADING_6`, `TITLE` |
|
||||
| [border](#border) | `IBorderOptions` | Optional | `top`, `bottom`, `left`, `right`. Each of these are of type IBorderPropertyOptions. Click here for Example |
|
||||
| [spacing](#spacing) | `ISpacingProperties` | Optional | See below for ISpacingProperties |
|
||||
| [outlineLevel](#outline-level) | `number` | Optional | |
|
||||
| alignment | `AlignmentType` | Optional | |
|
||||
| heading | `HeadingLevel` | Optional | |
|
||||
| bidirectional | `boolean` | Optional | |
|
||||
| thematicBreak | `boolean` | Optional | |
|
||||
| pageBreakBefore | `boolean` | Optional | |
|
||||
| contextualSpacing | `boolean` | Optional | |
|
||||
| indent | `IIndentAttributesProperties` | Optional | |
|
||||
| keepLines | `boolean` | Optional | |
|
||||
| keepNext | `boolean` | Optional | |
|
||||
| children | `(TextRun or PictureRun or Hyperlink)[]` | Optional | |
|
||||
| style | `string` | Optional | |
|
||||
| tabStop | `{ left?: ITabStopOptions; right?: ITabStopOptions; maxRight?: { leader: LeaderType; }; center?: ITabStopOptions }` | Optional | |
|
||||
| bullet | `{ level: number }` | Optional | |
|
||||
| numbering | `{ num: Num; level: number; custom?: boolean }` | Optional | |
|
||||
|
||||
## Text
|
||||
|
||||
This is the text in a paragraph. You can also add text by using the `Paragraph` shorthand (mentioned above) or adding `children`.
|
||||
|
||||
**Example:**
|
||||
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
text: "Hello World",
|
||||
});
|
||||
```
|
||||
|
||||
## Heading
|
||||
|
||||
**Example:**
|
||||
|
||||
Setting a Heading 1 paragraph with "Hello World" as it's text:
|
||||
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
text: "Hello World",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
});
|
||||
```
|
||||
|
||||
## Border
|
||||
|
||||
Add borders to a `Paragraph`. Good for making the `Paragraph` stand out
|
||||
|
||||
#### IBorderPropertyOptions
|
||||
|
||||
`top`, `bottom`, `left`, `right` of the border
|
||||
|
||||
| Property | Type | Notes |
|
||||
| -------- | -------- | -------- |
|
||||
| color | `string` | Required |
|
||||
| space | `number` | Required |
|
||||
| value | `string` | Required |
|
||||
| size | `number` | Required |
|
||||
|
||||
**Example:**
|
||||
|
||||
Add border on the top and the bottom of the paragraph
|
||||
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
text: "I have borders on my top and bottom sides!",
|
||||
border: {
|
||||
top: {
|
||||
color: "auto",
|
||||
space: 1,
|
||||
value: "single",
|
||||
size: 6,
|
||||
},
|
||||
bottom: {
|
||||
color: "auto",
|
||||
space: 1,
|
||||
value: "single",
|
||||
size: 6,
|
||||
},
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
## Spacing
|
||||
|
||||
Adding spacing between paragraphs
|
||||
|
||||
### ISpacingProperties
|
||||
|
||||
| Property | Type | Notes |
|
||||
| -------- | -------- | -------- |
|
||||
| after | `number` | Optional |
|
||||
| before | `number` | Optional |
|
||||
| line | `number` | Optional |
|
||||
| lineRule | `string` | Optional |
|
||||
|
||||
**Example:**
|
||||
|
||||
Add spacing before the paragraph:
|
||||
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
text: "Paragraph with spacing before",
|
||||
spacing: {
|
||||
before: 200,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
## Outline Level
|
||||
|
||||
**Example:**
|
||||
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
outlineLevel: 0,
|
||||
});
|
||||
```
|
||||
|
||||
## Styles
|
||||
@ -32,46 +184,29 @@ To create styles, please refer to the styling Wiki: https://github.com/dolanmiu/
|
||||
|
||||

|
||||
|
||||
### Heading1 - Heading5
|
||||
### Headings and titles
|
||||
|
||||
```js
|
||||
paragraph.heading1();
|
||||
paragraph.heading2();
|
||||
paragraph.heading3();
|
||||
paragraph.heading4();
|
||||
paragraph.heading5();
|
||||
```
|
||||
```ts
|
||||
import { HeadingLevel, Paragraph } from "docx";
|
||||
|
||||
### Title
|
||||
|
||||
```js
|
||||
paragraph.title();
|
||||
const paragraph = new Paragraph({
|
||||
text: "Hello World",
|
||||
heading: HeadingLevel.TITLE,
|
||||
});
|
||||
```
|
||||
|
||||
## Text Alignment
|
||||
|
||||
To change the text alignment of a paragraph, for center, left, right or justified:
|
||||
To change the text alignment of a paragraph, add an `AlignmentType` option on the paragraph.for center, left, right or justified:
|
||||
|
||||
```js
|
||||
paragraph.center();
|
||||
```
|
||||
**Example:**
|
||||
|
||||
```js
|
||||
paragraph.left();
|
||||
```
|
||||
|
||||
```js
|
||||
paragraph.right();
|
||||
```
|
||||
|
||||
```js
|
||||
paragraph.justified();
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js
|
||||
paragraph.heading1().center();
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
text: "Hello World",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
alignment: AlignmentType.CENTER,
|
||||
});
|
||||
```
|
||||
|
||||
The above will create a `heading 1` which is `centered`.
|
||||
@ -94,10 +229,15 @@ The result is:
|
||||
|
||||
## Thematic Break
|
||||
|
||||
To add a break in the page, simply add `.thematicBreak()` on a paragraph:
|
||||
To add a thematic break in the `Paragraph`:
|
||||
|
||||
```js
|
||||
var paragraph = new docx.Paragraph("Amazing Heading").heading1().thematicBreak();
|
||||
```ts
|
||||
const paragraph = new docx.Paragraph("Amazing Heading");
|
||||
const paragraph = new Paragraph({
|
||||
text: "Amazing Heading",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
thematicBreak: true,
|
||||
});
|
||||
```
|
||||
|
||||
The above example will create a heading with a page break directly under it.
|
||||
@ -106,8 +246,8 @@ The above example will create a heading with a page break directly under it.
|
||||
|
||||
To move to a new page (insert a page break), simply add `.pageBreak()` on a paragraph:
|
||||
|
||||
```js
|
||||
var paragraph = new docx.Paragraph("Amazing Heading").heading1().pageBreak();
|
||||
```ts
|
||||
const paragraph = new docx.Paragraph("Amazing Heading").pageBreak();
|
||||
```
|
||||
|
||||
The above example will create a heading and start a new page immediately afterwards.
|
||||
@ -116,8 +256,11 @@ The above example will create a heading and start a new page immediately afterwa
|
||||
|
||||
This option (available in word) will make sure that the paragraph will start on a new page (if it's not already on a new page).
|
||||
|
||||
```js
|
||||
var paragraph = new docx.Paragraph("Hello World on another page").pageBreakBefore();
|
||||
```ts
|
||||
const paragraph = new Paragraph({
|
||||
text: "Hello World on another page",
|
||||
pageBreakBefore: true,
|
||||
});
|
||||
```
|
||||
|
||||

|
||||
|
21
usage/sections.md
Normal file
21
usage/sections.md
Normal file
@ -0,0 +1,21 @@
|
||||
# Sections
|
||||
|
||||
> Every document is made up of one or more sections
|
||||
|
||||
A section is a grouping of paragraphs that have a specific set of properties used to define the pages on which the text will appear. Properties include page size, page numbers, page orientation, headers, borders and margins.
|
||||
|
||||
For example, you could have one section which is portrait with a header and footer, and another section in landscape with no footer, and a header showing the current page number.
|
||||
|
||||
## Example
|
||||
|
||||
This creates a simple section in a document with one paragraph inside:
|
||||
|
||||
```ts
|
||||
doc.addSection({
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [new TextRun("Hello World")],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
@ -2,7 +2,7 @@
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
```ts
|
||||
const para = new Paragraph("To whom it may concern:").heading2().center();
|
||||
|
||||
const name = new TextRun("Name:")
|
||||
@ -56,7 +56,7 @@ Unlike CSS, less specific rules don't _necessarily_ override parent rules. The r
|
||||
|
||||
This is the type of formatting that your uncle uses when he types out documents: _N ... a ... m ... e ... :_ Then he grabs the mouse, highlights _Name:_ and moves over to the **B** for bold. This manner of formatting results in markup that is similar to writing `<span style="bold: true">Name:</span>` if you were typing out HTML. DOCX (the format) allows you to specify this for any of the four types of items. `docx` (the library) only supports this type of formatting for paragraphs and characters, using a _fluent_ api. Thus you could do:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const name = new TextRun("Name:")
|
||||
.bold()
|
||||
.font("Calibri")
|
||||
@ -65,7 +65,7 @@ const name = new TextRun("Name:")
|
||||
|
||||
Or for paragraph formatting:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const para = new Paragraph("To whom it may concern:").heading2().center();
|
||||
```
|
||||
|
||||
@ -76,12 +76,12 @@ DOCX files contain a styles section separate from the main content, much like ho
|
||||
There are three parts to using custom styles with `docx`:
|
||||
|
||||
1. Create a container object for the style definitions:
|
||||
```js
|
||||
```ts
|
||||
const myStyles = new docx.Styles();
|
||||
```
|
||||
2. Define your custom styles, similar to the way you would format a paragraph or run
|
||||
|
||||
```js
|
||||
```ts
|
||||
// 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
|
||||
myStyles
|
||||
@ -106,7 +106,7 @@ There are three parts to using custom styles with `docx`:
|
||||
|
||||
3. When you generate your document, make sure to pass the `styles` container to the `Packer`:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const packer = new Packer(doc, myStyles);
|
||||
packer.pack(myOutStream);
|
||||
```
|
||||
|
@ -24,7 +24,7 @@
|
||||
|
||||
Read the styles using `fs`, and put it into the `Document` object in the constructor:
|
||||
|
||||
```js
|
||||
```ts
|
||||
const styles = fs.readFileSync("./styles.xml", "utf-8");
|
||||
const doc = new docx.Document({
|
||||
title: "Title",
|
||||
@ -34,12 +34,12 @@ const doc = new docx.Document({
|
||||
|
||||
You can use paragraphs, `heading1()`, `heading2()` etc and it will be styled according to your `styles.xml` created earlier. You can even use your new style you made by calling the `style` method:
|
||||
|
||||
```js
|
||||
```ts
|
||||
doc.createParagraph("Cool Heading Text").heading1();
|
||||
|
||||
let paragraph = new docx.Paragraph('This is a custom named style from the template "Cool New Style"');
|
||||
const paragraph = new docx.Paragraph('This is a custom named style from the template "Cool New Style"');
|
||||
paragraph.style("Cool New Style");
|
||||
doc.addParagraph(paragraph);
|
||||
doc.add(paragraph);
|
||||
|
||||
doc.createParagraph("Some normal text");
|
||||
```
|
||||
|
@ -10,45 +10,45 @@ Simply call the relevant methods on the paragraph listed below. Then just add a
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
var paragraph = new docx.Paragraph().maxRightTabStop();
|
||||
var leftText = new docx.TextRun("Hey everyone").bold();
|
||||
var rightText = new docx.TextRun("11th November 2015").tab();
|
||||
```ts
|
||||
const paragraph = new docx.Paragraph().maxRightTabStop();
|
||||
const leftText = new docx.TextRun("Hey everyone").bold();
|
||||
const rightText = new docx.TextRun("11th November 2015").tab();
|
||||
paragraph.addRun(leftText);
|
||||
paragraph.addRun(rightText);
|
||||
```
|
||||
The example above will create a left aligned text, and a right aligned text on the same line. The laymans approach to this problem would be to either use text boxes or tables. YUK!
|
||||
|
||||
```js
|
||||
var paragraph = new docx.Paragraph();
|
||||
```ts
|
||||
const paragraph = new docx.Paragraph();
|
||||
paragraph.maxRightTabStop();
|
||||
paragraph.leftTabStop(1000);
|
||||
var text = new docx.TextRun("Second tab stop here I come!").tab().tab();
|
||||
const text = new docx.TextRun("Second tab stop here I come!").tab().tab();
|
||||
paragraph.addRun(text);
|
||||
```
|
||||
|
||||
The above shows the use of two tab stops, and how to select/use it.
|
||||
|
||||
## Left Tab Stop
|
||||
```js
|
||||
```ts
|
||||
paragraph.leftTabStop(2268);
|
||||
```
|
||||
2268 is the distance from the left side.
|
||||
|
||||
## Center Tab Stop
|
||||
```js
|
||||
```ts
|
||||
paragraph.centerTabStop(2268);
|
||||
```
|
||||
2268 is the distance from the left side.
|
||||
|
||||
## Right Tab Stop
|
||||
```js
|
||||
```ts
|
||||
paragraph.rightTabStop(2268);
|
||||
```
|
||||
2268 is the distance from the left side.
|
||||
|
||||
## Max Right Tab Stop
|
||||
```js
|
||||
```ts
|
||||
paragraph.maxRightTabStop();
|
||||
```
|
||||
This will create a tab stop on the very edge of the right hand side. Handy for right aligning and left aligning text on the same line.
|
||||
|
@ -12,7 +12,7 @@ The complete documentation can be found [here](https://www.ecma-international.or
|
||||
|
||||
All you need to do is create a `TableOfContents` object and assign it to the document.
|
||||
|
||||
```js
|
||||
```ts
|
||||
const toc = new TableOfContents("Summary", {
|
||||
hyperlink: true,
|
||||
headingStyleRange: "1-5",
|
||||
@ -47,7 +47,7 @@ Here is the list of all options that you can use to generate your tables of cont
|
||||
|
||||
## Examples
|
||||
|
||||
```js
|
||||
```ts
|
||||
// Let's define the options for generate a TOC for heading 1-5 and MySpectacularStyle,
|
||||
// making the entries be hyperlinks for the paragraph
|
||||
const toc = new TableOfContents("Summary", {
|
||||
@ -58,15 +58,15 @@ const toc = new TableOfContents("Summary", {
|
||||
|
||||
doc.addTableOfContents(toc);
|
||||
|
||||
doc.addParagraph(new Paragraph("Header #1").heading1().pageBreakBefore());
|
||||
doc.addParagraph(new Paragraph("I'm a little text, very nicely written.'"));
|
||||
doc.add(new Paragraph("Header #1").heading1().pageBreakBefore());
|
||||
doc.add(new Paragraph("I'm a little text, very nicely written.'"));
|
||||
|
||||
doc.addParagraph(new Paragraph("Header #2").heading1().pageBreakBefore());
|
||||
doc.addParagraph(new Paragraph("I'm another text very nicely written.'"));
|
||||
doc.addParagraph(new Paragraph("Header #2.1").heading2());
|
||||
doc.addParagraph(new Paragraph("I'm another text very nicely written.'"));
|
||||
doc.add(new Paragraph("Header #2").heading1().pageBreakBefore());
|
||||
doc.add(new Paragraph("I'm another text very nicely written.'"));
|
||||
doc.add(new Paragraph("Header #2.1").heading2());
|
||||
doc.add(new Paragraph("I'm another text very nicely written.'"));
|
||||
|
||||
doc.addParagraph(new Paragraph("My Spectacular Style #1").style("MySpectacularStyle").pageBreakBefore());
|
||||
doc.add(new Paragraph("My Spectacular Style #1").style("MySpectacularStyle").pageBreakBefore());
|
||||
```
|
||||
|
||||
### Complete example
|
||||
|
@ -4,28 +4,30 @@ You can create tables with `docx`. More information can be found [here](http://o
|
||||
|
||||
## Create Table
|
||||
|
||||
To create a table, simply use the `createTable()` method on a `document`.
|
||||
To create a table, simply create one with `new Table()`, then add it to the document: `doc.add()`.
|
||||
|
||||
```ts
|
||||
const table = doc.createTable([NUMBER OF ROWS], [NUMBER OF COLUMNS]);
|
||||
const table = doc.add(new Table({
|
||||
rows: [NUMBER OF ROWS],
|
||||
columns: [NUMBER OF COLUMNS]
|
||||
});
|
||||
```
|
||||
|
||||
Alternatively, you can create a table object directly, and then add it in the `document`
|
||||
|
||||
```ts
|
||||
const table = new Table(4, 4);
|
||||
doc.addTable(table);
|
||||
doc.add(table);
|
||||
```
|
||||
|
||||
The snippet below creates a table of 2 rows and 4 columns.
|
||||
|
||||
```ts
|
||||
const table = doc.createTable(2, 4);
|
||||
|
||||
// Or
|
||||
|
||||
const table = new Table(2, 4);
|
||||
doc.addTable(table);
|
||||
const table = new Table({
|
||||
rows: 2,
|
||||
columns: 4,
|
||||
});
|
||||
doc.add(table);
|
||||
```
|
||||
|
||||
## Rows and Columns
|
||||
@ -70,7 +72,7 @@ column.getCell(index);
|
||||
|
||||
## Cells
|
||||
|
||||
The `createTable()` method created a table with cells. To access the cell, use the `getCell()` method.
|
||||
To access the cell, use the `getCell()` method.
|
||||
|
||||
```ts
|
||||
const cell = table.getCell([ROW INDEX], [COLUMN INDEX]);
|
||||
@ -90,10 +92,10 @@ const cell = column.getCell(2);
|
||||
|
||||
### Add paragraph to a cell
|
||||
|
||||
Once you have got the cell, you can add data to it with the `addParagraph()` method.
|
||||
Once you have got the cell, you can add data to it with the `add()` method.
|
||||
|
||||
```ts
|
||||
cell.addParagraph(new Paragraph("Hello"));
|
||||
cell.add(new Paragraph("Hello"));
|
||||
```
|
||||
|
||||
### Set width of a cell
|
||||
@ -103,10 +105,11 @@ You can specify the width of a cell using:
|
||||
`cell.Properties.setWidth(width, format)`
|
||||
|
||||
format can be:
|
||||
* WidthType.AUTO
|
||||
* WidthType.DXA: value is in twentieths of a point
|
||||
* WidthType.NIL: is considered as zero
|
||||
* WidthType.PCT: percent of table width
|
||||
|
||||
- WidthType.AUTO
|
||||
- WidthType.DXA: value is in twentieths of a point
|
||||
- WidthType.NIL: is considered as zero
|
||||
- WidthType.PCT: percent of table width
|
||||
|
||||
### Example
|
||||
|
||||
@ -115,7 +118,7 @@ cell.Properties.setWidth(100, WidthType.DXA);
|
||||
```
|
||||
|
||||
```ts
|
||||
cell.Properties.setWidth('50%', WidthType.PCT);
|
||||
cell.Properties.setWidth("50%", WidthType.PCT);
|
||||
```
|
||||
|
||||
## Borders
|
||||
@ -223,7 +226,7 @@ It has not been implemented yet, but it will follow a similar structure as mergi
|
||||
To have a table within a table
|
||||
|
||||
```ts
|
||||
cell.addTable(new Table(1, 1));
|
||||
cell.add(new Table(1, 1));
|
||||
```
|
||||
|
||||
## Pagination
|
||||
@ -241,11 +244,10 @@ If a table is paginated on multiple pages, it is possible to repeat a row at the
|
||||
```ts
|
||||
table.getRow(0).setTableHeader();
|
||||
```
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo4.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
|
||||
|
||||
@ -253,7 +255,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo4.ts_
|
||||
|
||||
Example showing how to add colourful borders to tables
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo20.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
|
||||
|
||||
@ -261,11 +263,11 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo20.ts_
|
||||
|
||||
Example showing how to add images to tables
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo24.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo24.ts_
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo36.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo36.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo36.ts_
|
||||
|
||||
@ -273,7 +275,7 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo36.ts_
|
||||
|
||||
Example showing how align text in a table cell
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo31.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo31.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo31.ts_
|
||||
|
||||
@ -281,11 +283,11 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo31.ts_
|
||||
|
||||
Example showing merging of `rows`
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo32.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo32.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo32.ts_
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo41.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo41.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo41.ts_
|
||||
|
||||
@ -293,13 +295,12 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo41.ts_
|
||||
|
||||
Example showing merging of `columns`
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo43.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo43.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo43.ts_
|
||||
|
||||
### Floating tables
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo34.ts ':include')
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo34.ts ":include")
|
||||
|
||||
_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo34.ts_
|
||||
|
||||
|
@ -1,10 +1,15 @@
|
||||
# Text
|
||||
# Text Runs
|
||||
|
||||
Paragraphs need `text run` objects. To create text:
|
||||
!> TextRuns requires an understanding of [Paragraphs](usage/paragraph.md).
|
||||
|
||||
```js
|
||||
var text = new docx.TextRun("My awesome text here for my university dissertation");
|
||||
paragraph.addRun(text);
|
||||
You can add multiple `text runs` in `Paragraphs`. This is the most verbose way of writing a `Paragraph` but it is also the most flexible:
|
||||
|
||||
```ts
|
||||
import { Paragraph, TextRun } from "docx";
|
||||
|
||||
const paragraph = new Paragraph({
|
||||
children: [new TextRun("My awesome text here for my university dissertation"), new TextRun("Foo Bar")],
|
||||
});
|
||||
```
|
||||
|
||||
Text objects have methods inside which changes the way the text is displayed.
|
||||
@ -15,55 +20,87 @@ More info [here](https://english.stackexchange.com/questions/97081/what-is-the-t
|
||||
|
||||
### Bold
|
||||
|
||||
```js
|
||||
text.bold();
|
||||
```ts
|
||||
const text = new TextRun({
|
||||
text: "Foo Bar",
|
||||
bold: true,
|
||||
});
|
||||
```
|
||||
|
||||
### Italics
|
||||
|
||||
```js
|
||||
text.italics();
|
||||
```ts
|
||||
const text = new TextRun({
|
||||
text: "Foo Bar",
|
||||
italics: true,
|
||||
});
|
||||
```
|
||||
|
||||
### Underline
|
||||
|
||||
```js
|
||||
text.underline();
|
||||
Underline has a few options
|
||||
|
||||
#### Options
|
||||
|
||||
| Property | Type | Notes | Possible Values |
|
||||
| -------- | --------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| type | `UnderlineType` | Optional | SINGLE, WORD, DOUBLE, THICK, DOTTED, DOTTEDHEAV, DASH, DASHEDHEAV, DASHLONG, DASHLONGHEAV, DOTDASH, DASHDOTHEAVY, DOTDOTDAS, DASHDOTDOTHEAVY, WAVE, WAVYHEAVY, WAVYDOUBLE |
|
||||
| color | `string` | Optional | Color Hex values |
|
||||
|
||||
**Example:**
|
||||
|
||||
```ts
|
||||
const text = new TextRun({
|
||||
text: "and then underlined ",
|
||||
underline: {
|
||||
type: UnderlineType.DOUBLE,
|
||||
color: "990011",
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
To do a simple vanilla underline:
|
||||
|
||||
```ts
|
||||
const text = new TextRun({
|
||||
text: "and then underlined ",
|
||||
underline: {},
|
||||
});
|
||||
```
|
||||
|
||||
### Strike through
|
||||
|
||||
```js
|
||||
```ts
|
||||
text.strike();
|
||||
```
|
||||
|
||||
### Double strike through
|
||||
|
||||
```js
|
||||
```ts
|
||||
text.doubleStrike();
|
||||
```
|
||||
|
||||
### Superscript
|
||||
|
||||
```js
|
||||
```ts
|
||||
text.superScript();
|
||||
```
|
||||
|
||||
### Subscript
|
||||
|
||||
```js
|
||||
```ts
|
||||
text.subScript();
|
||||
```
|
||||
|
||||
### All Capitals
|
||||
|
||||
```js
|
||||
```ts
|
||||
text.allCaps();
|
||||
```
|
||||
|
||||
### Small Capitals
|
||||
|
||||
```js
|
||||
```ts
|
||||
text.smallCaps();
|
||||
```
|
||||
|
||||
@ -71,7 +108,7 @@ text.smallCaps();
|
||||
|
||||
Sometimes you would want to put text underneath another line of text but inside the same paragraph.
|
||||
|
||||
```js
|
||||
```ts
|
||||
text.break();
|
||||
```
|
||||
|
||||
@ -79,6 +116,6 @@ text.break();
|
||||
|
||||
What if you want to create a paragraph which is **_bold_** and **_italic_**?
|
||||
|
||||
```js
|
||||
```ts
|
||||
paragraph.bold().italics();
|
||||
```
|
||||
|
Reference in New Issue
Block a user