Skip to content

PDFiumDocument

@scaryterry/pdfium


Defined in: src/document/document.ts:122

Represents a loaded PDF document.

Documents must be disposed when no longer needed to free WASM memory. Use the using keyword for automatic disposal.

using document = await pdfium.openDocument(pdfBytes);
console.log(`Document has ${document.pageCount} pages`);
for (const page of document.pages()) {
using p = page;
const text = p.getText();
}

readonly events: EventEmitter<DocumentEvents>

Defined in: src/document/document.ts:129

Event emitter for document events.

get attachmentCount(): number

Defined in: src/document/document.ts:371

Get the number of file attachments in this document.

number

Get the number of attachments.

IDocumentReader.attachmentCount


get disposed(): boolean

Defined in: src/core/disposable.ts:73

Whether this resource has been disposed.

boolean

Disposable.disposed


get duplexMode(): DuplexMode

Defined in: src/document/document.ts:820

Get the duplex printing mode preference.

DuplexMode

The duplex mode setting


get fileVersion(): number | undefined

Defined in: src/document/document.ts:591

Get the PDF file version.

Returns the PDF version as an integer (e.g., 14 for PDF 1.4, 17 for PDF 1.7). Returns undefined if the version cannot be determined.

number | undefined

Get the PDF file version.

IDocumentReader.fileVersion


get formType(): FormType

Defined in: src/document/document.ts:1187

Get the form type of this document.

Returns what type of interactive form (if any) the document contains.

FormType


get javaScriptActionCount(): number

Defined in: src/document/document.ts:980

Get the count of JavaScript actions in this document.

JavaScript actions can be triggered by document events, page events, or user interactions like button clicks.

number

The number of JavaScript actions


get namedDestinationCount(): number

Defined in: src/document/document.ts:883

Get the count of named destinations in this document.

number

The number of named destinations


get numCopies(): number

Defined in: src/document/document.ts:809

Get the number of copies to print by default.

number

The default number of copies


get pageCount(): number

Defined in: src/document/document.ts:209

Get the number of pages in the document.

number

Get the number of pages in the document.

IDocumentReader.pageCount


get pageMode(): PageMode

Defined in: src/document/document.ts:672

Get the document’s initial page mode.

Determines what panel should be displayed when the document is opened.

PageMode

Get the document’s initial page mode.

IDocumentReader.pageMode


get printScaling(): boolean

Defined in: src/document/document.ts:799

Check if print scaling should be applied.

boolean

True if print scaling is enabled, false otherwise


get rawPermissions(): number

Defined in: src/document/document.ts:617

Get the raw document permissions bitmask.

Returns the raw permissions value from the document’s encryption dictionary. Use bitwise operations with DocumentPermission enum to check specific permissions, or use getPermissions for a structured object.

const perms = document.rawPermissions;
const canPrint = (perms & DocumentPermission.Print) !== 0;

number

Get the raw document permissions bitmask.

IDocumentReader.rawPermissions


get securityHandlerRevision(): number

Defined in: src/document/document.ts:685

Get the security handler revision.

Returns the revision number of the document’s security handler, or -1 if the document is not encrypted.

number

Get the security handler revision.

IDocumentReader.securityHandlerRevision


get signatureCount(): number

Defined in: src/document/document.ts:1058

Get the number of digital signatures in this document.

number


get userPermissions(): number

Defined in: src/document/document.ts:662

Get the document user permissions bitmask.

Similar to permissions, but returns the user-level permissions (which may differ from owner permissions for encrypted documents).

number

[dispose](): void

Defined in: src/core/disposable.ts:148

Dispose of this resource, freeing WASM memory.

This method is idempotent - calling it multiple times has no effect after the first call.

void

IDocumentReader.[dispose]

Disposable.[dispose]


addAttachment(name): PDFiumAttachmentWriter | null

Defined in: src/document/document.ts:494

Add a new attachment to the document.

ParameterTypeDescription
namestringThe name of the attachment

