# PdfDocument

## Content

[**DsPdfJS API v9.1.3**](../README)

***

[DsPdfJS API](../globals) / PdfDocument

# Class: PdfDocument

Represents a PDF document that can be created from scratch or loaded from existing data.
Provides methods for creating, loading, and manipulating PDF documents.

## Examples

```ts
// Create a new PDF document
const { connectDsPdf, ObjectManager, PdfDocument } = require("@mescius/ds-pdf");

async function createNewPdf() {
  await connectDsPdf();
  const om = new ObjectManager();  
  const doc = new PdfDocument(om);
  
  for (let p = 0; p < 10; p++) {
    doc.pages.addNew();
  }
  
  const pdfData = doc.savePdf();
  fs.writeFileSync("new-document.pdf", pdfData);
  om.dispose();
}
```

```ts
// Load an existing PDF document
async function loadExistingPdf() {
  await connectDsPdf();
  using om = new ObjectManager();
  const pdfBytes = fs.readFileSync("existing.pdf");
  
  const doc = PdfDocument.load(om, pdfBytes);
  // Work with the loaded document...
}
```

```ts
// Load a password-protected PDF
async function loadProtectedPdf() {
  await connectDsPdf();
  pushObjectManager();
  const pdfBytes = fs.readFileSync("protected.pdf");
  
  const decryptionOptions = { password: "secret123" };
  const doc = PdfDocument.load(pdfBytes, decryptionOptions);
  // Work with the loaded document...
  popObjectManager();
}
```

## Extends

- [`ObjectBase`](ObjectBase)

## Constructors

### Constructor

> **new PdfDocument**(`om`, `options?`): `PdfDocument`

Creates a new PDF document instance.

#### Parameters

##### om

[`ObjectManager`](ObjectManager)

[ObjectManager](ObjectManager) that controls the object lifetime

##### options?

[`PdfDocumentOptions`](../type-aliases/PdfDocumentOptions)

Creation options for new documents (optional)

#### Returns

`PdfDocument`

#### Examples

```ts
// Create empty document
const doc1 = new PdfDocument(om);
```

```ts
// Create document with options
const options = { compressionLevel: "Optimal", conformanceLevel: "PdfA1a" };
const doc2 = new PdfDocument(om, options);
```

#### Overrides

`ObjectBase.constructor`

### Constructor

> **new PdfDocument**(`options?`): `PdfDocument`

Creates a new PDF document instance.

#### Parameters

##### options?

[`PdfDocumentOptions`](../type-aliases/PdfDocumentOptions)

Creation options for new documents (optional)

#### Returns

`PdfDocument`

#### Examples

```ts
// Create empty document
const doc1 = new PdfDocument();
```

```ts
// Create document with options
const options = { compressionLevel: "Optimal", conformanceLevel: "PdfA1a" };
const doc2 = new PdfDocument(options);
```

#### Overrides

`ObjectBase.constructor`

## Accessors

### acroForm

#### Get Signature

> **get** **acroForm**(): [`AcroForm`](AcroForm)

Gets the [AcroForm](AcroForm) object defining common properties of the AcroForms in this document.

##### Returns

[`AcroForm`](AcroForm)

***

### compressionLevel

#### Get Signature

> **get** **compressionLevel**(): [`CompressionLevel`](../enumerations/CompressionLevel)

