Removing Pages from a PDF on iOS

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

Using Processor

To remove pages via the Processor API, load your document and then configure the page removal 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
}

// Remove the first page. This API can be used to remove multiple pages at the same time.
configuration.removePages(IndexSet(integer: 0))

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)
}

Using the Document Editor

Page removal is also available as part of 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 remove the current page of a displayed document:

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

    // Remove the currently visible page.
    editor.removePages(IndexSet(integer: IndexSet.Element(pageIndex)))

    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()
            }
        }
    }
}

Using the Built-In UI

PSPDFKit for iOS comes with a pre-built user interface for document editing that includes an option to remove pages. To learn more, check out the Document Editor UI overview.