PDFiumAttachmentWriter | null

An attachment writer for setting file contents and metadata, or null if failed


attachments(): IterableIterator<PDFAttachment>

Defined in: src/document/document.ts:424

Iterate over file attachments lazily.

Use this for memory-efficient iteration over attachments.

IterableIterator<PDFAttachment>

for (const attachment of document.attachments()) {
console.log(attachment.name);
}

IDocumentReader.attachments


bookmarks(): IterableIterator<Bookmark>

Defined in: src/document/document.ts:326

Iterate over top-level bookmarks lazily.

Each yielded bookmark includes its full subtree of children (eagerly loaded). Use this to avoid building the entire bookmark array up-front.

IterableIterator<Bookmark>

A generator yielding top-level bookmarks with their children

If the bookmark tree depth exceeds the maximum

for (const bookmark of document.bookmarks()) {
console.log(bookmark.title);
}

IDocumentReader.bookmarks


copyViewerPreferences(source): boolean

Defined in: src/document/document.ts:1394

Copy viewer preferences from another document.

Copies settings like print scaling, duplex mode, and display preferences.

ParameterTypeDescription
sourcePDFiumDocumentThe source document to copy preferences from

boolean

True if preferences were successfully copied


createNUpDocument(options): PDFiumDocument | undefined

Defined in: src/document/document.ts:1424

Create a new document with N-up layout (multiple source pages per output page).

This is useful for creating print layouts where multiple pages appear on each sheet (e.g., 2-up, 4-up, 9-up layouts).

Note: The caller is responsible for disposing the returned document.

ParameterTypeDescription
optionsNUpLayoutOptionsN-up layout configuration

PDFiumDocument | undefined

A new document with the N-up layout, or undefined if failed

// Create 2-up layout (2 pages side by side)
using nupDoc = document.createNUpDocument({
outputWidth: 842, // A4 landscape width
outputHeight: 595, // A4 landscape height
pagesPerRow: 2,
pagesPerColumn: 1,
});

deleteAttachment(index): boolean

Defined in: src/document/document.ts:517

Delete an attachment from the document.

ParameterTypeDescription
indexnumberZero-based index of the attachment to delete

boolean

True if successful


dispose(): void

Defined in: src/core/disposable.ts:164

Alias for Symbol.dispose for explicit calls.

void

document.dispose();

Disposable.dispose


executeDocumentAction(actionType): void

Defined in: src/document/document.ts:1461

Execute a document-level action.

This triggers JavaScript or other actions associated with document lifecycle events.

ParameterTypeDescription
actionTypeDocumentActionTypeThe document action type to execute

void

// Execute before-save actions
document.executeDocumentAction(DocumentActionType.WillSave);

executeDocumentJSAction(): void

Defined in: src/document/document.ts:1475

Execute document-level JavaScript actions.

This triggers any JavaScript actions that are set to run at document level.

void


executeDocumentOpenAction(): void

Defined in: src/document/document.ts:1490

Execute the document open action.

This triggers any action set to run when the document is opened. Should be called after the document is fully loaded.

void


getAttachment(index): PDFAttachment

Defined in: src/document/document.ts:383

Get a file attachment by index.

ParameterTypeDescription
indexnumberZero-based attachment index

PDFAttachment

The attachment metadata and data

If the attachment cannot be loaded

IDocumentReader.getAttachment


getAttachments(): PDFAttachment[]

Defined in: src/document/document.ts:444

Get all file attachments in this document.

For documents with many attachments, prefer using the attachments() generator.

PDFAttachment[]

IDocumentReader.getAttachments


getBookmarks(): Bookmark[]

Defined in: src/document/document.ts:306

Get the bookmark (outline) tree for this document.

Returns an array of top-level bookmarks, each with nested children. Returns an empty array if the document has no bookmarks.

For large bookmark trees, prefer the lazy bookmarks generator.

Bookmark[]

IDocumentReader.getBookmarks


getJavaScriptAction(index): JavaScriptAction | undefined

