Word (DOCX)
Show Word documents (DOCX, DOCM, DOTX) in React with real page layout, styles, tables, headers, footers, footnotes, comments and charts, entirely in the browser.
Word files are laid out into pages by our own parser and layout engine, so what the reader sees follows the document's page size, margins and sections.
File types: .docx, .docm (macros are never run), .dotx. Password-protected files open after the reader enters the password, or with the password prop.
Supported
- Text: paragraph and character styles, theme fonts and colours, lists and numbering, custom tab stops with leaders, links and bookmarks.
- Pages: sections with their own page size and orientation, multi-column sections with column breaks and balanced columns, headers and footers with page numbers, footnotes at the bottom of their page.
- Flow: paragraphs split across pages with widow and orphan control, and keep-with-next.
- Tables: table styles with banding, merged cells, borders, and header rows repeated on each page.
- Pictures and drawings: images with text wrapping, text boxes, charts, SmartArt (from its saved drawing), and EMF/WMF pictures.
- Review: comments in a margin beside each page, with replies and resolved state.
- Navigation: page thumbnails, an outline from headings, search across all pages, and printing at each section's paper size.
Tips
- For line breaks that match Word, install the Office fonts. Without Calibri or its metric twin Carlito, lines wrap a little differently.
- Large files (512 KB and up) are parsed in a Web Worker, so the page stays responsive.
Controller
| State | For a Word document |
|---|---|
pageKind, page, pageCount | 'page', the laid-out page most in view, and the number of pages after layout. Page breaks follow the document's page sizes and margins. |
zoom, scale | Every zoom works, fit modes included. Layout is measured at 100%, so zooming never moves page breaks. |
rotation | Not supported (can.rotate is false). |
outline | The document's headings (Heading 1, 2, 3… and outline levels), nested, each with the page it falls on. |
thumbnails | One per laid-out page. |
search.count | Matches in the body, tables, headers and footers, footnotes and text boxes. |
can | zoom, fit, search, print, download, sidebar. |
Things to know:
pageCountis known once layout finishes, usually a moment afterstatusbecomes'ready'. Showpageonly when it is defined.- For layout that matches Word, install the Office fonts; without them line breaks, and so
pageCount, can differ slightly. - Comments show in the surface's margin; they are not part of the controller.
- Protected documents give
status: 'locked'until the password is entered.
Headless
A reader with a table of contents built from the headings, "Page 3 of 12", whole-word search and printing:
'use client';
// A Word reader: a table of contents built from the document's headings, "Page 3 of 12",
// fit-to-width by default, search and print.
import { useState } from 'react';
import { DocumentSurface, useDocViewer, useDocumentSearch, useOutline, type DocSource, type OutlineItem } from '@anypreview/react';
import '@anypreview/react/styles.css';
export default function WordReader({ source }: { source: DocSource }) {
const viewer = useDocViewer({ source, defaultZoom: 'fit-width' });
const headings = useOutline(viewer); // Heading 1, 2, 3… nested, each with the page it is on
const [query, setQuery] = useState('');
const results = useDocumentSearch(viewer, query, { wholeWord: true });
return (
<div className="flex h-full text-sm">
<aside className="w-52 shrink-0 overflow-y-auto border-r p-3">
<h4 className="mb-2 font-semibold">Contents</h4>
<Toc items={headings} current={viewer.page} onPick={viewer.goToOutline} />
</aside>
<main className="flex min-w-0 flex-1 flex-col">
<header className="flex items-center gap-3 border-b p-2">
<span>{viewer.page ? `Page ${viewer.page} of ${viewer.pageCount}` : 'Loading…'}</span>
<input className="rounded border px-2" placeholder="Find whole words" value={query} onChange={(e) => setQuery(e.target.value)} />
{query && <span>{results.count} found</span>}
<button className="ml-auto" onClick={() => viewer.setZoom(viewer.zoom === 'fit-page' ? 'fit-width' : 'fit-page')}>
{viewer.zoom === 'fit-page' ? 'Fit width' : 'Whole page'}
</button>
<button onClick={() => void viewer.print()}>Print</button>
<button onClick={viewer.download}>Download .docx</button>
</header>
<DocumentSurface viewer={viewer} className="min-h-0 flex-1" />
</main>
</div>
);
}
function Toc({ items, current, onPick }: { items: OutlineItem[]; current?: number; onPick: (item: OutlineItem) => void }) {
return (
<ol className="space-y-1 pl-2">
{items.map((h, i) => (
<li key={i}>
<button className={`text-left ${h.page === current ? 'font-semibold' : ''}`} onClick={() => onPick(h)}>
{h.title} <span className="opacity-60">{h.page}</span>
</button>
{h.items?.length ? <Toc items={h.items} current={current} onPick={onPick} /> : null}
</li>
))}
</ol>
);
}Not yet
- Viewing tracked changes. Documents show their current text.
- Legacy Word 97–2003
.docfiles are detected but not drawn yet. See Coming later.
Headless hooks
Build your own toolbar, search, sidebar or whole viewer with useDocViewer. Full reference of the controller's state and commands, what each format supports, and live examples.
Excel (XLSX)
Show Excel workbooks (XLSX, XLSM, XLTX) in React with sheet tabs, formatting, merges, frozen panes, charts, comments and conditional formatting. Windowed for 100,000-row sheets.