Files
docx/usage/checkboxes.md
T

5.8 KiB

Checkboxes

!> Checkboxes require an understanding of Paragraphs.

Checkboxes allow you to add interactive checkbox controls to your Word documents. These are commonly used in forms, checklists, and surveys.

?> These checkboxes are interactive in Word - users can click to toggle them on and off.

Basic Checkbox

Create a simple checkbox:

import { CheckBox, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [new CheckBox(), new TextRun(" Unchecked item")],
                }),
            ],
        },
    ],
});

Checked Checkbox

Create a pre-checked checkbox:

import { CheckBox, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [new CheckBox({ checked: true }), new TextRun(" Completed task")],
                }),
            ],
        },
    ],
});

Custom Checkbox Symbols

You can customize the symbols used for checked and unchecked states. This is useful for branding consistency, platform compatibility, or when the default symbols don't render correctly on certain systems:

import { CheckBox, Document, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new CheckBox({
                            checked: true,
                            checkedState: {
                                value: "2611", // Unicode ballot box with check
                                font: "MS Gothic",
                            },
                            uncheckedState: {
                                value: "2610", // Unicode ballot box
                                font: "MS Gothic",
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});

Common Unicode Values

Symbol Unicode Description
☐ 2610 Ballot Box
☑ 2611 Ballot Box with Check
☒ 2612 Ballot Box with X
✓ 2713 Check Mark
✗ 2717 Ballot X

Checkbox Options

Property Type Notes Description
checked boolean Optional Whether the checkbox is checked
checkedState SymbolState Optional Symbol and font for checked state
uncheckedState SymbolState Optional Symbol and font for unchecked state
alias string Optional Accessibility label for the checkbox

SymbolState Options

Property Type Notes Description
value string Required Unicode hex value
font string Optional Font to use for the symbol

Accessibility

Use the alias property to provide an accessibility label:

import { CheckBox, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Do you agree to the terms?"),
                        new TextRun({ break: 1 }),
                        new CheckBox({
                            checked: false,
                            alias: "Terms agreement checkbox",
                        }),
                        new TextRun(" Yes, I agree"),
                    ],
                }),
            ],
        },
    ],
});

Building a Checklist

Create a complete checklist:

import { CheckBox, Document, Paragraph, TextRun } from "docx";

const tasks = [
    { text: "Review documentation", done: true },
    { text: "Write unit tests", done: true },
    { text: "Deploy to production", done: false },
    { text: "Update README", done: false },
];

const doc = new Document({
    sections: [
        {
            children: tasks.map(
                (task) =>
                    new Paragraph({
                        children: [new CheckBox({ checked: task.done }), new TextRun(` ${task.text}`)],
                    }),
            ),
        },
    ],
});

Form with Multiple Checkboxes

import { CheckBox, Document, HeadingLevel, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    text: "Survey Form",
                    heading: HeadingLevel.HEADING_1,
                }),
                new Paragraph({
                    text: "Which features do you use?",
                    heading: HeadingLevel.HEADING_2,
                }),
                new Paragraph({
                    children: [new CheckBox(), new TextRun(" Tables")],
                }),
                new Paragraph({
                    children: [new CheckBox(), new TextRun(" Images")],
                }),
                new Paragraph({
                    children: [new CheckBox(), new TextRun(" Headers/Footers")],
                }),
                new Paragraph({
                    children: [new CheckBox(), new TextRun(" Styles")],
                }),
            ],
        },
    ],
});

Demo

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/text/check-boxes.ts