Move or Rearrange PDF Pages in Java

Pages can be moved to a new location in a document by using the Document Editor. To do this, specify the pages you wish to move and the index they should be moved to:

// Move page 1 after index 5.
Set<Integer> pages = new HashSet<>();
pages.add(1);
documentEditor.movePages(pages, 5, DocumentEditor.IndexPosition.AfterIndex);

Chaining Edits Together

Document edits can be chained to perform multiple synchronous operations to create a single document. For example:

File file = File.createTempFile("documentEditorOutput", ".pdf");
DocumentEditor documentEditor = documentToEdit.createDocumentEditor();
Set<Integer> pages = new HashSet<>();
pages.add(1);
pages.add(4);
documentEditor.removePages(pages);
Set<Integer> movePages = new HashSet<>();
movePages.add(1);
documentEditor.movePages(movePages, 5, DocumentEditor.IndexPosition.AfterIndex);
documentEditor.saveDocument(new FileDataProvider(file));