Skip to main content

Reorder PDF Pages

To reorder pages in a document, retrieve a page by index and insert it at a new position in the collection.

The following code snippet moves the first page to the end of the document and saves the result to a new PDF file:

import com.devexpress.docs.pdf.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument(FileChannel.open(
                Path.of("document.pdf")))) {

            Page page = pdfDocument.getPages().getFirst();
            pdfDocument.getPages().add(page);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Next Steps