Defined in: src/document/document.ts:1000

Get a JavaScript action by index.

ParameterTypeDescription
indexnumberZero-based index of the JavaScript action

JavaScriptAction | undefined

The JavaScript action with name and script, or undefined


getJavaScriptActions(): JavaScriptAction[]

Defined in: src/document/document.ts:1032

Get all JavaScript actions in this document.

JavaScriptAction[]

Array of JavaScript actions with names and scripts


getMetadata(): DocumentMetadata

Defined in: src/document/document.ts:535

Get all standard metadata fields from the document.

Returns an object containing all available metadata fields. Fields that are not present in the document will not be included.

DocumentMetadata

const metadata = document.getMetadata();
console.log(`Title: ${metadata.title}`);
console.log(`Author: ${metadata.author}`);

IDocumentReader.getMetadata


getMetaText(tag): string | undefined

Defined in: src/document/document.ts:576

Get a specific metadata field by tag name.

Standard tags include: Title, Author, Subject, Keywords, Creator, Producer, CreationDate, ModDate.

ParameterTypeDescription
tagstringThe metadata tag name (case-sensitive)

string | undefined

The metadata value, or undefined if not present

IDocumentReader.getMetaText


getNamedDestinationByName(name): NamedDestination | undefined

Defined in: src/document/document.ts:894

Get a named destination by its name.

ParameterTypeDescription
namestringThe destination name

NamedDestination | undefined

The destination with page index, or undefined if not found

IDocumentReader.getNamedDestinationByName


getNamedDestinations(): NamedDestination[]

Defined in: src/document/document.ts:919

Get all named destinations in this document.

NamedDestination[]

Array of named destinations

IDocumentReader.getNamedDestinations


getPage(pageIndex): PDFiumPage

Defined in: src/document/document.ts:224

Load a specific page from the document.

ParameterTypeDescription
pageIndexnumberZero-based page index

PDFiumPage

The loaded page

If the page cannot be loaded

IDocumentReader.getPage


getPageLabel(pageIndex): string | undefined

Defined in: src/document/document.ts:741

Get the label for a specific page.

PDF documents can have custom page labels (e.g., “i”, “ii”, “1”, “2”). Returns undefined if no label is defined for the page.

ParameterTypeDescription
pageIndexnumberZero-based page index

string | undefined


getPermissions(): DocumentPermissions

Defined in: src/document/document.ts:641

Get structured document permissions with named boolean fields.

This decodes the raw permissions bitmask into a typed object with a named boolean field for each permission.

DocumentPermissions

Structured permissions object

const perms = document.getPermissions();
if (perms.canPrint) {
console.log('Printing is allowed');
}

IDocumentReader.getPermissions


getPrintPageRanges(): number[] | undefined

Defined in: src/document/document.ts:831

Get the print page ranges for this document.

number[] | undefined

Array of page indices to print, or undefined if all pages


getSignature(index): PDFSignature | undefined

Defined in: src/document/document.ts:1076

Get a digital signature by index.

ParameterTypeDescription
indexnumberZero-based signature index

PDFSignature | undefined

Signature information, or undefined if not found


getSignatures(): PDFSignature[]

Defined in: src/document/document.ts:1128

Get all digital signatures in this document.

Returns metadata only (reason, time, sub-filter, contents). Signature verification is not supported by PDFium.

For documents with many signatures, prefer using the signatures() generator.

PDFSignature[]


getTrailerEnds(): number[]

Defined in: src/document/document.ts:712

Get trailer end offsets for incremental saves.

Returns an array of byte offsets where trailer sections end. Useful for determining save points in incrementally saved documents. Returns an empty array if no trailer ends are found.

number[]


getViewerPreference(key): string | undefined

Defined in: src/document/document.ts:861

Get a viewer preference value by name.

ParameterTypeDescription
keystringThe preference key name

string | undefined

The preference value as a string, or undefined if not found


getViewerPreferences(): ViewerPreferences

