Skip to content
Guides

Replace Pages

The steps to replace pages are as follows:

  1. Remove the pages in the target file that need to be replaced.
  2. Insert the replacement pages into the location where the original document was deleted.

This example shows how to replace pages:

swift
var indexSet = IndexSet()
indexSet.insert(0)

// Remove the first page from the document.
document?.removePage(at: indexSet)
let insertDocument = CPDFDocument(url: URL(fileURLWithPath: "OtherPDF.pdf"))
var indexSet = IndexSet()
indexSet.insert(0)

// Insert the first page of another document into the original document's first-page position to complete the replacement.
document?.importPages(indexSet, from: insertDocument, at: 0)
objective-c
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
[indexSet addIndex:0];

// Remove the first page from the document.
[document removePageAtIndexSet:indexSet];
CPDFDocument *insertDocument = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"OtherPDF.pdf"]];
NSIndexSet *inserSet = [[NSIndexSet alloc]  initWithIndex:0];
// Insert the first page of another document into the original document's first-page position to complete the replacement.
[document importPages:inserSet fromDocument:insertDocument atIndex:0];