Move or Copy PDF Pages on iOS

PSPDFKit for iOS lets you move or copy the pages of a document using the Processor or the Document Editor APIs.

Using Processor

To move pages via the Processor API, load your document and then configure the page move parameters on a Processor.Configuration instance. Processor is a great choice if you want to build an automated document processing operation:

let document = ...

guard let configuration = Processor.Configuration(document: document) else {
    print("Could not create a processor configuration. The document might be locked or invalid.")
    return
}

// Move the first page to the end of the document.
// Note the destination index (see the note at the bottom of the guide for additional details).
configuration.movePages(IndexSet(integer: 0), toDestinationIndex: document.pageCount)

let processor = Processor(configuration: configuration, securityOptions: nil)
do {
    // Write the modified document. This can be used to initialize
    // and present a new PSPDFKit document.
    try processor.write(toFileURL: destinationURL)
} catch {
    print(error)
}

To copy the page instead of moving it, replace the movePages call with an addNewPage call that’s configured to take a source page from the same document:

let template = PageTemplate(document: document, sourcePageIndex: 0)
let newPageConfiguration = PDFNewPageConfiguration(pageTemplate: template, builderBlock: nil)
configuration.addNewPage(at: document.pageCount, configuration: newPageConfiguration)

ℹ️ Note: Adding new pages with Processor is only available if you have the Document Editor component enabled in your license.

Using the Document Editor

Pages can also be moved and copied using the Document Editor API. The Document Editor is particularly useful if you want to build a general-purpose UI for manipulating pages, as it provides helpers that make building a document editing UI easy.

Here’s how you can use the Document Editor to move the current page of a displayed document:

// Instance method on a `PDFViewController` subclass.
@objc private func movePageToEnd() {
    guard let document = document, let editor = PDFDocumentEditor(document: document) else {
        print("Document editing not available.")
        return
    }

    // Move the current page to the end of the document.
    // Note the destination index (see note at the bottom of the guide for additional details).
    editor.movePages(IndexSet(integer: IndexSet.Element(pageIndex)), to: document.pageCount - 1)

    editor.save { _, error in
        if let error = error {
            print("Error while saving: \(error)")
        } else {
            DispatchQueue.main.async {
                // Reload the document in the UI.
                self.reloadData()
            }
        }
    }
}

ℹ️ Note: Destination page indexes are specified differently between corresponding methods on Processor and the Document Editor. This is because the Document Editor was designed to be easily connected to UI components such as UICollectionView and to match the behavior of built-in cell reordering. Processor determines destination indexes before adjusting for any removed pages, while the Document Editor determines destination indexes after applying any removals.

Similar to how it works with Processor, you can use the Document Editor to replace the movePages call with addPages to create a copy:

let template = PageTemplate(document: document, sourcePageIndex: 0)
let newPageConfiguration = PDFNewPageConfiguration(pageTemplate: template, builderBlock: nil)
let destinationRange = NSRange(location: Int(document.pageCount), length: 1)
editor.addPages(in: destinationRange, with: newPageConfiguration)

Using the Built-In UI

PSPDFKit for iOS comes with a pre-built user interface for document editing that includes options for moving and copying pages. To learn more, check out the Document Editing UI overview.