Skip to main content

API Reference

<DocViewer> React Component

The main React component. Renders a document viewer with toolbar, search, zoom, and all features.

import { DocViewer } from 'doclens';

<DocViewer
document={{ uri: '/report.pdf' }}
theme="dark"
enableOCR
onDocumentLoad={(meta) => console.log(meta)}
/>

Props

PropTypeDefaultDescription
documentDocumentSourceSingle document source
documentsDocumentSource[]Multiple documents (renders tabs)
activeDocumentnumber0Active document index
initialSearchTermsstring[]Terms to highlight on load
theme'light' | 'dark' | ThemeConfig'light'Theme
headerboolean | HeaderConfigtrueToolbar configuration
defaultZoomnumber1Initial zoom level
minZoomnumber0.25Minimum zoom
maxZoomnumber5Maximum zoom
zoomStepnumber0.25Zoom increment
disableSelectionbooleanfalseDisable text selection
disablePrintbooleanfalseDisable printing
enableOCRbooleanfalseEnable OCR (requires tesseract.js)
heightstring | number'100%'Viewer height
widthstring | number'100%'Viewer width
onDocumentLoad(meta: DocumentMeta) => voidDocument loaded
onDocumentChange(index: number, doc: DocumentSource) => voidTab switch
onError(error: Error) => voidError
onSearchChange(query: string, results: SearchResult[]) => voidSearch results
onDrop(files: File[]) => voidFile drop
renderHeaderLeft() => ReactNodeLeft header slot
renderHeaderCenter() => ReactNodeCenter header slot
renderHeaderRight() => ReactNodeRight header slot
classNamestringRoot class name
styleCSSPropertiesRoot styles

DocViewerEngine

The framework-agnostic core engine. Used directly for vanilla JS or advanced React integrations.

import { DocViewerEngine } from 'doclens/core';

const engine = new DocViewerEngine(container, options, engineMode?);

Constructor

ParameterTypeDescription
containerHTMLElementDOM element to render into
optionsDocViewerOptionsConfiguration options
engineModeEngineModeOptional: { mode: 'managed' | 'headless', viewerEl? }

Methods

MethodReturnsDescription
load(source?)Promise<void>Load and render a document
loadDocument(index)Promise<void>Switch to document by index
search(query)SearchResult[]Search and highlight
nextMatch()numberNavigate to next match
prevMatch()numberNavigate to previous match
clearSearch()voidClear search highlights
getSearchResults()SearchResult[]Get current results
getActiveSearchIndex()numberGet active match index
setZoom(level)voidSet zoom level
getZoom()numberGet current zoom
zoomIn()voidZoom in by step
zoomOut()voidZoom out by step
resetZoom()voidReset to 1x
fitToWidth()voidFit to container width
getPageCount()numberTotal pages
getCurrentPage()numberCurrent page
goToPage(page)voidNavigate to page
rotate(degrees?)voidRotate document
getMeta()DocumentMeta | nullGet document metadata
getFileType()FileType | nullGet detected file type
destroy()voidClean up everything

Events

EventPayloadDescription
loadDocumentMetaDocument loaded successfully
errorErrorError occurred
search{ query, results }Search results updated
zoom{ level }Zoom level changed
pageChange{ page }Page changed
documentChange{ index, document }Active document changed
ocrProgress{ processing }OCR started/finished
engine.on('load', (meta) => { });
engine.off('load', callback);

React Hooks

useSearch()

const {
query, // string — current query
results, // SearchResult[] — all matches
count, // number — total count
activeIndex, // number — currently highlighted
search, // (query: string) => void
nextMatch, // () => void
prevMatch, // () => void
clearSearch, // () => void
} = useSearch();

useZoom()

const {
level, // number — current zoom (e.g. 1.5)
percentage, // number — as percentage (e.g. 150)
setZoom, // (level: number) => void
zoomIn, // () => void
zoomOut, // () => void
resetZoom, // () => void
fitToWidth, // () => void
} = useZoom();

useDocViewerContext()

Access the full state and actions:

const { state, actions } = useDocViewerContext();

Types

DocumentSource

interface DocumentSource {
uri?: string;
fileData?: ArrayBuffer | Blob | Uint8Array | File;
fileName?: string;
fileType?: string;
requestInit?: RequestInit;
}

DocumentMeta

interface DocumentMeta {
fileName: string;
fileType: FileType;
pageCount?: number;
fileSize: number;
}

SearchResult

interface SearchResult {
index: number;
node: HTMLElement;
text: string;
page?: number;
termIndex?: number;
}

FileType

type FileType =
| 'pdf' | 'xlsx' | 'xls' | 'csv' | 'pptx' | 'docx'
| 'xml' | 'json' | 'html' | 'markdown'
| 'image' | 'video' | 'audio' | 'text';

ThemePreset

type ThemePreset = 'light' | 'dark';

ThemeConfig

interface ThemeConfig {
preset?: ThemePreset;
variables?: Record<string, string>;
}

HeaderConfig

interface HeaderConfig {
show?: boolean;
fileName?: boolean;
search?: boolean;
zoom?: boolean;
fullscreen?: boolean;
download?: boolean;
print?: boolean;
pageNav?: boolean;
}

OutlineItem

interface OutlineItem {
title: string;
page?: number;
children?: OutlineItem[];
}

DocViewerEvent

type DocViewerEvent =
| 'load' | 'error' | 'search' | 'zoom'
| 'pageChange' | 'documentChange' | 'ocrProgress';