2018-08-03 23:26:14 +01:00
# Paragraph
2018-08-04 03:28:27 +01:00
2021-03-25 19:21:27 +03:00
> Everything (text, images, graphs etc) in OpenXML is organized in paragraphs.
2018-08-04 03:28:27 +01:00
2021-05-18 17:15:29 +03:00
!> Paragraphs requires an understanding of [Sections ](usage/sections.md ).
2018-08-04 03:28:27 +01:00
2019-08-03 13:42:24 +01:00
You can create `Paragraphs` in the following ways:
### Shorthand
2018-08-03 23:26:14 +01:00
2019-08-06 17:51:13 +01:00
```ts
2019-08-03 13:42:24 +01:00
import { Paragraph } from "docx";
2019-08-06 17:51:13 +01:00
const paragraph = new Paragraph("Short hand Hello World");
2018-08-03 23:26:14 +01:00
```
2019-08-03 13:42:24 +01:00
### Children Method
2021-05-18 17:15:29 +03:00
This method is useful for adding different [text ](usage/text.md ) with different styles, [symbols ](usage/symbols.md ), or adding [images ](usage/images.md ) inline.
2019-08-03 13:42:24 +01:00
2019-08-06 17:51:13 +01:00
```ts
const paragraph = new Paragraph({
2019-11-01 01:57:01 +00:00
children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World"), new SymbolRun("F071")],
2019-08-03 13:42:24 +01:00
});
2018-08-03 23:26:14 +01:00
```
2019-08-03 13:42:24 +01:00
### Explicit
2019-08-06 17:51:13 +01:00
```ts
const paragraph = new Paragraph({
2019-08-03 13:42:24 +01:00
text: "Short hand notation for adding text.",
});
2018-08-03 23:26:14 +01:00
```
2021-03-14 18:12:52 +00:00
After you create the paragraph, you must add the paragraph into a `section` :
2018-08-03 23:26:14 +01:00
2019-08-06 17:51:13 +01:00
```ts
2021-03-19 20:53:56 +00:00
const doc = new Document({
sections: [{
children: [paragraph],
}];
2019-08-03 13:42:24 +01:00
});
```
2021-03-25 19:21:27 +03:00
Or the preferred convention, define the paragraph inside the section and remove the usage of variables:
2019-08-03 13:42:24 +01:00
2019-08-06 17:51:13 +01:00
```ts
2021-03-19 20:53:56 +00:00
const doc = new Document({
sections: [{
children: [
new Paragraph({
children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World")],
}),
],
}];
2019-08-03 13:42:24 +01:00
});
```
## Options
This is the list of options for a paragraph. A detailed explanation is below:
2022-11-19 20:14:15 +00:00
| 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 | `START` , `CENTER` , `END` , `BOTH` , `MEDIUM_KASHIDA` , `DISTRIBUTE` , `NUM_TAB` , `HIGH_KASHIDA` , `LOW_KASHIDA` , `THAI_DISTRIBUTE` , `LEFT` , `RIGHT` , `JUSTIFIED` |
| 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 ImageRun or Hyperlink)[]` | Optional | |
| style | `string` | Optional | |
| [tabStop ](usage/tab-stops ) | `{ left?: ITabStopOptions; right?: ITabStopOptions; maxRight?: { leader: LeaderType; }; center?: ITabStopOptions }` | Optional | |
| [bullet ](usage/bullet-points ) | `{ level: number }` | Optional | |
| [numbering ](usage/numbering ) | `{ num: ConcreteNumbering; level: number; custom?: boolean }` | Optional | |
| [widowControl ](#widow-control ) | `boolean` | Optional | |
| [frame ](usage/text-frames.md ) | `IFrameOptions` | Optional | |
2019-08-03 13:42:24 +01:00
## 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
2022-09-28 09:19:18 -06:00
Add borders to a `Paragraph` . Good for making the `Paragraph` stand out. Border top and border bottom can be used as a horizontal rule (also known as horizontal line).
2019-08-03 13:42:24 +01:00
#### IBorderPropertyOptions
`top` , `bottom` , `left` , `right` of the border
| Property | Type | Notes |
| -------- | -------- | -------- |
| color | `string` | Required |
| space | `number` | Required |
2022-07-06 11:51:10 +02:00
| style | `string` | Required |
2019-08-03 13:42:24 +01:00
| 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,
2022-07-06 11:51:10 +02:00
style: "single",
2019-08-03 13:42:24 +01:00
size: 6,
},
bottom: {
color: "auto",
space: 1,
2022-07-06 11:51:10 +02:00
style: "single",
2019-08-03 13:42:24 +01:00
size: 6,
},
},
});
```
2020-10-28 01:05:31 +00:00
## Shading
Add color to an entire paragraph block
```ts
const paragraph = new Paragraph({
text: "shading",
shading: {
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "00FFFF",
fill: "FF0000",
},
});
```
2021-03-14 18:12:52 +00:00
## Widow Control
Allow First/Last Line to Display on a Separate Page
```ts
const paragraph = new Paragraph({
text: "shading",
widowControl: true,
});
```
2019-08-03 13:42:24 +01:00
## Spacing
Adding spacing between paragraphs
### ISpacingProperties
2022-11-19 15:51:41 +00:00
| Property | Type | Notes | Possible Values |
| -------- | -------------- | -------- | -------------------------------------- |
| after | `number` | Optional | |
| before | `number` | Optional | |
| line | `number` | Optional | |
| lineRule | `LineRuleType` | Optional | `AT_LEAST` , `EXACTLY` , `EXACT` , `AUTO` |
Note: The `lineRule` property has different values depending on the version of Word you are using. The `EXACTLY` value is only available in Word 2016 and above. Use `EXACT` for greater support, including LibreOffice etc. Read this issue for more information: https://github.com/dolanmiu/docx/issues/1773.
2019-08-03 13:42:24 +01:00
**Example:**
Add spacing before the paragraph:
```ts
const paragraph = new Paragraph({
2019-08-05 23:24:43 +01:00
text: "Paragraph with spacing before",
2019-08-03 13:42:24 +01:00
spacing: {
before: 200,
},
});
2018-08-03 23:26:14 +01:00
```
2019-08-05 23:24:43 +01:00
## Outline Level
**Example:**
2019-08-06 17:15:45 +01:00
```ts
const paragraph = new Paragraph({
outlineLevel: 0,
});
```
2018-08-03 23:26:14 +01:00
## Styles
2021-03-14 18:12:52 +00:00
To create styles, please refer to the [styling documentation ](usage/styling-with-js )
2018-08-03 23:26:14 +01:00

