mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types, the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and the pages side by side, or one of them. The demos a page includes are live too. Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain. Running them showed docs that were wrong, fixed here: - Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page marked as landscape - Comments: `comments.children` takes plain objects, not `new Comment(...)` - Custom properties are text only - APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...), createParagraph().heading1(), a border's `value`, TextRun's `.break()` - The templates and patcher examples read files that aren't in the repository. They now patch the templates in demo/assets, or make their own `npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document "ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example now awaits patchDocument and writes the document it makes, as the example after it does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
5.8 KiB
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
Source: https://github.com/dolanmiu/docx/blob/master/demo/text/check-boxes.ts