Rotating PDF pages on iOS
Nutrient iOS SDK lets you rotate the pages of a document using the Processor or the Document Editor APIs.
Using the Processor API
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 Nutrient document. try processor.write(toFileURL: destinationURL)} catch { print(error)}
Rotating pages with Processor is only available if you have the Document Editor component enabled in your license.
Using Document Editor
Page rotation is also available as part of the Document Editor API. 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 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(opens in a new tab) in our Catalog(opens in a new tab) project for a runnable example.
Using the built-in UI
Nutrient iOS SDK comes with a prebuilt user interface for document editing that includes an option to rotate pages. To learn more, check out the document editor UI overview.