2019-08-03 13:42:24 +01:00
### Headings and titles
2018-08-03 23:26:14 +01:00
2019-08-06 17:51:13 +01:00
```ts
2019-08-03 13:42:24 +01:00
import { HeadingLevel, Paragraph } from "docx";
2018-08-03 23:26:14 +01:00
2019-08-03 13:42:24 +01:00
const paragraph = new Paragraph({
text: "Hello World",
heading: HeadingLevel.TITLE,
});
2018-08-03 23:26:14 +01:00
```
## Text Alignment
2019-08-06 17:36:05 +01:00
To change the text alignment of a paragraph, add an `AlignmentType` option on the paragraph.for center, left, right or justified:
2018-08-03 23:26:14 +01:00
2019-08-06 17:36:05 +01:00
**Example:**
2018-08-03 23:26:14 +01:00
2019-08-06 17:51:13 +01:00
```ts
2019-08-03 13:42:24 +01:00
const paragraph = new Paragraph({
text: "Hello World",
heading: HeadingLevel.HEADING_1,
alignment: AlignmentType.CENTER,
});
2018-08-03 23:26:14 +01:00
```
The above will create a `heading 1` which is `centered` .
2019-04-12 13:46:05 +02:00
### Justified text with breaks
When a paragraph is justified, you may want to not justify the contents of incomplete lines, which end in a soft line break.

2019-04-25 09:49:44 -04:00
This is possible to achieve using:
2019-04-12 13:46:05 +02:00
```ts
2019-04-25 09:49:44 -04:00
this.doc.Settings.addCompatibility().doNotExpandShiftReturn();
2019-04-12 13:46:05 +02:00
```
The result is:

2018-08-03 23:26:14 +01:00
## Thematic Break
2018-08-04 03:28:27 +01:00
2019-08-06 17:15:45 +01:00
To add a thematic break in the `Paragraph` :
2018-08-03 23:26:14 +01:00
2019-08-06 17:51:13 +01:00
```ts
const paragraph = new docx.Paragraph("Amazing Heading");
2019-08-06 17:15:45 +01:00
const paragraph = new Paragraph({
text: "Amazing Heading",
heading: HeadingLevel.HEADING_1,
thematicBreak: true,
});
2018-08-03 23:26:14 +01:00
```
The above example will create a heading with a page break directly under it.
## Page Break
2019-09-30 22:56:21 +01:00
To move to a new page (insert a page break):
2018-08-03 23:26:14 +01:00
2019-08-06 17:15:45 +01:00
```ts
2019-09-30 22:56:21 +01:00
const paragraph = new docx.Paragraph({
2019-11-01 01:57:01 +00:00
children: [new TextRun("Amazing Heading"), new PageBreak()],
2019-09-30 22:56:21 +01:00
});
2018-08-03 23:26:14 +01:00
```
The above example will create a heading and start a new page immediately afterwards.
### Page break before:
2018-08-04 03:28:27 +01:00
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).
2018-08-03 23:26:14 +01:00
2019-08-06 17:15:45 +01:00
```ts
const paragraph = new Paragraph({
text: "Hello World on another page",
pageBreakBefore: true,
});
2018-08-03 23:26:14 +01:00
```

2019-08-20 22:23:14 +01:00
Example: https://github.com/dolanmiu/docx/blob/master/demo/15-page-break-before.ts
2018-08-03 23:26:14 +01:00
## Page break control
2023-03-26 04:05:41 +01:00
Paragraphs have `keepLines` and `keepNext` properties that allow restricting page breaks within and between paragraphs. See [this Microsoft article ](https://support.office.com/en-us/article/Keep-lines-and-paragraphs-together-d72af534-926f-4c4b-830a-abfc2daa3bfa ) for more details.
```ts
const paragraph = new Paragraph({
text: "Stay on the same page",
keepLines: true,
keepNext: true,
});
```