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

# How to merge or insert PDF pages on Server/Desktop

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

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

Packages are required for production license. Trial keys have unlimited access to all features.

Demo showcases functionality using WebViewer UI.

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

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

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
PDFDoc new_doc = new PDFDoc();
int page_num = 15;
for (int i = 1; i <= page_num; ++i)
{
    PDFDoc in_doc = new PDFDoc(filename + "_split_page_" + i + ".pdf");
    new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc.InsertFlag.e_none);
}
new_doc.Save(output_filename + "_merge_pages.pdf", SDFDoc.SaveOptions.e_linearized);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc new_doc;
int page_num = 15;
for (int i=1; i<=page_num; ++i)
{
    char fname[64];
    sprintf(fname, "_split_page_%d.pdf", i);
    string input_file(filename + fname);
    PDFDoc doc(input_file);
    new_doc.InsertPages(i, doc, 1, doc.GetPageCount(), PDFDoc::e_none);
}
new_doc.Save(output_filename + "_merge_pages.pdf", SDFDoc::e_linearized, 0);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
newDoc := NewPDFDoc()

pageNum = 15
for i := 1; i <= pageNum; i ++ {
    doc = NewPDFDoc(outputPath + "_split_page_" + i + ".pdf")
    newDoc.InsertPages(i, doc, 1, doc.GetPageCount(), PDFDocE_none)
    doc.Close()
}
newDoc.Save(outputPath + "_merge_pages.pdf", uint(SDFDocE_linearized))
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc new_doc = new PDFDoc();
int page_num = 15;
for (int i=1; i<=page_num; ++i)
{
    PDFDoc doc = new PDFDoc(filename + "_split_page_" + i + ".pdf");
    new_doc.insertPages(i, doc, 1, doc.getPageCount(), PDFDoc.InsertBookmarkMode.NONE, null);
    doc.close();
}
new_doc.save(output_filename + "_merge_pages.pdf", SDFDoc.SaveMode.LINEARIZED, null);
```

{% endcode %}
{% endtab %}

{% 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 %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val new_doc = PDFDoc()
val page_num = 15;
for (i in 1..page_num)
{
    val doc = PDFDoc(filename + "_split_page_" + i + ".pdf")
    new_doc.insertPages(i, doc, 1, in_doc.pageCount, PDFDoc.InsertBookmarkMode.NONE, null)
    in_doc.close()
}
new_doc.save(output_filename + "_merge_pages.pdf", SDFDoc.SaveMode.LINEARIZED, null)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
int page_num = 15;
for (int i=1; i<=page_num; ++i)
{
    NSString *input_file = [output_filename stringByAppendingFormat: @"_split_page_%d.pdf", i]; 
    PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: input_file];
    [new_doc InsertPages: i src_doc: doc start_page: 1 end_page: [doc GetPageCount] flag: e_ptinsert_none];
}
[new_doc SaveToFile: [output_filename stringByAppendingString:@"_merge_pages.pdf"] flags: e_ptlinearized];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
let page_num = 15
for i in 1..page_num
{
    let input_file: String = "\(filename)_split_page_\(i).pdf"
    let doc: PTPDFDoc = PTPDFDoc(filepath: input_file)
    new_doc.insertPages(Int32(i), src_doc: doc, start_page: 1, end_page: doc.getPageCount(), flag: e_ptinsert_none)
}
new_doc.save(toFile: "\(output_filename)_merge_pages.pdf", flags: e_ptlinearized.rawValue)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$new_doc = new PDFDoc();
$page_num = 15;
for ($i = 1; $i <= $page_num; ++$i)
{
    $doc = new PDFDoc($filename."_split_page_".$i.".pdf");
    $new_doc->InsertPages($i, $doc, 1, $doc->GetPageCount(), PDFDoc::e_none);
    $doc->Close();
}
$new_doc->Save($output_filename."_merge_pages.pdf", SDFDoc::e_linearized);
$doc->Close();
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
new_doc = PDFDoc()
page_num = 15
for i in range(1, page_num):
    doc = PDFDoc(filename + "_split_page_" + str(i) + ".pdf")
    new_doc.InsertPages(i, doc, 1, doc.GetPageCount(), PDFDoc.e_none)
    doc.Close()
new_doc.Save(output_filename + "_merge_pages.pdf", SDFDoc.e_linearized)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
new_doc = PDFDoc.new()
page_num = 15
for i in 1..page_num do
    doc = PDFDoc.new(filename + "_split_page_" + i.to_s + ".pdf")
    new_doc.InsertPages(i, doc, 1, doc.GetPageCount, PDFDoc::E_none)
    doc.Close
end
new_doc.Save(output_filename + "_merge_pages.pdf", SDFDoc::E_linearized)
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim new_doc As PDFDoc = New PDFDoc()
Dim page_num As Integer = 15
For i = 1 To page_num
    Dim doc As PDFDoc = New PDFDoc(filename + "_split_page_" + CStr(1) + ".pdf")
    new_doc.InsertPages(i, doc, 1, doc.GetPageCount(), PDFDoc.InsertFlag.e_none)
    doc.Close()
Next i
new_doc.Save(output_filename + "_merge_pages.pdf", SDFDoc.SaveOptions.e_linearized)
```

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

