Skip to main content

Remove PDF Pages

Remove PDF pages by index or reference.

Remove a Page by Index

Use the remove(int index) method to remove a page at a specified zero-based index.

The following code snippet removes the first page from a document:

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")))) {

            pdfDocument.getPages().remove(0);

            // 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);
            }
        }
    }
}

Remove a Page by Reference

Use the remove(Page page) method to remove a specific page instance from the document.

The following code snippet removes the first page by reference:

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

// Remove the specified page from the document.
pdfDocument.getPages().remove(page);

Next Steps