mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* chore: organise the demos into a folder for each topic The demos were one flat list of 124 numbered files, which made it hard to find the one you wanted. Each is now in a folder for its topic, such as demo/tables/basic-table.ts, with a name that says what it shows instead of a number. npm run demo lists every demo and narrows the list as you type. It also takes a demo's path or name, a topic, or search words. The demos' images moved to demo/assets/images, as images is now a topic. The docs' links, the Demos workflow, the shape render script and its reference images use the new paths and names. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * fix: list only a topic's own demos, and drop stale output file names npm run demo text also offered the text-boxes demos and others with "text" in their name, so a topic now searches for "text/". Three demos logged their old numbered file name after writing My Document.docx. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
169 lines
4.7 KiB
Markdown
169 lines
4.7 KiB
Markdown
# Checkboxes
|
|
|
|
!> Checkboxes require an understanding of [Paragraphs](usage/paragraph.md).
|
|
|
|
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:
|
|
|
|
```ts
|
|
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:
|
|
|
|
```ts
|
|
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:
|
|
|
|
```ts
|
|
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:
|
|
|
|
```ts
|
|
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:
|
|
|
|
```ts
|
|
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
|
|
|
|
```ts
|
|
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](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/text/check-boxes.ts ":include")
|
|
|
|
_Source: https://github.com/dolanmiu/docx/blob/master/demo/text/check-boxes.ts_
|