[Merge, copy, delete, and rearrange PDF pages](/core/get-started/samples/pdfpagetest.md) 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(). Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.

## 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="C#" %}
{% code lineNumbers="true" %}

```csharp
dest_doc.InsertPages(2, source_doc, 3, 3, PDFDoc.InsertFlag.e_none);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
dest_doc.InsertPages(2, source_doc, 3, 3, PDFDoc::e_none);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
destDoc.InsertPages(3, sourceDoc, 3, 3, PDFDocE_none)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
dest_doc.insertPages(2, source_doc, 3, 3, PDFDoc.InsertBookmarkMode.NONE, null);
```

{% endcode %}
{% endtab %}

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

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

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
dest_doc.insertPages(2, source_doc, 3, 3, PDFDoc.InsertBookmarkMode.NONE, null)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
[dest_doc InsertPages: 2 src_doc: source_doc start_page: 3 end_page: 3 flag: e_ptinsert_none];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
dest_doc.insertPages(2, src_doc: source_doc, start_page: 3, end_page: 3, flag: e_ptinsert_none)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$dest_doc->InsertPages(2, $source_doc, 3, 3, PDFDoc::e_none);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
dest_doc.InsertPages(2, source_doc, 3, 3, PDFDoc.e_none)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
dest_doc.InsertPages(2, source_doc, 3, 3, PDFDoc::E_none)
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
dest_doc.InsertPages(2, source_doc, 3, 3, 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="C#" %}
{% code lineNumbers="true" %}

```csharp
dest_doc.InsertPages(dest_doc.GetPageCount() + 1, source_doc, 2, 4, PDFDoc.InsertFlag.e_none);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
dest_doc.InsertPages(dest_doc.GetPageCount() + 1, source_doc, 2, 4, PDFDoc::e_none);
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
destDoc.InsertPages(destDoc.GetPageCount() + 1, sourceDoc, 2, 4, PDFDocE_none)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
dest_doc.insertPages(dest_doc.getPageCount() + 1, source_doc, 2, 4, PDFDoc.InsertBookmarkMode.NONE, null);
```

{% endcode %}
{% endtab %}

{% 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 %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
dest_doc.insertPages(dest_doc.getPageCount() + 1, source_doc, 2, 4, PDFDoc.InsertBookmarkMode.NONE, null)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
[dest_doc InsertPages: [dest_doc GetPageCount] + 1 src_doc: source_doc start_page: 2 end_page: 4 flag: e_ptinsert_none];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
dest_doc.insertPages(dest_doc.getPageCount() + 1, src_doc: source_doc, start_page: 2, end_page: 4, flag: e_ptinsert_none)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$dest_doc->InsertPages($dest_doc->GetPageCount() + 1, $source_doc, 2, 4, PDFDoc::e_none);
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
dest_doc.InsertPages(dest_doc.GetPageCount() + 1, source_doc, 2, 4, PDFDoc.e_none)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
dest_doc.InsertPages(dest_doc.GetPageCount() + 1, source_doc, 2, 4, PDFDoc::E_none)
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
dest_doc.InsertPages(dest_doc.GetPageCount() + 1, source_doc, 2, 4, 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/core/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.
