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

# How to merge or insert PDF pages on iOS

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

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

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

[Merge, copy, delete, and rearrange PDF pages](/ios/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="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 %}
{% 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="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 %}
{% 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/ios/page-manipulation/merge.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