Gets or sets the compression level. Default value is [CompressionLevel#Fastest](../enumerations/CompressionLevel#fastest).

##### Returns

[`CompressionLevel`](../enumerations/CompressionLevel)

#### Set Signature

> **set** **compressionLevel**(`value`): `void`

Gets or sets the compression level. Default value is [CompressionLevel#Fastest](../enumerations/CompressionLevel#fastest).

##### Parameters

###### value

[`CompressionLevel`](../enumerations/CompressionLevel)

##### Returns

`void`

***

### didPrintAction

#### Get Signature

> **get** **didPrintAction**(): [`ActionJavaScript`](ActionJavaScript) \| `null`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed after printing the document.

##### Returns

[`ActionJavaScript`](ActionJavaScript) \| `null`

#### Set Signature

> **set** **didPrintAction**(`value`): `void`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed after printing the document.

##### Parameters

###### value

[`ActionJavaScriptProperties`](../type-aliases/ActionJavaScriptProperties) | [`ActionJavaScript`](ActionJavaScript) | `null`

##### Returns

`void`

***

### didSaveAction

#### Get Signature

> **get** **didSaveAction**(): [`ActionJavaScript`](ActionJavaScript) \| `null`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed after saving the document.

##### Returns

[`ActionJavaScript`](ActionJavaScript) \| `null`

#### Set Signature

> **set** **didSaveAction**(`value`): `void`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed after saving the document.

##### Parameters

###### value

[`ActionJavaScriptProperties`](../type-aliases/ActionJavaScriptProperties) | [`ActionJavaScript`](ActionJavaScript) | `null`

##### Returns

`void`

***

### documentInfo

#### Get Signature

> **get** **documentInfo**(): [`DocumentInfo`](DocumentInfo) \| `null`

Gets or sets the [DocumentInfo](DocumentInfo) object that contains information about
this document (author, title, etc).

##### Returns

[`DocumentInfo`](DocumentInfo) \| `null`

#### Set Signature

> **set** **documentInfo**(`docInfo`): `void`

Gets or sets the [DocumentInfo](DocumentInfo) object that contains information about
this document (author, title, etc).

##### Example

```ts
const doc = new PdfDocument();
doc.documentInfo = {
    title: "Document Info Sample",
    author: "John Doe",
    subject: "DsPdfJS PdfDocumentInfo",
    creationDate: new Date()
};
const res: Uint8Array = doc.savePdf();
```

##### Parameters

###### docInfo

[`DocumentInfo`](DocumentInfo) | [`DocumentInfoProperties`](../type-aliases/DocumentInfoProperties) | `null`

##### Returns

`void`

***

### embeddedFiles

#### Get Signature

> **get** **embeddedFiles**(): [`FileSpecificationMap`](FileSpecificationMap)

Gets the dictionary of document level file attachments.

##### Returns

[`FileSpecificationMap`](FileSpecificationMap)

***

### fileID

#### Get Signature

> **get** **fileID**(): [`FileID`](../type-aliases/FileID) \| `null`

Gets or sets the [FileID](../type-aliases/FileID) object defining ID of this PDF document.
Note that this ID is automatically updated if the [clear](#clear) method is called.

##### Returns

[`FileID`](../type-aliases/FileID) \| `null`

#### Set Signature

> **set** **fileID**(`value`): `void`

Gets or sets the [FileID](../type-aliases/FileID) object defining ID of this PDF document.
Note that this ID is automatically updated if the [clear](#clear) method is called.

##### Parameters

###### value

[`FileID`](../type-aliases/FileID) | `null`

##### Returns

`void`

***

### fontCollection

#### Get Signature

> **get** **fontCollection**(): [`FontCollection`](FontCollection) \| `null`

Gets or sets the [FontCollection](FontCollection) object used when the PdfDocument needs to find a Font (e.g. if it is not embedded in the PDF).

##### Returns

[`FontCollection`](FontCollection) \| `null`

#### Set Signature

> **set** **fontCollection**(`coll`): `void`

Gets or sets the [FontCollection](FontCollection) object used when the PdfDocument needs to find a Font (e.g. if it is not embedded in the PDF).

##### Parameters

###### coll

[`FontCollection`](FontCollection) | `null`

##### Returns

`void`

***

### fontEmbedMode

#### Get Signature

> **get** **fontEmbedMode**(): [`FontEmbedMode`](../enumerations/FontEmbedMode)

Gets or sets the font embedding mode.
The default is [FontEmbedMode.EmbedSubset](../enumerations/FontEmbedMode#embedsubset).
Note that this property does not affect the 14 standard PDF fonts,
their embedding is determined by the [PdfDocument#standardFontEmbedMode](#standardfontembedmode) property.
Also note that if the PdfDocument is saved as PDF/A,
and the value of this property is [FontEmbedMode.NotEmbed](../enumerations/FontEmbedMode#notembed),
the fonts are embedded anyway using the [FontEmbedMode.EmbedSubset](../enumerations/FontEmbedMode#embedsubset) mode.

##### Returns

[`FontEmbedMode`](../enumerations/FontEmbedMode)

#### Set Signature

> **set** **fontEmbedMode**(`value`): `void`

Gets or sets the font embedding mode.
The default is [FontEmbedMode.EmbedSubset](../enumerations/FontEmbedMode#embedsubset).
Note that this property does not affect the 14 standard PDF fonts,
their embedding is determined by the [PdfDocument#standardFontEmbedMode](#standardfontembedmode) property.
Also note that if the PdfDocument is saved as PDF/A,
and the value of this property is [FontEmbedMode.NotEmbed](../enumerations/FontEmbedMode#notembed),
the fonts are embedded anyway using the [FontEmbedMode.EmbedSubset](../enumerations/FontEmbedMode#embedsubset) mode.

##### Parameters

###### value

[`FontEmbedMode`](../enumerations/FontEmbedMode)

##### Returns

`void`

***

### fontHandlers

#### Get Signature

> **get** **fontHandlers**(): [`FontHandlerCollection`](FontHandlerCollection)

Gets the collection of font handlers associated with the current document.

##### Returns

[`FontHandlerCollection`](FontHandlerCollection)

***

### id

#### Get Signature

> **get** **id**(): `number`

Gets the reference to the object.

##### Returns

`number`

#### Inherited from

[`ObjectBase`](ObjectBase).[`id`](ObjectBase#id)

***

### imageHandlers

#### Get Signature

> **get** **imageHandlers**(): [`PdfImageHandlerCollection`](PdfImageHandlerCollection)

Gets the collection of [PdfImageHandler](PdfImageHandler) objects associated with the current document.

##### Returns

[`PdfImageHandlerCollection`](PdfImageHandlerCollection)

***

### imageOptions

#### Get Signature

> **get** **imageOptions**(): [`ImageOptions`](../type-aliases/ImageOptions)

Gets or sets the [ImageOptions](../type-aliases/ImageOptions) object that contains options
controlling how images are processed in the current document.

##### Returns

[`ImageOptions`](../type-aliases/ImageOptions)

#### Set Signature

> **set** **imageOptions**(`value`): `void`

Gets or sets the [ImageOptions](../type-aliases/ImageOptions) object that contains options
controlling how images are processed in the current document.

##### Parameters

###### value

[`ImageOptions`](../type-aliases/ImageOptions)

##### Returns

`void`

***

### isNewPdf

#### Get Signature

> **get** **isNewPdf**(): `boolean`

Gets a value indicating if the PDF document was created from scratch.

##### Returns

`boolean`

***

### javaScripts

#### Get Signature

> **get** **javaScripts**(): [`ActionJavaScriptMap`](ActionJavaScriptMap)

Gets the document-level java scripts as a dictionary where key is a custom user defined name
and value is a [ActionJavaScript](ActionJavaScript) object containing a JavaScript associated with a name.

##### Returns

[`ActionJavaScriptMap`](ActionJavaScriptMap)

***

### linearized

#### Get Signature

> **get** **linearized**(): `boolean`

Gets a value indicating whether the PDF was linearized ("fast web view").

##### Returns

`boolean`

***

### metadata

#### Get Signature

> **get** **metadata**(): [`Metadata`](Metadata) \| `null`

Gets or sets the metadata associated with this document.

##### Returns

[`Metadata`](Metadata) \| `null`

#### Set Signature

> **set** **metadata**(`meta`): `void`

Gets or sets the metadata associated with this document.

##### Parameters

###### meta

[`Metadata`](Metadata) | [`MetadataProperties`](../type-aliases/MetadataProperties) | `null`

##### Returns

`void`

***

### namedDestinations

#### Get Signature

> **get** **namedDestinations**(): [`DocumentDestinations`](DocumentDestinations)

Gets the dictionary of named destinations defined in the current document.

##### Returns

[`DocumentDestinations`](DocumentDestinations)

***

### om

#### Get Signature

> **get** **om**(): [`ObjectManager`](ObjectManager)

Gets the owner [ObjectManager](ObjectManager) instance.

##### Returns

[`ObjectManager`](ObjectManager)

#### Inherited from

[`ObjectBase`](ObjectBase).[`om`](ObjectBase#om)

***

### openAction

#### Get Signature

> **get** **openAction**(): [`DocAction`](DocAction) \| `null`

Gets or sets a [DocAction](DocAction) to be displayed or performed when the document is opened.

##### Returns

[`DocAction`](DocAction) \| `null`

#### Set Signature

> **set** **openAction**(`value`): `void`

Gets or sets a [DocAction](DocAction) to be displayed or performed when the document is opened.

##### Parameters

###### value

[`DocAction`](DocAction) | [`DocActionProperties`](../type-aliases/DocActionProperties) | `null`

##### Returns

`void`

***

### outlines

#### Get Signature

> **get** **outlines**(): [`OutlineNodeCollection`](OutlineNodeCollection)

Gets the collection of the current document outlines.

##### Returns

[`OutlineNodeCollection`](OutlineNodeCollection)

***

### pages

#### Get Signature

> **get** **pages**(): [`PdfPageCollection`](PdfPageCollection)

Gets a [PdfPageCollection](PdfPageCollection) with document pages.

##### Examples

```ts
for (const page of doc.pages)
{
    const ctx = page.context;
    ctx.drawText(...);
}
```

```ts
import JSZip from 'jszip';

const doc = PdfDocument.load(await Util.loadPdfAsArray("document.pdf"));
const coll = doc.pages;
const pageCount = coll.count;
const zip = new JSZip();

for (let num = 1; num <= pageCount; num++) {
    const page = coll.getAt(num - 1);
    const svgBytes: Uint8Array = page.saveAsSvg({ zoom: 2 });
    zip.file(`page${num}.svg`, svgBytes, { binary: true });
}

const zipBytes = await zip.generateAsync({ type: "uint8array" });
Util.saveFile("sample.zip", zipBytes, 'application/zip');
```

##### Returns

[`PdfPageCollection`](PdfPageCollection)

***

### pdfFontFormat

#### Get Signature

> **get** **pdfFontFormat**(): [`PdfFontFormat`](../enumerations/PdfFontFormat)

Gets or sets the format used to represent fonts in the current document.
The default is [PdfFontFormat.Type0AutoOneByteEncoding](../enumerations/PdfFontFormat#type0autoonebyteencoding).
Note that this property does not affect the 14 standard PDF fonts,
those are always encoded as Type1.

##### Returns

[`PdfFontFormat`](../enumerations/PdfFontFormat)

#### Set Signature

> **set** **pdfFontFormat**(`value`): `void`

Gets or sets the format used to represent fonts in the current document.
The default is [PdfFontFormat.Type0AutoOneByteEncoding](../enumerations/PdfFontFormat#type0autoonebyteencoding).
Note that this property does not affect the 14 standard PDF fonts,
those are always encoded as Type1.

##### Parameters

###### value

[`PdfFontFormat`](../enumerations/PdfFontFormat)

##### Returns

`void`

***

### pdfVersion

#### Get Signature

> **get** **pdfVersion**(): `string`

Gets the PDF Version of the document.

##### Returns

`string`

***

### recognitionAlgorithm

#### Get Signature

> **get** **recognitionAlgorithm**(): [`RecognitionAlgorithm`](../enumerations/RecognitionAlgorithm)

Gets or sets the type of algorithm that is used for PDF content recognition
when building page text maps.

This property affects the behavior of methods such as [getText](#gettext),
[findText](#findtext) and other APIs that rely on text maps.

##### Returns

[`RecognitionAlgorithm`](../enumerations/RecognitionAlgorithm)

#### Set Signature

> **set** **recognitionAlgorithm**(`value`): `void`

Gets or sets the type of algorithm that is used for PDF content recognition
when building page text maps.

This property affects the behavior of methods such as [getText](#gettext),
[findText](#findtext) and other APIs that rely on text maps.

##### Parameters

###### value

[`RecognitionAlgorithm`](../enumerations/RecognitionAlgorithm)

##### Returns

`void`

***

### security

#### Get Signature

> **get** **security**(): [`Security`](Security)

Gets the [Security](Security) object that manages security for
the current document (passwords, etc).

##### Example

```ts
// load
const doc = PdfDocument.load(data);
...
// encrypt & save
doc.security.setEncryptOptions({
    ownerPassword: "abc",
    userPassword: "qwe",
    encryptionLevel: EncryptionLevel.AES256
});
const res: Uint8Array = doc.savePdf();
```

##### Returns

[`Security`](Security)

***

### standardFontEmbedMode

#### Get Signature

> **get** **standardFontEmbedMode**(): [`FontEmbedMode`](../enumerations/FontEmbedMode)

Gets or sets the font embedding mode for the 14 standard PDF fonts.
The default is [FontEmbedMode.NotEmbed](../enumerations/FontEmbedMode#notembed).
Note that if the PdfDocument is saved as PDF/A,
and the value of this property is [FontEmbedMode.NotEmbed](../enumerations/FontEmbedMode#notembed),
the standard fonts are embedded anyway using the [FontEmbedMode.EmbedSubset](../enumerations/FontEmbedMode#embedsubset) mode.

##### Returns

[`FontEmbedMode`](../enumerations/FontEmbedMode)

#### Set Signature

> **set** **standardFontEmbedMode**(`value`): `void`

Gets or sets the font embedding mode for the 14 standard PDF fonts.
The default is [FontEmbedMode.NotEmbed](../enumerations/FontEmbedMode#notembed).
Note that if the PdfDocument is saved as PDF/A,
and the value of this property is [FontEmbedMode.NotEmbed](../enumerations/FontEmbedMode#notembed),
the standard fonts are embedded anyway using the [FontEmbedMode.EmbedSubset](../enumerations/FontEmbedMode#embedsubset) mode.

##### Parameters

###### value

[`FontEmbedMode`](../enumerations/FontEmbedMode)

##### Returns

`void`

***

### willCloseAction

#### Get Signature

> **get** **willCloseAction**(): [`ActionJavaScript`](ActionJavaScript) \| `null`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed before closing the document.

##### Returns

[`ActionJavaScript`](ActionJavaScript) \| `null`

#### Set Signature

> **set** **willCloseAction**(`value`): `void`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed before closing the document.

##### Parameters

###### value

[`ActionJavaScriptProperties`](../type-aliases/ActionJavaScriptProperties) | [`ActionJavaScript`](ActionJavaScript) | `null`

##### Returns

`void`

***

### willPrintAction

#### Get Signature

> **get** **willPrintAction**(): [`ActionJavaScript`](ActionJavaScript) \| `null`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed before printing the document.

##### Returns

[`ActionJavaScript`](ActionJavaScript) \| `null`

#### Set Signature

> **set** **willPrintAction**(`value`): `void`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed before printing the document.

##### Parameters

###### value

[`ActionJavaScriptProperties`](../type-aliases/ActionJavaScriptProperties) | [`ActionJavaScript`](ActionJavaScript) | `null`

##### Returns

`void`

***

### willSaveAction

#### Get Signature

> **get** **willSaveAction**(): [`ActionJavaScript`](ActionJavaScript) \| `null`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed before saving the document.

##### Returns

[`ActionJavaScript`](ActionJavaScript) \| `null`

#### Set Signature

> **set** **willSaveAction**(`value`): `void`

Gets or sets a [ActionJavaScript](ActionJavaScript) to be performed before saving the document.

##### Parameters

###### value

[`ActionJavaScriptProperties`](../type-aliases/ActionJavaScriptProperties) | [`ActionJavaScript`](ActionJavaScript) | `null`

##### Returns

`void`

## Methods

### load()

#### Call Signature

> `static` **load**(`om`, `data`, `decryption?`): `PdfDocument`

Loads an existing PDF document from binary data using specified decryption options.

##### Parameters

###### om

[`ObjectManager`](ObjectManager)

Object manager that controls the lifetime of the PdfDocument.

###### data

`Uint8Array`

Binary data containing the PDF document

###### decryption?

[`PdfDecryptionOptions`](../type-aliases/PdfDecryptionOptions)

Optional decryption options for password-protected documents

##### Returns

`PdfDocument`

A new instance of PdfDocument representing the loaded document

##### Throws

If the provided byte array contains invalid PDF data or the password is incorrect

##### Example

```ts
// Load a password-protected PDF
const pdfData = fs.readFileSync("protected.pdf");
const decryption = { password: "mysecret" };
const doc = PdfDocument.load(om, pdfData, decryption);
```

#### Call Signature

> `static` **load**(`data`, `decryption?`): `PdfDocument`

Loads an existing PDF document from binary data using specified decryption options.

##### Parameters

###### data

`Uint8Array`

Binary data containing the PDF document

###### decryption?

[`PdfDecryptionOptions`](../type-aliases/PdfDecryptionOptions)

Optional decryption options for password-protected documents

##### Returns

`PdfDocument`

A new instance of PdfDocument representing the loaded document

##### Throws

If the provided byte array contains invalid PDF data or the password is incorrect

##### Example

```ts
// Load a password-protected PDF
const pdfData = fs.readFileSync("protected.pdf");
const decryption = { password: "mysecret" };
const doc = PdfDocument.load(pdfData, decryption);
```

#### Call Signature

> `static` **load**(`om`, `data`, `password?`): `PdfDocument`

Loads an existing PDF document from binary data using specified password.

##### Parameters

###### om

[`ObjectManager`](ObjectManager)

Object manager that controls the lifetime of the PdfDocument.

###### data

`Uint8Array`

Binary data containing the PDF document

###### password?

`string`

The optional password used to decrypt a document

##### Returns

`PdfDocument`

A new instance of PdfDocument representing the loaded document

##### Throws

If the provided byte array contains invalid PDF data or the password is incorrect

##### Example

```ts
// Load a password-protected PDF
const pdfData = fs.readFileSync("protected.pdf");
const doc = PdfDocument.load(om, pdfData, "mysecret");
```

#### Call Signature

> `static` **load**(`data`, `password?`): `PdfDocument`

Loads an existing PDF document from binary data using specified password.

##### Parameters

###### data

`Uint8Array`

Binary data containing the PDF document

###### password?

`string`

The optional password used to decrypt a document

##### Returns

`PdfDocument`

A new instance of PdfDocument representing the loaded document

##### Throws

If the provided byte array contains invalid PDF data or the password is incorrect

##### Example

```ts
// Load a password-protected PDF
const pdfData = fs.readFileSync("protected.pdf");
const doc = PdfDocument.load(pdfData, "mysecret");
```

#### Call Signature

> `static` **load**(`om`, `data`): `PdfDocument`

Loads an existing PDF document from binary data.

##### Parameters

###### om

[`ObjectManager`](ObjectManager)

Object manager that controls the lifetime of the PdfDocument.

###### data

`Uint8Array`

Binary data containing the PDF document

##### Returns

`PdfDocument`

A new instance of PdfDocument representing the loaded document

##### Throws

If the provided byte array contains invalid PDF data

##### Examples

```ts
// Load a PDF from file
const pdfData = fs.readFileSync("document.pdf");
const doc = PdfDocument.load(om, pdfData);
```

```ts
// Load from HTTP response
const response = await fetch("https://example.com/document.pdf");
const pdfData = new Uint8Array(await response.arrayBuffer());
const doc = PdfDocument.load(om, pdfData);
```

#### Call Signature

> `static` **load**(`data`): `PdfDocument`

Loads an existing PDF document from binary data.

##### Parameters

###### data

`Uint8Array`

Binary data containing the PDF document

##### Returns

`PdfDocument`

A new instance of PdfDocument representing the loaded document

##### Throws

If the provided byte array contains invalid PDF data

##### Examples

```ts
// Load a PDF from file
const pdfData = fs.readFileSync("document.pdf");
const doc = PdfDocument.load(pdfData);
```

```ts
// Load from HTTP response
const response = await fetch("https://example.com/document.pdf");
const pdfData = new Uint8Array(await response.arrayBuffer());
const doc = PdfDocument.load(pdfData);
```

***

### addEmbeddedFile()

> **addEmbeddedFile**(`key`, `item`): `void`

Adds the binary data as an embedded file to the PDF document.

#### Parameters

##### key

`string`

##### item

The [FileSpecProperties](../type-aliases/FileSpecProperties) object defining properties of embedded file.

`Uint8Array`\<`ArrayBufferLike`\> | [`FileSpecificationProperties`](../type-aliases/FileSpecificationProperties) | [`FileSpecification`](FileSpecification)

#### Returns

`void`

#### Example

```ts
const doc = new PdfDocument();

const pngFile = await Util.loadImageAsArray("cars.png");
doc.addEmbeddedFile("cars.png", {
    fileName: "cars.png",
    desc: "My car from the dream.",
    stream: {
        data: pngFile,
        mimeType: "image/png",
        creationDate: new Date('2019/12/01'),
        modificationDate: new Date('2020/04/19')
    }
});

const jpgFile = await Util.loadImageAsArray("tudor.jpg");
doc.addEmbeddedFile("tudor.jpg", {
    fileName: "tudor.jpg",
    desc: "The house to buy.",
    stream: {
        data: jpgFile,
        mimeType: "image/jpeg",
        creationDate: new Date('2022/12/01'),
        modificationDate: new Date('2023/04/19')
    }
});

Util.saveFile("embeddedFiles.pdf", doc.savePdf(), 'application/pdf');
```

***

### clear()

> **clear**(): `void`

Clears the document, removing all content and resetting all properties and settings to their initial default values.

#### Returns

`void`

***

### deleteText()

> **deleteText**(`findTextParams`, `deleteTextMode`, `searchRange?`): `void`

Deletes a specified text from all pages of the current document.

Note that the results may be affected by the current value of the [RecognitionAlgorithm](../enumerations/RecognitionAlgorithm) property.

#### Parameters

##### findTextParams

[`FindTextParams`](../type-aliases/FindTextParams)

The text to search for.

##### deleteTextMode

[`DeleteTextMode`](../enumerations/DeleteTextMode)

The text delete mode.

##### searchRange?

[`OutputRange`](../type-aliases/OutputRange)

The search scope.

#### Returns

`void`

***

### exportFormDataToFDF()

> **exportFormDataToFDF**(`options?`): `Uint8Array`

Exports the document's form data to a stream in FDF format.

#### Parameters

##### options?

[`ExportFormDataOptions`](../type-aliases/ExportFormDataOptions)

The export options.

#### Returns

`Uint8Array`

***

### findText()

> **findText**(`findTextParams`, `searchRange?`): [`FoundPosition`](../type-aliases/FoundPosition)[] \| `null`

Searches for all occurrences of a text in a range of the document's pages.

Note that the results may be affected by the current value of the [RecognitionAlgorithm](../enumerations/RecognitionAlgorithm) property.

#### Parameters

##### findTextParams

[`FindTextParams`](../type-aliases/FindTextParams)

The text searching parameters.

##### searchRange?

[`OutputRange`](../type-aliases/OutputRange)

The search scope.

#### Returns

[`FoundPosition`](../type-aliases/FoundPosition)[] \| `null`

***

### free()

> **free**(): `void`

Detaches the object from the [ObjectManager](ObjectManager) and deallocates its memory, if possible.

#### Returns

`void`

#### Inherited from

[`ObjectBase`](ObjectBase).[`free`](ObjectBase#free)

***

### getText()

> **getText**(): `string`

Extracts and returns all text from the current document.

#### Returns

`string`

***

### importFormDataFromFDF()

> **importFormDataFromFDF**(`fdfData`): `void`

Imports the document's form data from a stream in FDF format.

#### Parameters

##### fdfData

`Uint8Array`

The data in FDF format.

#### Returns

`void`

***

### mergeWithDocument()

> **mergeWithDocument**(`sourceDoc`, `options?`): `void`

Merges all or some pages from a specified PdfDocument into the current document.

#### Parameters

##### sourceDoc

`PdfDocument`

The source document which is to be merged into the current document.

##### options?

The options controlling what and how to merge.

[`MergeDocumentOptions`](../type-aliases/MergeDocumentOptions) | `null`

#### Returns

`void`

#### Example

```ts
const doc = PdfDocument.load(data);
const doc2 = PdfDocument.load(data2);
doc.mergeWithDocument(doc2, { index: 1, range: { fromPage: 2, toPage: 5 } });
const res: Uint8Array = doc.savePdf();
```

***

### newPage()

> **newPage**(): [`PdfPage`](PdfPage)

Adds a blank [PdfPage](PdfPage) to the document.

#### Returns

[`PdfPage`](PdfPage)

***

### newPageContext()

> **newPageContext**(`options?`): [`PdfContext`](PdfContext)

Adds a new [PdfPage](PdfPage) to the document and returns its drawing context.

#### Parameters

##### options?

[`PdfPageContextOptions`](../type-aliases/PdfPageContextOptions)

The options for adding a new page and creating a [PdfContext](PdfContext) object.

#### Returns

[`PdfContext`](PdfContext)

A PdfContext object for the new page.

#### Example

```ts
const doc = new PdfDocument();
const ctx = doc.newPageContext({ width: 500, height: 700 });
ctx.drawRect(50, 50, 200, 500, {
    radius: 10,
    lineColor: "Red",
    lineWidth: 10
});
const res: Uint8Array = doc.savePdf();
```

***

### rebind()

> **rebind**(`omTo`): `void`

Rebinds the object from the current [ObjectManager](ObjectManager) to the specified one.

#### Parameters

##### omTo

[`ObjectManager`](ObjectManager)

The new [ObjectManager](ObjectManager) for the object.

#### Returns

`void`

#### Inherited from

[`ObjectBase`](ObjectBase).[`rebind`](ObjectBase#rebind)

***

### redact()

#### Call Signature

> **redact**(`options?`): `void`

Applies all [RedactAnnotation](RedactAnnotation)s to the current document.

##### Parameters

###### options?

[`RedactOptions`](../type-aliases/RedactOptions)

Specifies the additional redact options.

##### Returns

`void`

#### Call Signature

> **redact**(`annotations`, `options?`): `void`

Applies a list of specified [RedactAnnotation](RedactAnnotation)s to the current document.

##### Parameters

###### annotations

[`RedactAnnotation`](RedactAnnotation)[]

The array of [RedactAnnotation](RedactAnnotation) objects to apply.

###### options?

[`RedactOptions`](../type-aliases/RedactOptions)

Specifies the additional redact options.

##### Returns

`void`

***

### replaceText()

> **replaceText**(`findTextParams`, `newText`, `searchRange?`, `font?`, `fontSize?`): `void`

Replaces a specified text on all pages of the current document.

Note that the results may be affected by the current value of the [recognitionAlgorithm](#recognitionalgorithm) property.

#### Parameters

##### findTextParams

[`FindTextParams`](../type-aliases/FindTextParams)

The text to search for.

##### newText

`string`

The replacement text.

##### searchRange?

[`OutputRange`](../type-aliases/OutputRange)

The search scope.

##### font?

The font to use on 'newText', if null the current font will be used.

[`Font`](Font) | `null`

##### fontSize?

The font size to use on 'newText', if null the current font size will be used.

`number` | `null`

#### Returns

`void`

***

### saveOriginalPdf()

> **saveOriginalPdf**(): `Uint8Array`

Saves the original (unmodified) PdfDocument to a byte array.

#### Returns

`Uint8Array`

A byte array with original PDF document data.

***

### savePdf()

> **savePdf**(`options?`): `Uint8Array`

Saves the current PdfDocument to a byte array.

#### Parameters

##### options?

[`SavePdfOptions`](../type-aliases/SavePdfOptions)

The options for saving a PDF document.

#### Returns

`Uint8Array`

A byte array with PDF document data.
