Rotating PDF Pages on iOS

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

Using Processor

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

// Rotate the first page 90 degrees clockwise.
configuration.rotatePage(0, by: 90)

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

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

Using the Document Editor

Page rotation 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 rotate the current page of a displayed document:

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

    // Rotate the current page 90 degrees clockwise. This API can rotate multiple pages at the same time.
    editor.rotatePages([Int(pageIndex)], rotation: 90)

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

You can check out RotatePageExample.swift in our Catalog project for a runnable example.

Using the Built-In UI

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