Skip to content
Guides

Merge Pages

The steps to merge pages are as follows:

  1. Create a blank PDF document.
  2. Open the PDF document containing the pages to be merged.
  3. Select all the pages to be merged and combine them into the same document.

This example shows how to merge pages:

swift
let mergeDocument = CPDFDocument()

if let document1 = CPDFDocument(url: URL(fileURLWithPath: "filePath1")),
   let document2 = CPDFDocument(url: URL(fileURLWithPath: "filePath2")) {
    
    let indexSet = IndexSet(integersIn: 0..<2)
    
    mergeDocument?.importPages(indexSet, from: document1, at: mergeDocument?.pageCount ?? 0)//Insert the first two pages.
    mergeDocument?.importPages(indexSet, from: document2, at: mergeDocument?.pageCount ?? 0)
}
objective-c
CPDFDocument *mergeDocument = [[CPDFDocument alloc] init];

CPDFDocument *document1 = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath1"]];
CPDFDocument *document2 = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath2"]];

NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
[indexSet addIndex:0];
[indexSet addIndex:1];
[mergeDocument importPages:indexSet fromDocument:document1 atIndex:mergeDocument.pageCount];//Insert the first two pages.
[mergeDocument importPages:indexSet fromDocument:document2 atIndex:mergeDocument.pageCount];