Rendering mode
Where layout and paint run.
Parsing always runs in a Worker. Keep the default mode: 'main' for ordinary
previews. Choose mode: 'worker' when document layout and Canvas paint compete
with your application's UI.
main | worker | |
|---|---|---|
| Layout and paint | Main thread | Web Worker |
| Best fit | Most previews and custom renderer objects | Large or complex files |
| Browser requirement | Worker and Canvas | Worker and OffscreenCanvas |
Both modes need to start a module Worker, so opaque-origin iframes such as
sandbox="allow-scripts" are not a supported embedding setup. For a regular
iframe, omit sandbox. To keep sandbox restrictions, serve the viewer
from a dedicated origin separate from the parent and use
sandbox="allow-scripts allow-same-origin" with a src URL.
A srcdoc frame inherits the parent's origin in this setup and cannot provide
that separation. Cross-origin assets still need appropriate CORS permissions.
Ownership
Viewer-owned and shared documents.
The usual path is to construct a Viewer, call load(source), and destroy the
Viewer when its host unmounts. The Viewer owns the loaded document.
Load the document model yourself only when several views must share one parse. Destroy every borrowed view before destroying the shared model.
| Format | Shared model | Borrowing factory |
|---|---|---|
| DOCX | DocxDocument | fromDocument() |
| PPTX | PptxPresentation | fromPresentation() |
| XLSX | XlsxWorkbook | fromWorkbook() |
import { DocxDocument, DocxScrollViewer } from '@silurus/ooxml/docx';
const document = await DocxDocument.load(source, { mode: 'worker' });
const first = DocxScrollViewer.fromDocument(firstContainer, document);
const second = DocxScrollViewer.fromDocument(secondContainer, document);
// Destroy every view before the shared document.
first.destroy();
second.destroy();
document.destroy(); Optional modules
Optional rendering modules.
Equations, ChartEx, 3-D charts, Region Maps, and TIFF images use separate package entries. Import a module once and pass it to the Viewer or document engine. The same option works across DOCX, XLSX, and PPTX, including main and worker rendering modes.
Omit a module when the product does not need that content. Its main-mode implementation stays outside the ordinary format import graph, while built-in capabilities such as classic 2-D charts remain available. The separately loaded render-worker asset is self-contained so it can render enabled first-party modules without another download.
Try Yours and the VS Code extension enable every first-party optional rendering module. Applications built with the library choose their own set using the imports below.
import { XlsxViewer } from '@silurus/ooxml/xlsx';
import { math } from '@silurus/ooxml/math';
import { chartEx } from '@silurus/ooxml/chart-ex';
import { threeD } from '@silurus/ooxml/three-d';
import { regionMap } from '@silurus/ooxml/region-map';
import { tiff } from '@silurus/ooxml/tiff';
const viewer = new XlsxViewer(container, {
math,
chartEx,
threeD,
regionMap,
tiff,
}); | Capability | Import | Viewer option |
|---|---|---|
| Equation renderer | @silurus/ooxml/math | math |
| Microsoft ChartEx renderer | @silurus/ooxml/chart-ex | chartEx |
| 3-D chart renderer | @silurus/ooxml/three-d | threeD |
| Offline Region Map renderer | @silurus/ooxml/region-map | regionMap |
| TIFF image codec | @silurus/ooxml/tiff | tiff |
Experimental legacy Office add-on
Legacy .doc, .xls and .ppt reading is a separate, experimental opt-in package,
@silurus/ooxml-legacy-office. Pass each format's reader as its own option,
such as { legacyDoc }; unsupported content rejects instead of producing a partial
document. The viewer and add-on versions must match exactly.
Install and enable legacy sources →
Experimental OpenDocument add-on
OpenDocument reading is another experimental opt-in package,
@silurus/ooxml-opendocument, installed next to the viewer at the exact same
version. Pass its reader as an option: { odp } from
@silurus/ooxml-opendocument/odp for the PPTX viewers and loaders,
{ odt } for DOCX and { ods } for XLSX.
Install and enable OpenDocument sources →
Standalone TIFF preview
The TIFF module exists to display images embedded in Office files; it is not a general-purpose TIFF library. As a small by-product, applications can reuse it for a simple preview of a standalone TIFF file when that file is in the currently supported class. Unsupported or malformed TIFF variants reject with a diagnostic error.
import { tiff } from '@silurus/ooxml/tiff';
const bytes = new Uint8Array(await file.arrayBuffer());
const bitmap = await tiff.render(bytes);
if (bitmap) {
const canvas = document.getElementById('preview') as HTMLCanvasElement;
canvas.width = bitmap.width;
canvas.height = bitmap.height;
canvas.getContext('2d')?.drawImage(bitmap, 0, 0);
bitmap.close();
}