Rotating PDF Pages on Android

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

Using the Processor API

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

// Create a `PdfProcessorTask` in which to store the operation.
val task = PdfProcessorTask.empty()
// Rotate the first page 90 degrees.
task.rotatePage(0, PdfDocument.ROTATE_90)

// Process to an output document.
val outputFile = File(context.filesDir, "outputDocument.pdf")
PdfProcessor.processDocument(task, outputFile)
// Create a `PdfProcessorTask` in which to store the operation.
final PdfProcessorTask task = PdfProcessorTask.empty();
// Rotate the first page 90 degrees.
task.rotatePage(0, PdfDocument.ROTATE_90);

// Process to an output document.
final File outputFile = new File(context.getFilesDir(), "outputDocument.pdf");
PdfProcessor.processDocument(task, outputFile)

Using the Document Editor API

Page removal is also available as part of the Document Editor API. The Document Editor API 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 API to rotate the current page of a displayed document to 90 degrees:

// Instance method on a `PdfActivity` subclass.
fun rotatePages() {
    // Use the `PdfDocumentEditorFactory` to create a `PdfDocumentEditor`.
    val documentEditor = PdfDocumentEditorFactory.createForDocument(document)

    // Set up the output file location.
    val outputFile = File(filesDir, "outputDocument.pdf")

    // Rotate the current page.
    val pagesToRotate = setOf(pageIndex)

    // Make sure `disposable` is managed by the activity and destroyed correctly
    // when the activity is destroyed.
    val disposable: Disposable = documentEditor.rotatePages(pagesToRotate, PdfDocument.ROTATE_90)
        .flatMapCompletable { documentEditor.saveDocument(context, outputFile.outputStream(), null) }
        // Use `subscribeOn` to put `saveDocument` on a background thread, as it can be slow.
        .subscribeOn(Schedulers.io())
        // Make sure the resulting document rendering goes back on the main thread.
        .observeOn(AndroidSchedulers.mainThread())
        // You can display the saved document in a new activity with the following action on `subscribe`.
        .subscribe { showDocument(context, Uri.fromFile(outputFile), null) }
}
// Instance method on a `PdfActivity` subclass.
public void rotatePages() {
    // Use the `PdfDocumentEditorFactory` to create a `PdfDocumentEditor`.
    final PdfDocumentEditor documentEditor =
        PdfDocumentEditorFactory.createForDocument(document);

    // Set up the output file location.
    final File outputFile = new File(getFilesDir(), "outputDocument.pdf");

    // Rotate the current page.
    final HashSet<Integer> pagesToRotate = new HashSet<>(Arrays.asList(getPageIndex()));

    // Make sure `disposable` is managed by the activity and destroyed correctly
    // when the activity is destroyed.
    final Disposable disposable = documentEditor.rotatePages(pagesToRotate, PdfDocument.ROTATE_90)
        .flatMapCompletable(saved -> documentEditor.saveDocument(context, new FileOutputStream(outputFile), null))
        // Use `subscribeOn` to put `saveDocument` on a background thread, as it can be slow.
        .subscribeOn(Schedulers.io())
        // Make sure the resulting document rendering goes back on the main thread.
        .observeOn(AndroidSchedulers.mainThread())
        // You can display the saved document in a new activity with the following action on `subscribe`.
        .subscribe( () -> { PdfActivity.showDocument(context,
            Uri.fromFile(outputFile), null);
        });
}

Using the Built-In UI

PSPDFKit for Android 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.