Defined in: src/document/document.ts:779

Get the viewer preferences for this document.

Returns settings that control how a PDF viewer should display and print the document.

ViewerPreferences

The viewer preferences


hasAcroForm(): boolean

Defined in: src/document/document.ts:1203

Check if this document uses AcroForm (standard PDF forms).

boolean


hasForm(): boolean

Defined in: src/document/document.ts:1196

Check if this document has an interactive form.

boolean


hasJavaScript(): boolean

Defined in: src/document/document.ts:990

Check if this document contains any JavaScript.

boolean

True if the document contains JavaScript actions


hasSignatures(): boolean

Defined in: src/document/document.ts:1066

Check if this document has digital signatures.

boolean


hasValidCrossReferenceTable(): boolean

Defined in: src/document/document.ts:700

Check if the document has a valid cross-reference table.

A valid cross-reference table is required for proper PDF parsing. Documents with damaged cross-reference tables may still open but could have rendering issues.

boolean


hasXFAForm(): boolean

Defined in: src/document/document.ts:1212

Check if this document uses XFA forms.

Note: XFA forms have limited support in PDFium.

boolean


importPages(source, options): void

Defined in: src/document/document.ts:1307

Import pages from another document.

ParameterTypeDescription
sourcePDFiumDocumentThe source document to import pages from
optionsImportPagesOptionsImport options (page range, insert position)

void

If page import fails or the function is unavailable

// Import all pages from source
document.importPages(sourceDoc);
// Import specific pages
document.importPages(sourceDoc, { pageRange: '1-3,5' });
// Insert pages at beginning
document.importPages(sourceDoc, { pageRange: '1', insertIndex: 0 });

importPagesByIndex(source, pageIndices, insertIndex?): void

Defined in: src/document/document.ts:1351

Import pages by their indices.

ParameterTypeDescription
sourcePDFiumDocumentThe source document to import pages from
pageIndicesreadonly number[]Zero-based page indices to import
insertIndex?numberZero-based index where pages should be inserted (default: end)

void

If page import fails or the function is unavailable

// Import pages 0, 2, and 4 from source
document.importPagesByIndex(sourceDoc, [0, 2, 4]);
// Insert at beginning
document.importPagesByIndex(sourceDoc, [0, 1], 0);

isTagged(): boolean

Defined in: src/document/document.ts:762

Check if the document is tagged (accessible).

Tagged PDFs contain structure information for accessibility tools. Returns true if the document’s catalog indicates it is tagged.

boolean


killFormFocus(): boolean

Defined in: src/document/document.ts:1222

Force release of keyboard focus from any form field.

boolean

True if focus was successfully released


pages(): IterableIterator<PDFiumPage>

Defined in: src/document/document.ts:290

Iterate over all pages in the document.

Each page is yielded directly. Throws on failure.

IterableIterator<PDFiumPage>

for (const page of document.pages()) {
using p = page;
// Use the page
}

IDocumentReader.pages


save(options): Uint8Array

Defined in: src/document/document.ts:1268

Save the document to a new byte array.

ParameterTypeDescription
optionsSaveOptionsSave options (flags, version)

Uint8Array

The serialised PDF bytes

If the save operation fails

IDocumentReader.save


setFormFieldHighlightAlpha(alpha): void

Defined in: src/document/document.ts:1253

Set the highlight alpha for form fields.

ParameterTypeDescription
alphanumberAlpha value (0-255)

void


setFormFieldHighlightColour(fieldType, colour): void

Defined in: src/document/document.ts:1236

Set the highlight colour for form fields.

ParameterTypeDescription
fieldTypeFormFieldTypeForm field type to highlight
colourColourColour value for the highlight

void


signatures(): IterableIterator<PDFSignature>

Defined in: src/document/document.ts:1109

Iterate over digital signatures lazily.

Use this for memory-efficient iteration over signatures.

IterableIterator<PDFSignature>

for (const signature of document.signatures()) {
console.log(signature.subFilter);
}