docs: fix mistakes found while answering discussions (#3748)

This commit is contained in:
Dolan
2026-10-12 13:42:45 +01:00
committed by GitHub
parent c9be7acae6
commit 7826217f8d
11 changed files with 229 additions and 42 deletions
+5 -4
View File
@@ -6,11 +6,12 @@ TypeScript library for generating .docx files. Declarative API, works in Node.js
## OOXML Specification
`ooxml-schemas/` contains official ISO-IEC29500 OOXML XSD schemas - the **golden source of truth**. Always reference these when implementing XML elements.
`ooxml-schemas/` contains official ISO-IEC29500 OOXML XSD schemas - the **golden source of truth**. Always reference these when implementing XML elements. `ooxml-schemas/README.md` says what each folder holds.
- `wml.xsd` - WordprocessingML (documents)
- `dml-main.xsd` - DrawingML (images/shapes)
- `shared-math.xsd` - Math
- `ooxml-schemas/ISO-IEC29500-4_2016/wml.xsd` - WordprocessingML (documents)
- `ooxml-schemas/ISO-IEC29500-4_2016/dml-main.xsd` - DrawingML (images/shapes)
- `ooxml-schemas/ISO-IEC29500-4_2016/shared-math.xsd` - Math
- `ooxml-schemas/microsoft/` - Microsoft's extensions, such as `w14:`, `w15:` and Word 2010 shapes (`wps:`)
## Code Conventions
+3 -5
View File
@@ -146,17 +146,15 @@ const doc = new Document({
});
```
2. In browsers, ensure proper data conversion:
2. In browsers, read the image as an `ArrayBuffer` and pass it straight in. `data` takes an `ArrayBuffer`, `Uint8Array`, `Buffer` or base64 string, but not a `Blob`, and browsers have no `Buffer`:
```ts
// Convert fetch response to ArrayBuffer
const response = await fetch(imageUrl);
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const data = await response.arrayBuffer();
new ImageRun({
type: "png",
data: buffer,
data,
transformation: { width: 100, height: 100 },
});
```
+45
View File
@@ -150,6 +150,51 @@ const doc = new Document({
});
```
## Position and Numbering
`footnoteProperties` in a section's `properties` sets where the section's footnotes go and how they're numbered. `NumberFormat.CHICAGO` marks them with \*, †, ‡, § and so on instead of numbers:
```ts live
import { Document, FootnoteReferenceRun, NoteNumberRestart, NumberFormat, Paragraph, TextRun } from "docx";
const doc = new Document({
footnotes: {
1: { children: [new Paragraph("The first footnote is marked with an asterisk.")] },
2: { children: [new Paragraph("The second is marked with a dagger.")] },
},
sections: [
{
properties: {
footnoteProperties: {
numberFormat: { type: NumberFormat.CHICAGO },
restart: NoteNumberRestart.EACH_PAGE,
},
},
children: [
new Paragraph({
children: [
new TextRun("Symbols instead of numbers"),
new FootnoteReferenceRun(1),
new TextRun(" for each footnote"),
new FootnoteReferenceRun(2),
new TextRun("."),
],
}),
],
},
],
});
```
| Property | Type | Notes |
| ------------ | ----------------------------------------- | -------------------------------------------------------------------------------------- |
| numberFormat | `{ type: NumberFormat; format?: string }` | How the marks are numbered, such as `NumberFormat.DECIMAL`, `LOWER_ROMAN` or `CHICAGO` |
| start | `number` | The number of the first footnote |
| restart | `NoteNumberRestart` | `CONTINUOUS`, `EACH_SECTION` or `EACH_PAGE` |
| position | `FootnotePosition` | `PAGE_BOTTOM`, `BENEATH_TEXT`, `SECTION_END` or `DOCUMENT_END` |
Endnotes take the same options as `endnoteProperties`, with `EndnotePosition` (`SECTION_END` or `DOCUMENT_END`) for `position`.
## Options
| Property | Type | Notes | Description |
+44
View File
@@ -541,6 +541,50 @@ const doc = new Document({
| right | `number` | Optional | `0` to `100` |
| bottom | `number` | Optional | `0` to `100` |
## Outline
Draw a border around an image with `outline`. Its `width` is in EMUs, where 12700 is one point and 9525 is one pixel:
```ts live
import { Document, ImageRun, Paragraph } from "docx";
import * as fs from "fs";
const image = new ImageRun({
type: "png",
data: fs.readFileSync("./demo/assets/images/dog.png"),
transformation: {
width: 166,
height: 150,
},
outline: {
type: "solidFill",
solidFillType: "rgb",
value: "1F4E79",
width: 38100,
},
});
const doc = new Document({
sections: [
{
children: [new Paragraph({ children: [image] })],
},
],
});
```
### Options
| Property | Type | Notes |
| ------------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
| type | `"solidFill"` or `"noFill"` | Required. `"noFill"` draws no outline |
| solidFillType | `"rgb"` or `"scheme"` | Required with `"solidFill"` |
| value | `string` | Required with `"solidFill"`. A hex colour such as `"FF0000"`, or a `SchemeColor` for `"scheme"` |
| width | `number` | Optional. In EMUs |
| cap | `"ROUND"`, `"SQUARE"` or `"FLAT"` | Optional. The shape of the line's ends |
| compoundLine | `"SINGLE"`, `"DOUBLE"`, `"THICK_THIN"`, `"THIN_THICK"` or `"TRI"` | Optional. Draws the outline as several lines |
| align | `"CENTER"` or `"INSET"` | Optional. Whether the line is centred on the image's edge or drawn inside it |
## Track Changes
Images can be marked as inserted or deleted revisions for change tracking. Pass an `insertion` or `deletion` property to `ImageRun`, or both for an image that was inserted and then deleted, such as by another author:
+5 -5
View File
@@ -106,11 +106,11 @@ const doc = new Document({
Along with `reference` and `level`, the `numbering` object supports an optional `instance` property:
| Property | Type | Notes |
| --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| reference | `string` | The numbering style reference, must match a defined config |
| level | `number` | The list level (0 = top-level, 1 = sub-list, etc.) |
| instance | `number` | _(Optional)_ Identifies the instance of the list. Lists with the same `instance` (or none) continue numbering across paragraphs. A new `instance` value restarts numbering from `1`. |
| Property | Type | Notes |
| --------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| reference | `string` | The numbering style reference, must match a defined config |
| level | `number` | The list level (0 = top-level, 1 = sub-list, etc.) |
| instance | `number` | _(Optional)_ Identifies the instance of the list. Lists with the same `instance` (or none) continue numbering across paragraphs. A new `instance` value restarts numbering from the first level's `start` (`1` by default). |
Example:
+9 -7
View File
@@ -268,14 +268,16 @@ Adding spacing between paragraphs
### ISpacingProperties
| Property | Type | Notes | Possible Values |
| -------- | -------------- | -------- | -------------------------------------- |
| after | `number` | Optional | |
| before | `number` | Optional | |
| line | `number` | Optional | |
| lineRule | `LineRuleType` | Optional | `AT_LEAST`, `EXACTLY`, `EXACT`, `AUTO` |
| Property | Type | Notes | Possible Values |
| -------- | -------------- | -------- | --------------------------- |
| after | `number` | Optional | |
| before | `number` | Optional | |
| line | `number` | Optional | |
| lineRule | `LineRuleType` | Optional | `AT_LEAST`, `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.
With `AUTO`, the default, `line` is in 240ths of a line, so `240` is single spacing and `360` is one and a half lines. With `EXACT` or `AT_LEAST`, `line` is a height in twips, so `360` is 18pt.
Note: Use `EXACT` for an exact line height. `LineRuleType.EXACTLY` writes `exactly`, which isn't a value the OOXML schema allows (it allows `auto`, `exact` and `atLeast`), so Word ignores it. It's kept so existing code still compiles. Read this issue for more information: https://github.com/dolanmiu/docx/issues/1773.
**Example:**
+48 -8
View File
@@ -206,16 +206,56 @@ const doc = new Document({
});
```
### Document Grid
The document grid sets a fixed number of lines per page and characters per line, as East Asian documents often do. It's Word's Layout → Page Setup → Document Grid, and is set with `grid` in a section's `properties`:
- `type`: `DocumentGridType.LINES` fixes only the line pitch. `LINES_AND_CHARS` fixes the line pitch and the character pitch. `SNAP_TO_CHARS` snaps characters to the grid. `DEFAULT` has no grid.
- `linePitch`: the height of each line, in twips (twentieths of a point).
- `charSpace`: how much wider each character is than the Normal font size, in 4096ths of a point. For a number of characters per line, use `(text width in points / characters per line - Normal font size in points) * 4096`.
This example has 28 characters per line: the text of an A4 page with the default margins is 451.3pt wide, so with 10.5pt text, `charSpace` is `(451.3 / 28 - 10.5) * 4096`, which is about 23010.
```ts live
import { Document, DocumentGridType, Paragraph } from "docx";
const doc = new Document({
styles: {
default: {
// 10.5pt Normal text
document: { run: { size: 21 } },
},
},
sections: [
{
properties: {
grid: {
type: DocumentGridType.LINES_AND_CHARS,
linePitch: 312,
charSpace: 23010,
},
},
children: [
new Paragraph("这一段的每一行有二十八个字。文档网格让每个汉字占据相同的宽度,所以每行的字数是固定的,就像稿纸一样。"),
],
},
],
});
```
## Section Properties Reference
| Property | Type | Description |
| ------------- | ----------------------- | --------------------------------------- |
| type | `SectionType` | How section starts relative to previous |
| page | `IPageSizeAttributes` | Page size, margins, borders |
| column | `IColumnsAttributes` | Multi-column configuration |
| titlePage | `boolean` | Different first page header/footer |
| lineNumbers | `ILineNumberAttributes` | Line numbering configuration |
| verticalAlign | `VerticalAlign` | Vertical alignment of content |
| Property | Type | Description |
| ------------------ | ------------------------------ | --------------------------------------------------------------------------------------------------------------- |
| type | `SectionType` | How section starts relative to previous |
| page | `IPageSizeAttributes` | Page size, margins, borders |
| column | `IColumnsAttributes` | Multi-column configuration |
| titlePage | `boolean` | Different first page header/footer |
| lineNumbers | `ILineNumberAttributes` | Line numbering configuration |
| verticalAlign | `VerticalAlign` | Vertical alignment of content |
| grid | `IDocGridAttributesProperties` | Lines per page and characters per line, see [Document Grid](#document-grid) |
| footnoteProperties | `IFootnotePropertiesOptions` | Where footnotes go and how they're numbered, see [Footnotes](usage/footnotes.md#position-and-numbering) |
| endnoteProperties | `IEndnotePropertiesOptions` | Where endnotes go and how they're numbered, with the same options as `footnoteProperties` and `EndnotePosition` |
## Related Topics
+44 -10
View File
@@ -299,6 +299,39 @@ const doc = new Document({
});
```
#### Keep a table on one page
Word has no setting that keeps a whole table on one page. Instead, set `keepNext` on the paragraphs of every row except the last, so each row stays on the same page as the row after it, and `cantSplit` on every row. If the last row has `keepNext` too, the table is also kept with whatever follows it. A table taller than a page still splits.
```ts live
import { Document, Paragraph, Table, TableCell, TableRow } from "docx";
const fruit = ["Apples", "Pears", "Figs", "Plums"];
const table = new Table({
rows: fruit.map(
(name, index) =>
new TableRow({
cantSplit: true,
children: [
new TableCell({
// Every row but the last is kept with the next row
children: [new Paragraph({ text: name, keepNext: index < fruit.length - 1 })],
}),
],
}),
),
});
const doc = new Document({
sections: [
{
children: [new Paragraph("If the table doesn't fit at the bottom of a page, all of it moves to the next page."), table],
},
],
});
```
## Table Cells
Cells need to be added in the `table row`, you can create a table cell like:
@@ -343,16 +376,17 @@ const doc = new Document({
### Options
| Property | Type | Notes |
| ------------- | ---------------------------------- | ----------------------------------------------------------- |
| children | `Array<Paragraph or Table>` | Required. You can nest tables by adding a table into a cell |
| shading | `IShadingAttributesProperties` | Optional |
| margins | `ITableCellMarginOptions` | Optional |
| verticalAlign | `VerticalAlignTable` | Optional |
| columnSpan | `number` | Optional |
| rowSpan | `number` | Optional |
| borders | `BorderOptions` | Optional |
| width | `{ size: number type: WidthType }` | Optional |
| Property | Type | Notes |
| ------------- | ---------------------------------- | --------------------------------------------------------------------------------------------------------- |
| children | `Array<Paragraph or Table>` | Required. You can nest tables by adding a table into a cell |
| shading | `IShadingAttributesProperties` | Optional |
| margins | `ITableCellMarginOptions` | Optional |
| verticalAlign | `VerticalAlignTable` | Optional |
| textDirection | `TextDirection` | Optional. `BOTTOM_TO_TOP_LEFT_TO_RIGHT` or `TOP_TO_BOTTOM_RIGHT_TO_LEFT` turns the text to run vertically |
| columnSpan | `number` | Optional |
| rowSpan | `number` | Optional |
| borders | `BorderOptions` | Optional |
| width | `{ size: number type: WidthType }` | Optional |
#### Border Options
+1 -1
View File
@@ -225,7 +225,7 @@ const doc = new Document({
});
```
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.
`TabStopPosition.MAX` is a fixed 9026 twips from the left margin: the width of the text on an A4 page with the default 1 inch margins. With other margins or page sizes it isn't at the right margin. Work out the position from your own page width minus the left and right margins, or use a [positional tab](#positional-tabs), which lines up with the right margin whatever the margins are.
## Positional Tabs
+23
View File
@@ -446,3 +446,26 @@ const doc = new Document({
],
});
```
## Hyphens
`NoBreakHyphen` is a hyphen that a line never breaks at, the character Word inserts for Ctrl+Shift+-. It keeps a negative number or a hyphenated name on one line. `SoftHyphen` is an optional hyphen: it's hidden unless Word breaks the line there.
```ts live
import { Document, NoBreakHyphen, Paragraph, SoftHyphen, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [new TextRun({ children: ["The balance is ", new NoBreakHyphen(), "1,234.56"] })],
}),
new Paragraph({
children: [new TextRun({ children: ["Extra", new SoftHyphen(), "ordinary"] })],
}),
],
},
],
});
```
+2 -2
View File
@@ -20,9 +20,9 @@ import { BuilderElement, type XmlComponent } from "@file/xml-components";
export const LineRuleType = {
/** Line spacing is at least the specified value */
AT_LEAST: "atLeast",
/** Line spacing is exactly the specified value */
/** Writes `exactly`, which isn't a value the schema allows, so Word ignores it. Use {@link LineRuleType.EXACT} for an exact line height. */
EXACTLY: "exactly",
/** Line spacing is exactly the specified value (alias for EXACTLY) */
/** Line spacing is exactly the specified value */
EXACT: "exact",
/** Line spacing is automatically determined based on content */
AUTO: "auto",