@syncfusion/ej2-docx)
Non-UI library for creating, reading, editing and saving .docx files in Node.js and browsers. No Microsoft Word needed. Unsupported content (unknown XML, parts, relationships) is preserved on open → save round-trips.
Install
npm install @syncfusion/ej2-docx
Import from the package root: import { WordDocument } from '@syncfusion/ej2-docx';.
Navigation guide
Read only the reference needed for the task:
| Task | Read |
|---|---|
| Create / open / save, base64, browser vs Node I/O, errors | references/getting-started.md |
| Paragraphs, text, character & paragraph formatting, lists | references/text-and-formatting.md |
| Tables, cells, merge, table formatting | references/tables.md |
| Images, hyperlinks, fields, content controls | references/inline-content.md |
| Sections, page setup, headers/footers, page numbers | references/sections-headers-footers.md |
| Limits, gotchas, internal/unsupported APIs | references/troubleshooting.md |
Quick start
import { WordDocument } from '@syncfusion/ej2-docx';
const document = WordDocument.create(); // new blank document
const section = document.sections[0]; // add content on the section body
const paragraph = section.body.paragraphs[0]; // default empty paragraph
paragraph.appendText('Bold intro. ', { bold: true });
paragraph.appendText('Normal text.');
const title = section.body.appendParagraph({ text: 'Quarterly Report' });
title.items; // paragraph items (text ranges, tabs, ...)
const table = section.body.appendTable(); // 1x1 table
table.resetCells(2, 2); // resize to 2 rows x 2 cols
table.rows[0].cells[0].text = 'Name';
table.rows[0].cells[1].text = 'Qty';
const bytes: Uint8Array = doc.saveSync(); // OPC zip bytes
// Node: fs.writeFileSync('out.docx', bytes)
// Browser: await doc.save('out.docx') -> triggers a download
Open an existing file:
import { readFileSync } from 'node:fs';
const doc = WordDocument.open(new Uint8Array(readFileSync('in.docx')));
console.log([...doc.sections[0].body.paragraphs].map(p => p.text));
Core object model
WordDocument→sections[]→Section→body(Body),pageSetup,headersFooters.Body/ table cells / headers / footers hold ordered blocks (items):Paragraph,Table, block content controls.body.paragraphsis a typed view.Paragraph→items(TextRange,Tab, fields, hyperlinks, pictures, ...),paragraphFormat,listFormat,text(read-only plain text).TextRange.characterFormatholds run formatting (bold,italic,fontName,fontSize,textColor, ...).
Rules of thumb
- File paths are not read by
open; pass bytes (Uint8Array/ArrayBuffer), aFile/BlobviaopenAsync, or base64 with{ format: 'base64' }. - Node file writing is done by you (
fs.writeFileSync) usingsaveSync()orawait save().save('name.docx')is a browser download only. - Add paragraphs and tables on the section body (
document.sections[0].body.appendParagraph/appendTable), not onWordDocument. Objects must be attached to a document to be mutated (detached ones throwDetachedObjectError). - Verify any API against the
.d.tsof the installed version before using symbols not shown in these references; some members are@internaland are trimmed from published typings. - Do not execute or rely on VBA/OLE/ActiveX content; the library only preserves it.