Files
docx/docs/usage/convenience-functions.md
T
f734656001 build(deps-dev): bump prettier from 3.8.3 to 3.9.8 (#3509)
* build(deps-dev): bump prettier from 3.8.3 to 3.9.8

Bumps [prettier](https://github.com/prettier/prettier) from 3.8.3 to 3.9.8.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/3.8.3...3.9.8)

---
updated-dependencies:
- dependency-name: prettier
  dependency-version: 3.9.8
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* style(docs): reformat code samples for prettier 3.9

Prettier 3.9 keeps arrays and objects containing only a block comment
on a single line (e.g. `[/* ... */]`), which failed the Prettier CI check.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 14:03:29 +01:00

4.0 KiB

Convenience Functions

OOXML and this library mainly uses a unit called twentieths of a point or twip for short. A twip is a typographical measurement, defined as 1/20 of a typographical point. One twip is 1/1440 inch, or 17.64 micrometers.

These functions help convert familiar units to twips.

More info: https://en.wikipedia.org/wiki/Twip

Unit Conversion Functions

Convert Inches to Twip

import { convertInchesToTwip } from "docx";

convertInchesToTwip(1); // returns 1440
convertInchesToTwip(0.5); // returns 720
convertInchesToTwip(2); // returns 2880

Convert Millimeters to Twip

import { convertMillimetersToTwip } from "docx";

convertMillimetersToTwip(25.4); // returns 1440 (1 inch = 25.4mm)
convertMillimetersToTwip(50); // returns 2834
convertMillimetersToTwip(10); // returns 567

Common Use Cases

Setting Page Margins

import { Document, convertInchesToTwip } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    margin: {
                        top: convertInchesToTwip(1), // 1 inch
                        right: convertInchesToTwip(1),
                        bottom: convertInchesToTwip(1),
                        left: convertInchesToTwip(1.5), // 1.5 inches for binding
                    },
                },
            },
            children: [/* ... */],
        },
    ],
});

Setting Page Size

import { Document, convertMillimetersToTwip, PageOrientation } from "docx";

// A4 paper size
const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    size: {
                        width: convertMillimetersToTwip(210), // A4 width
                        height: convertMillimetersToTwip(297), // A4 height
                        orientation: PageOrientation.PORTRAIT,
                    },
                },
            },
            children: [/* ... */],
        },
    ],
});

Column Spacing

import { Document, convertMillimetersToTwip } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                column: {
                    count: 2,
                    space: convertMillimetersToTwip(10), // 10mm between columns
                },
            },
            children: [/* ... */],
        },
    ],
});

Table Cell Width

import { Table, TableCell, TableRow, WidthType, convertInchesToTwip } from "docx";

new Table({
    rows: [
        new TableRow({
            children: [
                new TableCell({
                    width: {
                        size: convertInchesToTwip(2), // 2 inch wide cell
                        type: WidthType.DXA,
                    },
                    children: [new Paragraph("Cell content")],
                }),
            ],
        }),
    ],
});

Quick Reference

Inches to Twips

Inches Twips
0.25 360
0.5 720
0.75 1080
1 1440
1.5 2160
2 2880

Millimeters to Twips (approximate)

Millimeters Twips
5 283
10 567
15 850
20 1134
25 1417
50 2834

EMUs (English Metric Units)

For image positioning, OOXML uses EMUs (English Metric Units). One inch equals 914400 EMUs.

// Image offset of 1 inch
floating: {
    horizontalPosition: {
        offset: 914400,  // 1 inch in EMUs
    },
    verticalPosition: {
        offset: 914400,
    },
}

?> Unlike twips, there are currently no built-in conversion functions for EMUs. You can calculate them manually: inches * 914400 or mm * 36000.