> 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/get-started/samples/pdfpagetest.md).

# PDFPage

Sample Obj-C code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations.

Sample Obj-C code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations. Learn more about our [iOS SDK](/ios/guides.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

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

```objc
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#import <OBJC/PDFNetOBJC.h>
#import <Foundation/Foundation.h>

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int ret = 0;
        [PTPDFNet Initialize: 0];

        // Sample 1 - Split a PDF document into multiple pages
        @try
        {
            NSLog(@"_______________________________________________");
            NSLog(@"Sample 1 - Split a PDF document into multiple pages...");
            NSLog(@"Opening the input pdf...");
            PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [in_doc InitSecurityHandler];

            int page_num = [in_doc GetPageCount];
            int i;
            for (i=1; i<=page_num; ++i)
            {
                PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
                NSString *output_file = [@"../../TestFiles/Output/" stringByAppendingFormat: @"newsletter_split_page_%d.pdf", i]; 
                [new_doc InsertPages: 0 src_doc: in_doc start_page: i end_page: i flag: e_ptinsert_none];
                [new_doc SaveToFile: output_file flags: e_ptremove_unused];
                NSLog(@"Done. Result saved in newsletter_split_page_%d.pdf", i);
            }
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }

        // Sample 2 - Merge several PDF documents into one
        @try
        {
            NSLog(@"_______________________________________________");
            NSLog(@"Sample 2 - Merge several PDF documents into one...");
            PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
            [new_doc InitSecurityHandler];

            int page_num = 15;
            int i;
            for (i=1; i<=page_num; ++i)
            {
                NSString *input_file = [@"../../TestFiles/Output/" stringByAppendingFormat: @"newsletter_split_page_%d.pdf", i]; 
                NSLog(@"Opening newsletter_split_page_%d.pdf", i);
                PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: input_file];
                [new_doc InsertPages: i src_doc: in_doc start_page: 1 end_page: [in_doc GetPageCount] flag: e_ptinsert_none];
            }
            [new_doc SaveToFile: @"../../TestFiles/Output/newsletter_merge_pages.pdf" flags: e_ptremove_unused];
            NSLog(@"Done. Result saved in newsletter_merge_pages.pdf");
            
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }


        // Sample 3 - Delete every second page
        @try
        {
            NSLog(@"_______________________________________________");
            NSLog(@"Sample 3 - Delete every second page...");
            NSLog(@"Opening the input pdf...");
            PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [in_doc InitSecurityHandler];
            
            int page_num = [in_doc GetPageCount];
            while (page_num>=1)
            {
                PTPageIterator *itr = [in_doc GetPageIterator: page_num];
                [in_doc PageRemove: itr];
                page_num -= 2;
            }
            
            [in_doc SaveToFile: @"../../TestFiles/Output/newsletter_page_remove.pdf" flags: 0];
            NSLog(@"Done. Result saved in newsletter_page_remove.pdf...");

        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }
        
        // Sample 4 - Inserts a page from one document at different 
        // locations within another document
        @try
        {
            NSLog(@"_______________________________________________");
            NSLog(@"Sample 4 - Insert a page at different locations...");
            NSLog(@"Opening the input pdf...");
            
            PTPDFDoc *in1_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [in1_doc InitSecurityHandler];

            PTPDFDoc *in2_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/fish.pdf"];
            [in2_doc InitSecurityHandler];
            
            PTPageIterator *src_page = [in2_doc GetPageIterator: 1];
            PTPageIterator *dst_page = [in1_doc GetPageIterator: 1];
            int page_num = 1;
            
            while ([dst_page HasNext]) {
                if ((page_num++ % 3) == 0) {
                    [in1_doc PageInsert: dst_page page: [src_page Current]];
            }
                
            [dst_page Next];
        }
            
            [in1_doc SaveToFile: @"../../TestFiles/Output/newsletter_page_insert.pdf" flags: 0];
            NSLog(@"Done. Result saved in newsletter_page_insert.pdf...");
            

        }@catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }

        // Sample 5 - Replicate pages within a single document
        @try
        {

            NSLog(@"_______________________________________________");
            NSLog(@"Sample 5 - Replicate pages within a single document...");
            NSLog(@"Opening the input pdf...");
            PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [doc InitSecurityHandler];
            
            // Replicate the cover page three times (copy page #1 and place it before the 
            // seventh page in the document page sequence)
            PTPage *cover = [doc GetPage: 1];
            PTPageIterator *p7 = [doc GetPageIterator: 7];
            [doc PageInsert: p7 page: cover];
            [doc PageInsert: p7 page: cover];
            [doc PageInsert: p7 page: cover];
            
            // Replicate the cover page two more times by placing it before and after
            // existing pages.
            [doc PagePushFront: cover];
            [doc PagePushBack: cover];
            
            [doc SaveToFile: @"../../TestFiles/Output/newsletter_page_clone.pdf" flags: 0];
            NSLog(@"Done. Result saved in newsletter_page_clone.pdf...");
            
        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }
        
        // Sample 6 - Use ImportPages() in order to copy multiple pages at once 
        // in order to preserve shared resources between pages (e.g. images, fonts, 
        // colorspaces, etc.)
        @try
        {

            NSLog(@"_______________________________________________");
            NSLog(@"Sample 6 - Preserving shared resources using ImportPages...");
            NSLog(@"Opening the input pdf...");
            PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
            [in_doc InitSecurityHandler];

            PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
            
            PTVectorPage *copy_pages = [[PTVectorPage alloc] init];
            PTPageIterator *itr;
            for (itr=[in_doc GetPageIterator: 1]; [itr HasNext]; [itr Next])
            {
                [copy_pages add: [itr Current]];
            }
            
            PTVectorPage *imported_pages = [new_doc ImportPages: copy_pages import_bookmarks: NO];
            int i;
            for (i=0; i<[imported_pages size]; ++i)
            {
                [new_doc PagePushFront: [imported_pages get: i]]; // Order pages in reverse order. 
                // Use PagePushBack() if you would like to preserve the same order.
            }
            
            [new_doc SaveToFile: @"../../TestFiles/Output/newsletter_import_pages.pdf" flags: 0];
            NSLog(@"Done. Result saved in newsletter_import_pages.pdf...");
            NSLog(@"\n");
            NSLog(@"Note that the output file size is less than half the size");
            NSLog(@"of the file produced using individual page copy operations");
            NSLog(@"between two documents");

        }
        @catch(NSException *e)
        {
            NSLog(@"%@", e.reason);
            ret = 1;
        }
        [PTPDFNet Terminate: 0];
        return ret;
    }
}
```

{% endcode %}
{% endtab %}

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

```swift
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

func runPDFPageTest() -> Int {
    return autoreleasepool {
        var ret = 0
        
        
        // Sample 1 - Split a PDF document into multiple pages
        do {
            try PTPDFNet.catchException {
                print("_______________________________________________")
                print("Sample 1 - Split a PDF document into multiple pages...")
                print("Opening the input pdf...")
                let in_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                in_doc.initSecurityHandler()
                
                let page_num = in_doc.getPageCount()
                for i in 1...page_num {
                    let new_doc: PTPDFDoc = PTPDFDoc()
                    let output_file: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_split_page_\(i).pdf").path
                    new_doc.insertPages(0, src_doc: in_doc, start_page: i, end_page: i, flag: e_ptinsert_none)
                    new_doc.save(toFile: output_file, flags: e_ptremove_unused.rawValue)
                    print("Done. Result saved in newsletter_split_page_\(i).pdf")
                }
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        // Sample 2 - Merge several PDF documents into one
        do {
            try PTPDFNet.catchException {
                print("_______________________________________________")
                print("Sample 2 - Merge several PDF documents into one...")
                let new_doc: PTPDFDoc = PTPDFDoc()
                new_doc.initSecurityHandler()
                
                let page_num: Int = 15
                for i in 1...page_num {
                    let input_file: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_split_page_\(i).pdf").path
                    print("Opening newsletter_split_page_\(i).pdf")
                    let in_doc: PTPDFDoc = PTPDFDoc(filepath: input_file)
                    new_doc.insertPages(Int32(i), src_doc: in_doc, start_page: 1, end_page: in_doc.getPageCount(), flag: e_ptinsert_none)
                }
                new_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_merge_pages.pdf").path, flags: e_ptremove_unused.rawValue)
                print("Done. Result saved in newsletter_merge_pages.pdf")
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        // Sample 3 - Delete every second page
        do {
            try PTPDFNet.catchException {
                print("_______________________________________________")
                print("Sample 3 - Delete every second page...")
                print("Opening the input pdf...")
                let in_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                in_doc.initSecurityHandler()
                
                var page_num = in_doc.getPageCount()
                while page_num >= 1 {
                    let itr: PTPageIterator = in_doc.getPageIterator(UInt32(page_num))
                    in_doc.pageRemove(itr)
                    page_num -= 2
                }
                
                in_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_page_remove.pdf").path, flags: 0)
                print("Done. Result saved in newsletter_page_remove.pdf...")
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        // Sample 4 - Inserts a page from one document at different
        // locations within another document
        do {
            try PTPDFNet.catchException {
                print("_______________________________________________")
                print("Sample 4 - Insert a page at different locations...")
                print("Opening the input pdf...")
                
                let in1_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                in1_doc.initSecurityHandler()
                
                let in2_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "fish", ofType: "pdf"))
                in2_doc.initSecurityHandler()
                
                let src_page: PTPageIterator = in2_doc.getPageIterator(1)
                let dst_page: PTPageIterator = in1_doc.getPageIterator(1)
                var page_num: Int = 1
                while dst_page.hasNext() {
                    if (page_num % 3) == 0 {
                        in1_doc.pageInsert(dst_page, page: src_page.current())
                    }
                    page_num += 1
                    dst_page.next()
                }
                
                in1_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_page_insert.pdf").path, flags: 0)
                print("Done. Result saved in newsletter_page_insert.pdf...")
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        // Sample 5 - Replicate pages within a single document
        do {
            try PTPDFNet.catchException {
                print("_______________________________________________")
                print("Sample 5 - Replicate pages within a single document...")
                print("Opening the input pdf...")
                let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                doc.initSecurityHandler()
                
                // Replicate the cover page three times (copy page #1 and place it before the
                // seventh page in the document page sequence)
                let cover: PTPage = doc.getPage(1)
                let p7: PTPageIterator = doc.getPageIterator(7)
                doc.pageInsert(p7, page: cover)
                doc.pageInsert(p7, page: cover)
                doc.pageInsert(p7, page: cover)
                
                // Replicate the cover page two more times by placing it before and after
                // existing pages.
                doc.pagePushFront(cover)
                doc.pagePushBack(cover)
                
                doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_page_clone.pdf").path, flags: 0)
                print("Done. Result saved in newsletter_page_clone.pdf...")
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        // Sample 6 - Use ImportPages() in order to copy multiple pages at once
        // in order to preserve shared resources between pages (e.g. images, fonts,
        // colorspaces, etc.)
        do {
            try PTPDFNet.catchException {
                print("_______________________________________________")
                print("Sample 6 - Preserving shared resources using ImportPages...")
                print("Opening the input pdf...")
                let in_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
                in_doc.initSecurityHandler()
                
                let new_doc: PTPDFDoc = PTPDFDoc()
                
                let copy_pages: PTVectorPage = PTVectorPage()
                let itr: PTPageIterator = in_doc.getPageIterator(1)
                while itr.hasNext() {
                    copy_pages.add(itr.current())
                    itr.next()
                }
                
                let imported_pages: PTVectorPage = new_doc.importPages(copy_pages, import_bookmarks: false)
                for i in 0..<imported_pages.size() {
                    new_doc.pagePushFront(imported_pages.get(Int32(i)))
                    // Order pages in reverse order.
                    // Use PagePushBack() if you would like to preserve the same order.
                }
                
                new_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_import_pages.pdf").path, flags: 0)
                print("Done. Result saved in newsletter_import_pages.pdf...")
                
                print("Note that the output file size is less than half the size of the file produced using individual page copy operations between two documents")
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        return ret
    }
}
```

{% 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/ios/get-started/samples/pdfpagetest.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.
