> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/web/page-manipulation/insert.md).

# Inserting a page into a PDF using JavaScript

Learn how to insert a page into a PDF using JavaScript. Explore functions like insertBlankPages and insertPages for seamless document editing using web. The Apryse Web SDK streamlines secure, serverle

{% hint style="info" %}
**Requirements**

*These packages are required to use these features in production. Trial keys have unlimited access to all features*

<a href="https://apryse.com/capabilities#PageManipulation" class="button primary">Package: Page Manipulation</a><a href="https://showcase.apryse.com/pdf-page-manipulation-api" class="button primary">Live demo</a>
{% endhint %}

Pages can be inserted into the current PDF document using the [insertBlankPages](https://sdk.apryse.com/api/web/Core.Document.html#insertBlankPages) and [insertPages](https://sdk.apryse.com/api/web/Core.Document.html#insertPages) functions. Inserting blank pages is fairly straight forward.

## Insert blank pages

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
WebViewer(...)
  .then(instance => {
    const docViewer = instance.Core.documentViewer;

    docViewer.addEventListener('documentLoaded', async () => {
      const doc = docViewer.getDocument();

      const width = 612;
      const height = 792;
      docViewer.getPageCount(); // 3

      // Insert blank pages
      await doc.insertBlankPages([2, 3], width, height);
      // The document now contains [page1, newBlankPage1, page2, newBlankPage2, page3]
      docViewer.getPageCount(); // 5
    });
  });
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer.getDocument](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getDocument__anchor) [DocumentViewer.getPageCount](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getPageCount__anchor) [Document.insertBlankPages](https://sdk.apryse.com/api/web/Core.Document.html#insertBlankPages__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
WebViewer(...)
  .then(instance => {
    const docViewer = instance.docViewer;

    docViewer.on('documentLoaded', async () => {
      const doc = docViewer.getDocument();

      const width = 612;
      const height = 792;
      docViewer.getPageCount(); // 3

      // Insert blank pages
      await doc.insertBlankPages([2, 3], width, height);
      // The document now contains [page1, newBlankPage1, page2, newBlankPage2, page3]
      docViewer.getPageCount(); // 5
    });
  });
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [DocumentViewer.getDocument](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getDocument__anchor) [DocumentViewer.getPageCount](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getPageCount__anchor) [Document.insertBlankPages](https://sdk.apryse.com/api/web/Core.Document.html#insertBlankPages__anchor)
{% endtab %}
{% endtabs %}

## Insert a page from another document or the same document

More likely someone will want to insert pages from one PDF into another. To achieve this, instantiate a new `Document` object by passing the URL of the second PDF to [createDocument](https://sdk.apryse.com/api/web/Core.html#.createDocument__anchor) function as a first parameter.

{% hint style="info" %}
Currently there aren't any functions to duplicate pages in the current document directly. If you want to copy a page, you'll need to instantiate a new `Document` object and load the same PDF into it using `createDocument` and insert the page into the current document.
{% endhint %}

{% tabs %}
{% tab title="JavaScript (SDK v8.0+)" %}
{% code lineNumbers="true" %}

```js
WebViewer({
  // options
}, document.getElementById('viewer'))
  .then(instance => {
    const { documentViewer } = instance.Core;

    documentViewer.addEventListener('documentLoaded', async () => {
      const doc = documentViewer.getDocument();
      const pagesToInsert = [4, 5, 6];
      const pageIndexToInsert = doc.getPageCount() + 1;
      // in this example doc.getPageCount() returns 3


      // use createDocument to create CoreControls.Document instance
      const docToInsert = await instance.Core.CoreControls.createDocument('http://localhost/pdf/another.pdf', {});

      doc.insertPages(docToInsert, pagesToInsert, pageIndexToInsert).then(() => {
        // page 4, 5, and 6 from 'another.pdf' has been inserted into to the current pdf
        doc.getPageCount(); // 6
      });

    });
  });
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [Core.createDocument](https://sdk.apryse.com/api/web/Core.html#.createDocument__anchor) [DocumentViewer.getDocument](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getDocument__anchor) [DocumentViewer.getPageCount](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getPageCount__anchor) [Document.insertPages](https://sdk.apryse.com/api/web/Core.Document.html#insertPages__anchor)
{% endtab %}

{% tab title="JavaScript (SDK v6.0+)" %}
{% code lineNumbers="true" %}

```js
WebViewer({
  // options
}, document.getElementById('viewer'))
  .then(instance => {
    const { docViewer, CoreControls } = instance;

    docViewer.on('documentLoaded', async () => {
      const doc = docViewer.getDocument();
      const pagesToInsert = [4, 5, 6];
      const pageIndexToInsert = doc.getPageCount() + 1;
      // in this example doc.getPageCount() returns 3


      // use createDocument to create CoreControls.Document instance
      const docToInsert = await CoreControls.createDocument('http://localhost/pdf/another.pdf', {});

      doc.insertPages(docToInsert, pagesToInsert, pageIndexToInsert).then(() => {
        // page 4, 5, and 6 from 'another.pdf' has been inserted into to the current pdf
        doc.getPageCount(); // 6
      });

    });
  });
```

{% endcode %}

[WebViewerInstance](https://sdk.apryse.com/api/web/WebViewerInstance.html) [CoreControls.createDocument](https://sdk.apryse.com/api/web/Core.html#.createDocument__anchor) [DocumentViewer.getDocument](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getDocument__anchor) [DocumentViewer.getPageCount](https://sdk.apryse.com/api/web/Core.DocumentViewer.html#getPageCount__anchor) [Document.insertPages](https://sdk.apryse.com/api/web/Core.Document.html#insertPages__anchor)
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/web/page-manipulation/insert.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
