PDFiumDocument
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.
Example
Section titled “Example”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();}Extends
Section titled “Extends”Implements
Section titled “Implements”Properties
Section titled “Properties”events
Section titled “events”
readonlyevents:EventEmitter<DocumentEvents>
Defined in: src/document/document.ts:129
Event emitter for document events.
Accessors
Section titled “Accessors”attachmentCount
Section titled “attachmentCount”Get Signature
Section titled “Get Signature”get attachmentCount():
number
Defined in: src/document/document.ts:371
Get the number of file attachments in this document.
Returns
Section titled “Returns”number
Get the number of attachments.
Implementation of
Section titled “Implementation of”IDocumentReader.attachmentCount
disposed
Section titled “disposed”Get Signature
Section titled “Get Signature”get disposed():
boolean
Defined in: src/core/disposable.ts:73
Whether this resource has been disposed.
Returns
Section titled “Returns”boolean
Inherited from
Section titled “Inherited from”duplexMode
Section titled “duplexMode”Get Signature
Section titled “Get Signature”get duplexMode():
DuplexMode
Defined in: src/document/document.ts:820
Get the duplex printing mode preference.
Returns
Section titled “Returns”The duplex mode setting
fileVersion
Section titled “fileVersion”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”number | undefined
Get the PDF file version.
Implementation of
Section titled “Implementation of”formType
Section titled “formType”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”javaScriptActionCount
Section titled “javaScriptActionCount”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”number
The number of JavaScript actions
namedDestinationCount
Section titled “namedDestinationCount”Get Signature
Section titled “Get Signature”get namedDestinationCount():
number
Defined in: src/document/document.ts:883
Get the count of named destinations in this document.
Returns
Section titled “Returns”number
The number of named destinations
numCopies
Section titled “numCopies”Get Signature
Section titled “Get Signature”get numCopies():
number
Defined in: src/document/document.ts:809
Get the number of copies to print by default.
Returns
Section titled “Returns”number
The default number of copies
pageCount
Section titled “pageCount”Get Signature
Section titled “Get Signature”get pageCount():
number
Defined in: src/document/document.ts:209
Get the number of pages in the document.
Returns
Section titled “Returns”number
Get the number of pages in the document.
Implementation of
Section titled “Implementation of”pageMode
Section titled “pageMode”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”Get the document’s initial page mode.
Implementation of
Section titled “Implementation of”printScaling
Section titled “printScaling”Get Signature
Section titled “Get Signature”get printScaling():
boolean
Defined in: src/document/document.ts:799
Check if print scaling should be applied.
Returns
Section titled “Returns”boolean
True if print scaling is enabled, false otherwise
rawPermissions
Section titled “rawPermissions”Get Signature
Section titled “Get Signature”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.
Example
Section titled “Example”const perms = document.rawPermissions;const canPrint = (perms & DocumentPermission.Print) !== 0;Returns
Section titled “Returns”number
Get the raw document permissions bitmask.
Implementation of
Section titled “Implementation of”IDocumentReader.rawPermissions
securityHandlerRevision
Section titled “securityHandlerRevision”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”number
Get the security handler revision.
Implementation of
Section titled “Implementation of”IDocumentReader.securityHandlerRevision
signatureCount
Section titled “signatureCount”Get Signature
Section titled “Get Signature”get signatureCount():
number
Defined in: src/document/document.ts:1058
Get the number of digital signatures in this document.
Returns
Section titled “Returns”number
userPermissions
Section titled “userPermissions”Get Signature
Section titled “Get Signature”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).
Returns
Section titled “Returns”number
Methods
Section titled “Methods”[dispose]()
Section titled “[dispose]()”[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.
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”Inherited from
Section titled “Inherited from”addAttachment()
Section titled “addAttachment()”addAttachment(
name):PDFiumAttachmentWriter|null
Defined in: src/document/document.ts:494
Add a new attachment to the document.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
name | string | The name of the attachment |
Returns
Section titled “Returns”PDFiumAttachmentWriter | null
An attachment writer for setting file contents and metadata, or null if failed
attachments()
Section titled “attachments()”attachments():
IterableIterator<PDFAttachment>
Defined in: src/document/document.ts:424
Iterate over file attachments lazily.
Use this for memory-efficient iteration over attachments.
Returns
Section titled “Returns”IterableIterator<PDFAttachment>
Example
Section titled “Example”for (const attachment of document.attachments()) { console.log(attachment.name);}Implementation of
Section titled “Implementation of”bookmarks()
Section titled “bookmarks()”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.
Returns
Section titled “Returns”IterableIterator<Bookmark>
A generator yielding top-level bookmarks with their children
Throws
Section titled “Throws”If the bookmark tree depth exceeds the maximum
Example
Section titled “Example”for (const bookmark of document.bookmarks()) { console.log(bookmark.title);}Implementation of
Section titled “Implementation of”copyViewerPreferences()
Section titled “copyViewerPreferences()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
source | PDFiumDocument | The source document to copy preferences from |
Returns
Section titled “Returns”boolean
True if preferences were successfully copied
createNUpDocument()
Section titled “createNUpDocument()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options | NUpLayoutOptions | N-up layout configuration |
Returns
Section titled “Returns”PDFiumDocument | undefined
A new document with the N-up layout, or undefined if failed
Example
Section titled “Example”// 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()
Section titled “deleteAttachment()”deleteAttachment(
index):boolean
Defined in: src/document/document.ts:517
Delete an attachment from the document.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
index | number | Zero-based index of the attachment to delete |
Returns
Section titled “Returns”boolean
True if successful
dispose()
Section titled “dispose()”dispose():
void
Defined in: src/core/disposable.ts:164
Alias for Symbol.dispose for explicit calls.
Returns
Section titled “Returns”void
Example
Section titled “Example”document.dispose();Inherited from
Section titled “Inherited from”executeDocumentAction()
Section titled “executeDocumentAction()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
actionType | DocumentActionType | The document action type to execute |
Returns
Section titled “Returns”void
Example
Section titled “Example”// Execute before-save actionsdocument.executeDocumentAction(DocumentActionType.WillSave);executeDocumentJSAction()
Section titled “executeDocumentJSAction()”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.
Returns
Section titled “Returns”void
executeDocumentOpenAction()
Section titled “executeDocumentOpenAction()”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.
Returns
Section titled “Returns”void
getAttachment()
Section titled “getAttachment()”getAttachment(
index):PDFAttachment
Defined in: src/document/document.ts:383
Get a file attachment by index.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
index | number | Zero-based attachment index |
Returns
Section titled “Returns”The attachment metadata and data
Throws
Section titled “Throws”If the attachment cannot be loaded
Implementation of
Section titled “Implementation of”getAttachments()
Section titled “getAttachments()”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.
Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”IDocumentReader.getAttachments
getBookmarks()
Section titled “getBookmarks()”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.
Returns
Section titled “Returns”Bookmark[]
Implementation of
Section titled “Implementation of”getJavaScriptAction()
Section titled “getJavaScriptAction()”getJavaScriptAction(
index):JavaScriptAction|undefined
Defined in: src/document/document.ts:1000
Get a JavaScript action by index.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
index | number | Zero-based index of the JavaScript action |
Returns
Section titled “Returns”JavaScriptAction | undefined
The JavaScript action with name and script, or undefined
getJavaScriptActions()
Section titled “getJavaScriptActions()”getJavaScriptActions():
JavaScriptAction[]
Defined in: src/document/document.ts:1032
Get all JavaScript actions in this document.
Returns
Section titled “Returns”Array of JavaScript actions with names and scripts
getMetadata()
Section titled “getMetadata()”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.
Returns
Section titled “Returns”Example
Section titled “Example”const metadata = document.getMetadata();console.log(`Title: ${metadata.title}`);console.log(`Author: ${metadata.author}`);Implementation of
Section titled “Implementation of”getMetaText()
Section titled “getMetaText()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
tag | string | The metadata tag name (case-sensitive) |
Returns
Section titled “Returns”string | undefined
The metadata value, or undefined if not present
Implementation of
Section titled “Implementation of”getNamedDestinationByName()
Section titled “getNamedDestinationByName()”getNamedDestinationByName(
name):NamedDestination|undefined
Defined in: src/document/document.ts:894
Get a named destination by its name.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
name | string | The destination name |
Returns
Section titled “Returns”NamedDestination | undefined
The destination with page index, or undefined if not found
Implementation of
Section titled “Implementation of”IDocumentReader.getNamedDestinationByName
getNamedDestinations()
Section titled “getNamedDestinations()”getNamedDestinations():
NamedDestination[]
Defined in: src/document/document.ts:919
Get all named destinations in this document.
Returns
Section titled “Returns”Array of named destinations
Implementation of
Section titled “Implementation of”IDocumentReader.getNamedDestinations
getPage()
Section titled “getPage()”getPage(
pageIndex):PDFiumPage
Defined in: src/document/document.ts:224
Load a specific page from the document.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
pageIndex | number | Zero-based page index |
Returns
Section titled “Returns”The loaded page
Throws
Section titled “Throws”If the page cannot be loaded
Implementation of
Section titled “Implementation of”getPageLabel()
Section titled “getPageLabel()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
pageIndex | number | Zero-based page index |
Returns
Section titled “Returns”string | undefined
getPermissions()
Section titled “getPermissions()”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.
Returns
Section titled “Returns”Structured permissions object
Example
Section titled “Example”const perms = document.getPermissions();if (perms.canPrint) { console.log('Printing is allowed');}Implementation of
Section titled “Implementation of”IDocumentReader.getPermissions
getPrintPageRanges()
Section titled “getPrintPageRanges()”getPrintPageRanges():
number[] |undefined
Defined in: src/document/document.ts:831
Get the print page ranges for this document.
Returns
Section titled “Returns”number[] | undefined
Array of page indices to print, or undefined if all pages
getSignature()
Section titled “getSignature()”getSignature(
index):PDFSignature|undefined
Defined in: src/document/document.ts:1076
Get a digital signature by index.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
index | number | Zero-based signature index |
Returns
Section titled “Returns”PDFSignature | undefined
Signature information, or undefined if not found
getSignatures()
Section titled “getSignatures()”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.
Returns
Section titled “Returns”getTrailerEnds()
Section titled “getTrailerEnds()”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.
Returns
Section titled “Returns”number[]
getViewerPreference()
Section titled “getViewerPreference()”getViewerPreference(
key):string|undefined
Defined in: src/document/document.ts:861
Get a viewer preference value by name.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
key | string | The preference key name |
Returns
Section titled “Returns”string | undefined
The preference value as a string, or undefined if not found
getViewerPreferences()
Section titled “getViewerPreferences()”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.
Returns
Section titled “Returns”The viewer preferences
hasAcroForm()
Section titled “hasAcroForm()”hasAcroForm():
boolean
Defined in: src/document/document.ts:1203
Check if this document uses AcroForm (standard PDF forms).
Returns
Section titled “Returns”boolean
hasForm()
Section titled “hasForm()”hasForm():
boolean
Defined in: src/document/document.ts:1196
Check if this document has an interactive form.
Returns
Section titled “Returns”boolean
hasJavaScript()
Section titled “hasJavaScript()”hasJavaScript():
boolean
Defined in: src/document/document.ts:990
Check if this document contains any JavaScript.
Returns
Section titled “Returns”boolean
True if the document contains JavaScript actions
hasSignatures()
Section titled “hasSignatures()”hasSignatures():
boolean
Defined in: src/document/document.ts:1066
Check if this document has digital signatures.
Returns
Section titled “Returns”boolean
hasValidCrossReferenceTable()
Section titled “hasValidCrossReferenceTable()”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.
Returns
Section titled “Returns”boolean
hasXFAForm()
Section titled “hasXFAForm()”hasXFAForm():
boolean
Defined in: src/document/document.ts:1212
Check if this document uses XFA forms.
Note: XFA forms have limited support in PDFium.
Returns
Section titled “Returns”boolean
importPages()
Section titled “importPages()”importPages(
source,options):void
Defined in: src/document/document.ts:1307
Import pages from another document.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
source | PDFiumDocument | The source document to import pages from |
options | ImportPagesOptions | Import options (page range, insert position) |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If page import fails or the function is unavailable
Example
Section titled “Example”// Import all pages from sourcedocument.importPages(sourceDoc);
// Import specific pagesdocument.importPages(sourceDoc, { pageRange: '1-3,5' });
// Insert pages at beginningdocument.importPages(sourceDoc, { pageRange: '1', insertIndex: 0 });importPagesByIndex()
Section titled “importPagesByIndex()”importPagesByIndex(
source,pageIndices,insertIndex?):void
Defined in: src/document/document.ts:1351
Import pages by their indices.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
source | PDFiumDocument | The source document to import pages from |
pageIndices | readonly number[] | Zero-based page indices to import |
insertIndex? | number | Zero-based index where pages should be inserted (default: end) |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If page import fails or the function is unavailable
Example
Section titled “Example”// Import pages 0, 2, and 4 from sourcedocument.importPagesByIndex(sourceDoc, [0, 2, 4]);
// Insert at beginningdocument.importPagesByIndex(sourceDoc, [0, 1], 0);isTagged()
Section titled “isTagged()”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.
Returns
Section titled “Returns”boolean
killFormFocus()
Section titled “killFormFocus()”killFormFocus():
boolean
Defined in: src/document/document.ts:1222
Force release of keyboard focus from any form field.
Returns
Section titled “Returns”boolean
True if focus was successfully released
pages()
Section titled “pages()”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.
Returns
Section titled “Returns”IterableIterator<PDFiumPage>
Example
Section titled “Example”for (const page of document.pages()) { using p = page; // Use the page}Implementation of
Section titled “Implementation of”save()
Section titled “save()”save(
options):Uint8Array
Defined in: src/document/document.ts:1268
Save the document to a new byte array.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
options | SaveOptions | Save options (flags, version) |
Returns
Section titled “Returns”Uint8Array
The serialised PDF bytes
Throws
Section titled “Throws”If the save operation fails
Implementation of
Section titled “Implementation of”setFormFieldHighlightAlpha()
Section titled “setFormFieldHighlightAlpha()”setFormFieldHighlightAlpha(
alpha):void
Defined in: src/document/document.ts:1253
Set the highlight alpha for form fields.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
alpha | number | Alpha value (0-255) |
Returns
Section titled “Returns”void
setFormFieldHighlightColour()
Section titled “setFormFieldHighlightColour()”setFormFieldHighlightColour(
fieldType,colour):void
Defined in: src/document/document.ts:1236
Set the highlight colour for form fields.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fieldType | FormFieldType | Form field type to highlight |
colour | Colour | Colour value for the highlight |
Returns
Section titled “Returns”void
signatures()
Section titled “signatures()”signatures():
IterableIterator<PDFSignature>
Defined in: src/document/document.ts:1109
Iterate over digital signatures lazily.
Use this for memory-efficient iteration over signatures.
Returns
Section titled “Returns”IterableIterator<PDFSignature>
Example
Section titled “Example”for (const signature of document.signatures()) { console.log(signature.subFilter);}