Rotate PDF Pages in Java

The following example shows how to rotate pages using the Document Editor.

Rotating Pages

Rotating pages is done using the DocumentEditor.rotatePages method, which takes a Rotation enum value to specify how much to rotate. Pages can be rotated in 90-degree increments relative to the current rotation, so if a page is already rotated, this will add the specified rotation:

final DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
final Set<Integer> pages = new HashSet<>(Arrays.asList(0, 2));
final File file = new File("documentEditorOutput.pdf");
documentEditor.saveDocument(new FileDataProvider(file));

The code above will rotate the first and third page 90 degrees clockwise. If the first page was already rotated 90 degrees, it’ll now be rotated 180 degrees.

Chaining Operations

It’s also possible to chain editor operations. This can be done either by using a JSON operations array or by calling multiple Document Editor methods before SaveDocument:

final Set<Integer> pages = new HashSet<>();
documentEditor.rotatePages(pages, Rotation.Degrees90);
documentEditor.saveDocument(new FileDataProvider(file));