2019-08-06 18:27:00 +01:00
|
|
|
# Text Runs
|
|
|
|
|
|
|
|
!> TextRuns requires an understanding of [Paragraphs](usage/paragraph.md).
|
2018-08-04 03:28:27 +01:00
|
|
|
|
2019-08-03 13:42:24 +01:00
|
|
|
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:
|
2018-08-04 03:28:27 +01:00
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2019-08-06 18:27:00 +01:00
|
|
|
import { Paragraph, TextRun } from "docx";
|
2019-08-03 13:42:24 +01:00
|
|
|
|
|
|
|
const paragraph = new Paragraph({
|
2019-08-06 18:27:00 +01:00
|
|
|
children: [new TextRun("My awesome text here for my university dissertation"), new TextRun("Foo Bar")],
|
2019-08-03 13:42:24 +01:00
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
Text objects have methods inside which changes the way the text is displayed.
|
|
|
|
|
|
|
|
## Typographical Emphasis
|
|
|
|
|
|
|
|
More info [here](https://english.stackexchange.com/questions/97081/what-is-the-typography-term-which-refers-to-the-usage-of-bold-italics-and-unde)
|
|
|
|
|
|
|
|
### Bold
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2019-08-06 18:27:00 +01:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
bold: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Italics
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2019-08-06 18:27:00 +01:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
italics: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Underline
|
|
|
|
|
2019-08-06 18:27:00 +01:00
|
|
|
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:**
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2019-08-06 18:27:00 +01:00
|
|
|
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: {},
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
2020-05-22 12:32:40 +08:00
|
|
|
### Emphasis Mark
|
|
|
|
|
|
|
|
```ts
|
|
|
|
const text = new TextRun({
|
|
|
|
text: "and then emphasis mark",
|
|
|
|
emphasisMark: {},
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-10-28 01:05:31 +00:00
|
|
|
### Shading and Highlighting
|
|
|
|
|
|
|
|
```ts
|
|
|
|
const text = new TextRun({
|
|
|
|
text: "shading",
|
|
|
|
shading: {
|
|
|
|
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
|
|
|
|
color: "00FFFF",
|
|
|
|
fill: "FF0000",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
```ts
|
|
|
|
const text = new TextRun({
|
|
|
|
text: "highlighting",
|
|
|
|
highlight: "yellow",
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2018-08-04 03:28:27 +01:00
|
|
|
### Strike through
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2020-10-28 01:05:31 +00:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "strike",
|
|
|
|
strike: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Double strike through
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2020-10-28 01:05:31 +00:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "doubleStrike",
|
|
|
|
doubleStrike: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Superscript
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2020-10-28 01:05:31 +00:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "superScript",
|
|
|
|
superScript: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Subscript
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2020-10-28 01:05:31 +00:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "subScript",
|
|
|
|
subScript: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### All Capitals
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2020-10-28 01:05:31 +00:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "allCaps",
|
|
|
|
allCaps: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
### Small Capitals
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2020-10-28 01:05:31 +00:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "smallCaps",
|
|
|
|
smallCaps: true,
|
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
## Break
|
|
|
|
|
|
|
|
Sometimes you would want to put text underneath another line of text but inside the same paragraph.
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2020-10-28 01:05:31 +00:00
|
|
|
const text = new TextRun({
|
|
|
|
text: "break",
|
2020-12-23 23:31:28 +00:00
|
|
|
break: 1,
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
Adding two breaks:
|
|
|
|
|
|
|
|
```ts
|
|
|
|
const text = new TextRun({
|
|
|
|
text: "break",
|
|
|
|
break: 2,
|
2020-10-28 01:05:31 +00:00
|
|
|
});
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|