> 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/salesforce/page-manipulation/merge.md).

# Merging PDF pages using JavaScript

Merge multiple PDF files easily using the insertPage function on the Document instance. Learn how to merge files without WebViewer by setting specific flags for Core to function correctly. See example

Two or more PDF files can be merged together by using the [insertPage](https://sdk.apryse.com/api/web/Core.Document.html#insertPages__anchor) function on the Document instance. See the following example:

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

```js
// config.js
const { documentViewer } = instance.Core;
const doc = documentViewer.getDocument();

const secondDoc = await instance.Core.createDocument('url/to/second.pdf');
const pagesToInsert = [4, 5, 6];
const pageIndexToInsert = doc.getPageCount() + 1;
// in this example doc.getPageCount() returns 3

doc.insertPages(secondDoc, pagesToInsert, pageIndexToInsert).then(() => {
  doc.getPageCount(); // 6
});
```

{% endcode %}

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

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

```js
// config.js
const { docViewer, CoreControls } = instance;
const doc = docViewer.getDocument();

const secondDoc = await CoreControls.createDocument('url/to/second.pdf');
const pagesToInsert = [4, 5, 6];
const pageIndexToInsert = doc.getPageCount() + 1;
// in this example doc.getPageCount() returns 3

doc.insertPages(secondDoc, pagesToInsert, pageIndexToInsert).then(() => {
  doc.getPageCount(); // 6
});
```

{% endcode %}

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

## Merge files without WebViewer

When using Core without WebViewer, certain flags need to set to make Core function correctly and to be able to load web workers from the correct path. You need to set [setPDFWorkerPath](https://sdk.apryse.com/api/web/Core.html#.setPDFWorkerPath__anchor), [forceBackendType](https://sdk.apryse.com/api/web/Core.html#.forceBackendType__anchor) and [disableEmbeddedJavaScript](https://sdk.apryse.com/api/web/Core.html#.disableEmbeddedJavaScript__anchor) as shown:

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

```js
Core.forceBackendType('ems');
Core.setPDFWorkerPath('/lib/core/pdf'); // path to web workers
Core.disableEmbeddedJavaScript(); // disabling pdf javascript

// array of url of PDFs to merge
const urls = [
  'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf',
  'https://pdftron.s3.amazonaws.com/downloads/pl/Cheetahs.pdf',
  'https://pdftron.s3.amazonaws.com/downloads/pl/magazine-short.pdf',
];

mergeDocuments(urls).then(mergedPdf => {
  // merged pdf, here you can download it using mergedPdf.getFileData
  
});


// recursive function with promise 
function mergeDocuments(urlArray, nextCount = 1, doc = null) {
  return new Promise(async function(resolve, reject) {
    if (!doc) {
      doc = await Core.createDocument(urlArray[0]);
    }
    const newDoc = await Core.createDocument(urlArray[nextCount]);
    const newDocPageCount = newDoc.getPageCount();

    // create an array containing 1…N
    const pages = Array.from({ length: newDocPageCount }, (v, k) => k + 1);
    const pageIndexToInsert = doc.getPageCount() + 1;
    // in this example doc.getPageCount() returns 3

    doc.insertPages(newDoc, pages, pageIndexToInsert)
      .then(result => resolve({
        next: urlArray.length - 1 > nextCount,
        doc: doc,
      })
    );
    // end Promise
  }).then(res => {
    return res.next ?
      mergeDocuments(urlArray, nextCount + 1, res.doc) :
      res.doc;
  });
}
```

{% endcode %}

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

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

```js
CoreControls.forceBackendType('ems');
CoreControls.setPDFWorkerPath('/lib/core/pdf'); // path to web workers
CoreControls.disableEmbeddedJavaScript(); // disabling pdf javascript

// array of url of PDFs to merge
const urls = [
  'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf',
  'https://pdftron.s3.amazonaws.com/downloads/pl/Cheetahs.pdf',
  'https://pdftron.s3.amazonaws.com/downloads/pl/magazine-short.pdf',
];

mergeDocuments(urls).then(mergedPdf => {
  // merged pdf, here you can download it using mergedPdf.getFileData
  
});


// recursive function with promise 
function mergeDocuments(urlArray, nextCount = 1, doc = null) {
  return new Promise(async function(resolve, reject) {
    if (!doc) {
      doc = await CoreControls.createDocument(urlArray[0]);
    }
    const newDoc = await CoreControls.createDocument(urlArray[nextCount]);
    const newDocPageCount = newDoc.getPageCount();

    // create an array containing 1…N
    const pages = Array.from({ length: newDocPageCount }, (v, k) => k + 1);
    const pageIndexToInsert = doc.getPageCount() + 1;
    // in this example doc.getPageCount() returns 3

    doc.insertPages(newDoc, pages, pageIndexToInsert)
      .then(result => resolve({
        next: urlArray.length - 1 > nextCount,
        doc: doc,
      })
    );
    // end Promise
  }).then(res => {
    return res.next ?
      mergeDocuments(urlArray, nextCount + 1, res.doc) :
      res.doc;
  });
}
```

{% endcode %}

[CoreControls.createDocument](https://sdk.apryse.com/api/web/Core.html#.createDocument__anchor) [Document.insertPages](https://sdk.apryse.com/api/web/Core.Document.html#insertPages__anchor) [Document.getPageCount](https://sdk.apryse.com/api/web/Core.Document.html#getPageCount__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/salesforce/page-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.
