Skip to main content
Version: Version 5

Interface: Document

Groups all the API actions related to document handling.

Extends​

Properties​

addAnnotation​

addAnnotation: (annotation) => Promise<void>

Adds an annotation to the document.

Parameters​

annotation​

Annotation | Annotation[]

The annotation(s) to add.

Returns​

Promise<void>

A promise that resolves when the annotation (or annotations) have been added.

Throws​

Error if no document is loaded or if the annotation is invalid.

Example​

import { NoteAnnotation, Point, PdfPoint } from '@pdftools/pdf-web-viewer';

const page = viewer.document.getPage(1);
const annotation = new NoteAnnotation({
page,
topLeft: new Point(100, 100) as PdfPoint,
content: 'My note',
});
await viewer.document.addAnnotation(annotation);

deleteAnnotation​

deleteAnnotation: (annotation) => Promise<void>

Deletes the specified annotation from the document.

Parameters​

annotation​

Annotation | Annotation[]

The annotation(s) to be deleted.

Returns​

Promise<void>

A promise that resolves when the annotation (or annotations) are deleted.

Throws​

Error if no document is loaded or if the annotation is invalid.

Example​

await viewer.document.deleteAnnotation(annotation);

download​

download: (fileName, options?) => Promise<void>

Downloads the current document.

Parameters​

fileName​

string

The name of the file to be downloaded (default is ‘download.pdf’).

options?​

DocumentSaveOptions

Document save options

Returns​

Promise<void>

A promise that resolves when the document is downloaded.

Example​

await viewer.document.download();

getAnnotations​

getAnnotations: () => Promise<Annotation[]>

Retrieves all annotations present in the current document.

Returns​

Promise<Annotation[]>

A promise that resolves to an array of annotations.

Example​

const annotations = await viewer.document.getAnnotations();

getOutlines​

getOutlines: () => Promise<OutlineItem[]>

Retrieves all outline items from the current document.

Returns​

Promise<OutlineItem[]>

A promise that resolves to an array of outline items.

Throws​

Error if no document is loaded.

Example​

const outlines = await viewer.document.getOutlines();
outlines.forEach((item) => console.log(item.title));

getPage​

getPage: (pageNumber) => Page

Retrieves a page from the document by its page number.

Parameters​

pageNumber​

number

The page number to retrieve (1-based index).

Returns​

Page

The Page object.

Throws​

Error if no document is loaded or if the page number is invalid.

Example​

const page = viewer.document.getPage(1);

getThumbnail​

getThumbnail: (pageNumber, options?) => Promise<HTMLImageElement>

Retrieves a thumbnail image for a specific page in the document.

Parameters​

pageNumber​

number

The page number for which to retrieve the thumbnail image (1-based index).

options?​

ThumbnailOptions

Options for thumbnail generation including dimensions and format.

Returns​

Promise<HTMLImageElement>

A promise that resolves to the thumbnail image for the specified page.

Example​

const thumbnail = await viewer.document.getThumbnail(1, {
width: 100,
height: 100,
});
const defaultThumbnail = await viewer.document.getThumbnail(1);
const jpegThumbnail = await viewer.document.getThumbnail(1, {
format: 'jpeg',
quality: 0.8,
});

getThumbnails​

getThumbnails: (options?) => Promise<HTMLImageElement[]>

Retrieves thumbnail images for all pages in the document. Supports progressive rendering by using the onThumbnail callback and cancellation through signal.

Parameters​

options?​

ThumbnailOptions

Options for thumbnail generation including dimensions, format, and optional onThumbnail callback for progressive rendering and signal for cancellation.

Returns​

Promise<HTMLImageElement[]>

A promise that resolves to an array of thumbnail images.

Example​

// Basic usage
const thumbnails = await viewer.document.getThumbnails({
width: 100,
height: 100,
});

// Progressive rendering with callback
const abortController = new AbortController();
await viewer.document.getThumbnails({
width: 145,
height: 204,
signal: abortController.signal,
onThumbnail: (thumbnail, pageNumber) => {
container.appendChild(thumbnail);
},
});

open​

open: (inputDocument, options?) => Promise<void>

Opens a document in the PDF viewer.

Parameters​

inputDocument​

InputFile | InputUri

The input document to be opened, either as a File object or a URI string.

options?​

DocumentOpenOptions

Optional settings for opening the document.

Returns​

Promise<void>

A promise that resolves when the document is opened.

Example​

await viewer.document.open({ uri: 'https://example.com/sample.pdf' });
// With options
await viewer.document.open(
{ uri: 'https://example.com/sample.pdf' },
{ useInitialDestination: true }
);

print​

print: (options?) => Promise<void>

Prints the current document.

Parameters​

options?​

DocumentPrintOptions

Document print options.

Returns​

Promise<void>

A promise that resolves when the print dialog is closed.

Example​

await viewer.document.print({ pageNumbers: [1, 2], dpi: 300 });

rotatePage​

rotatePage: (pageNumber, direction, rotation?) => Promise<void>

Rotates a page in the document by the specified degree.

Parameters​

pageNumber​

number

The number of the page to rotate (1-based index).

direction​

RotationDirection

The rotation direction. It can be either clockwise or counter-clockwise.

rotation?​

Rotation

The rotation in degrees. Defaults to 90 degrees if not specified.

Returns​

Promise<void>

A promise that resolves when the page has been rotated.

Throws​

If no document is loaded or if the page number is invalid.

Example​

import { RotationDirection, Rotation } from '@pdftools/pdf-web-viewer';

// Rotate page 1 clockwise by 90 degrees
await viewer.document.rotatePage(1, RotationDirection.clockwise);

// Rotate page 2 counter-clockwise by 180 degrees
await viewer.document.rotatePage(
2,
RotationDirection.counterClockwise,
Rotation.UpsideDown
);

save​

save: (options?) => Promise<Blob>

Saves the current document.

Parameters​

options?​

DocumentSaveOptions

Document save options

Returns​

Promise<Blob>

A promise that resolves to a Blob representing the saved document.

Example​

await viewer.document.save();

updateAnnotation​

updateAnnotation: (annotation) => Promise<void>

Updates the specified annotation in the document.

Parameters​

annotation​

Annotation | Annotation[]

The annotation(s) to be updated.

Returns​

Promise<void>

A promise that resolves when the annotation (or annotations) are updated.

Throws​

Error if no document is loaded or if the annotation is invalid.

Example​

await viewer.document.updateAnnotation(annotation);

Methods​

addEventListener()​

addEventListener<K>(eventName, callback): void

Adds an event listener for a specified event.

Type Parameters​

K​

K extends keyof DocumentEventMap

Parameters​

eventName​

K

The name of the event to listen for.

callback​

DocumentEventMap[K]

The callback function to execute when the event is triggered.

Returns​

void

Inherited from​

EventHandler.addEventListener


getCurrentPageNumber()​

getCurrentPageNumber(): number

Gets the current page number.

Returns​

number

The current page number.

Example​

const currentPage = viewer.document.getCurrentPageNumber();
console.log('Current Page Number:', currentPage);

getPageCount()​

getPageCount(): number

Gets the total number of pages in the document.

Returns​

number

The total number of pages.

Example​

const pageCount = viewer.document.getPageCount();
console.log('Total Pages:', pageCount);

removeEventListener()​

removeEventListener<K>(eventName, callback): void

Removes an event listener for a specified event.

Type Parameters​

K​

K extends keyof DocumentEventMap

Parameters​

eventName​

K

The name of the event to stop listening for.

callback​

DocumentEventMap[K]

The callback function to remove.

Returns​

void

Inherited from​

EventHandler.removeEventListener