> 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/get-started/guides/features/manipulation/merge.md).

# How to merge or insert PDF pages using JavaScript

Learn how to merge, insert, copy, rearrange, or delete pdf pages using Python

{% hint style="warning" %}
Make sure you have [Full API enabled in WebViewer.](/web/what-is-webviewer/full-api.md)
{% endhint %}

To insert/copy/merge several PDF documents into one.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
    const newDoc = await PDFNet.PDFDoc.create();
    const page_num = 15;
    for (let i=1; i<=page_num; ++i)
    {
        const doc = await PDFNet.PDFDoc.createFromURL(filename + "_split_page_" + i + ".pdf");
        const pageCount = await doc.getPageCount();
        newDoc.insertPages(i, doc, 1, pageCount, PDFNet.PDFDoc.InsertFlag.e_none);
    }
    const buf = await newDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);

    //optionally save the blob to a file or upload to a server
    const blob = new Blob([buf], { type: 'application/pdf' });
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Merge, copy, delete, and rearrange PDF pages](/web/get-started/samples.md#pdfpage) Full code sample which illustrates how to copy pages from one document to another, how to delete, and rearrange pages and how to use ImportPages().

## About page copying/merging

The recommended way to copy pages from one document to another is with `PDFDoc.InsertPages()`. Its arguments are:

* *insertBeforeThisPage*: An integer specifying where the pages should be inserted
* *sourceDoc*: A PDFDoc from which the pages should be read
* *startPage*: An integer specifying the first page number to insert
* *endPage*: An integer specifying the last page number to insert
* *flag*: A PDFDoc.InsertFlag value

For example, suppose we want to insert the third page of one document after the first page of a second document. The following code snippet performs this with an *insertBeforeThisPage* value of 2 and *startPage* and *endPage* values of 3.

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
dest_doc.insertPages(2, source_doc, 3, 3, PDFNet.PDFDoc.InsertFlag.e_none);
```

{% endcode %}
{% endtab %}
{% endtabs %}

We can also insert a range of pages. For example, the following code will insert the second, third, and fourth pages of one document into the end of the second document. We specify that we're inserting into the end of the document by using an *insertBeforeThisPage* value higher than the number of pages in the document:

{% tabs %}
{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
dest_doc.insertPages(dest_doc.getPageCount() + 1, source_doc, 2, 4, PDFNet.PDFDoc.InsertFlag.e_none);
```

{% endcode %}
{% 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/get-started/guides/features/manipulation/merge.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.
