IRONSOFTWAREHOME

How to Delete Pages From a PDF in Java

Curtis Chau
Curtis Chau
Updated: July 29, 2026

Removing PDF pages in Java is straightforward with IronPDF: the library exposes removePage and removePages methods backed by PageSelection, giving you precise control over which pages to delete -- whether that is a single page, a contiguous range, or a scattered set of page indexes. All page indexes in IronPDF are zero-based, so the first page of a document is always index 0.

Quickstart: Delete PDF Pages in Java
  1. Install IronPDF for Java via Maven or Gradle
  2. Set your license key with License.setLicenseKey()
  3. Load the PDF with PdfDocument.fromFile()
  4. Remove a single page with removePage()
  5. Save the result with saveAs()
  1. 1Install IronPDF with Maven Central Repository

    mvn install

  2. 2Copy and run this code snippet.

    :path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/quickstart.java
    Java
  3. 3Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

Removing pages from a PDF is a common document-processing task. You may need to strip a cover page before distributing a report, cut confidential sections from a document before sharing it externally, or clean up blank pages that a scanner or template introduced. IronPDF handles all of these cases through a consistent Java API without requiring any native PDF editing tools on the host machine.

The library integrates into Java applications via Maven or Gradle and supports the full range of PDF manipulation operations beyond page removal, including merging PDFs, splitting documents, and adding watermarks. For a complete overview of setup and available features, visit the Get Started Overview.

The examples in this guide cover three scenarios: removing a single page by index, removing a contiguous page range using PageSelection, and removing multiple non-consecutive pages safely without triggering index-shift errors.

What Do I Need Before Getting Started?

Before removing pages from a PDF, confirm that IronPDF is configured in your Java project. The library requires Java 8 or higher and integrates through Maven or Gradle. Add the IronPdf dependency to your project build file. For full setup instructions, refer to the Get Started Overview.

A valid license key is required for both development and production use. Set the key at the start of your application before calling any IronPDF methods. For details on licensing options, visit the license keys guide.

Tips: All page indexes in IronPDF use zero-based numbering. Page 1 of your document is index 0, page 2 is index 1, and so on.

How Do I Delete a Single Page From a PDF?

The removePage(int pageIndex) method accepts a zero-based page index and removes exactly that page from the document. After the call completes, all subsequent pages shift down by one position, so any index you cached before the call may no longer point to the same page.

For example, if a document has five pages (indexes 0 through 4) and you remove index 2, the page that was at index 3 is now at index 2, and the page that was at index 4 is now at index 3. Plan your removal sequence with this shift in mind, particularly when calling removePage multiple times in a row.

import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key before calling any IronPDF methods
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load the source PDF from disk
        PdfDocument pdf = PdfDocument.fromFile(Path.of("report.pdf"));

        // Remove the cover page at index 0 (the first page)
        pdf.removePage(0);

        // Save the modified PDF -- the original file is not overwritten
        pdf.saveAs(Path.of("report-no-cover.pdf"));
    }
}
Java

This pattern is the simplest approach when you know exactly which page to remove and you only need to remove one. For removing a contiguous range of pages in a single operation, use removePages with a PageSelection range instead.

How Do I Delete a Range of Pages From a PDF?

The removePages(PageSelection) method deletes all pages covered by the given selection in a single atomic operation, which avoids the index-shift problem that arises when calling removePage multiple times. Use PageSelection.pageRange(int fromIndex, int toIndex) to specify the range -- both endpoints are inclusive and zero-based.

The example below removes pages 3, 4, and 5 of a document by passing fromIndex = 2 and toIndex = 4. Because all three pages are removed at once, no intermediate index shifting occurs during the operation.

import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key before calling any IronPDF methods
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load a multi-page PDF from disk
        PdfDocument pdf = PdfDocument.fromFile(Path.of("annual-report.pdf"));

        // Remove pages 3, 4, and 5 using a zero-based inclusive range (indexes 2 to 4)
        pdf.removePages(PageSelection.pageRange(2, 4));

        // Save the result to a new file
        pdf.saveAs(Path.of("annual-report-trimmed.pdf"));
    }
}
Java

PageSelection.pageRange is the preferred approach whenever you need to remove a block of adjacent pages. It is cleaner and more efficient than looping over individual removePage calls, and the single-operation semantics mean the page count is updated only once.

How Do I Delete Multiple Non-Consecutive Pages?

When you need to remove pages that are not adjacent to each other, you have two practical options: use removePages with a PageSelection that targets individual indexes, or call removePage multiple times in a carefully ordered sequence.

If you call removePage multiple times, always work from the highest index to the lowest. Removing a lower-indexed page first shifts all higher indexes down by one, which causes subsequent calls to target the wrong pages. By starting at the end of the document and working backward, each removal leaves the remaining lower indexes undisturbed.

The example below removes the first page, a middle page, and the last page of a six-page document. The calls are ordered from highest to lowest index -- 5, 3, 0 -- to prevent index drift.

import java.io.IOException;
import java.nio.file.Path;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key before calling any IronPDF methods
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load a six-page PDF from disk
        PdfDocument pdf = PdfDocument.fromFile(Path.of("contract.pdf"));

        // Remove non-consecutive pages: always work from highest index to lowest
        // to prevent index shifting from affecting subsequent removals.

        // Remove the last page (index 5 in a six-page document)
        pdf.removePage(5);

        // Remove a page in the middle (index 3 -- now safe because no lower index has shifted)
        pdf.removePage(3);

        // Remove the first page (index 0 -- lowest, so processed last)
        pdf.removePage(0);

        // Save the modified PDF
        pdf.saveAs(Path.of("contract-redacted.pdf"));
    }
}
Java
Please note: When building the list of page indexes to remove at runtime -- for example from user input or a configuration file -- sort the list in descending order before iterating. This ensures each removePage call targets the correct page regardless of how many pages have already been removed.

If the set of pages to remove is determined dynamically, a concise pattern is to sort your index list in reverse and loop:

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key before calling any IronPDF methods
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load the PDF from disk
        PdfDocument pdf = PdfDocument.fromFile(Path.of("document.pdf"));

        // Define the zero-based page indexes to remove
        List<Integer> pagesToRemove = Arrays.asList(0, 3, 5);

        // Sort descending to avoid index-shift errors during sequential removal
        pagesToRemove.sort(Comparator.reverseOrder());

        // Remove each page in reverse-index order
        for (int pageIndex : pagesToRemove) {
            pdf.removePage(pageIndex);
        }

        // Save the modified PDF
        pdf.saveAs(Path.of("document-pages-removed.pdf"));
    }
}
Java

Sorting the index list in descending order before the loop is a safe, general-purpose pattern that works regardless of how many pages are targeted or how the input list is ordered.

What Are the Next Steps for Deleting PDF Pages in Java?

IronPDF's removePage and removePages methods give you targeted control over page deletion -- whether you need to strip a single page, excise a range, or remove a scattered set of pages. The zero-based indexing model is consistent across the full IronPDF Java API, so the same conventions apply when you move on to page splitting, merging, or reordering.

To continue working with PDF page structure and document manipulation, explore these related resources:

Start your free trial to remove pages from PDFs in your Java workflow. To purchase a license for production use, view licensing options.

Frequently Asked Questions

How do I delete a single PDF page using Java and IronPDF?

To delete a single PDF page using IronPDF in Java, use the `removePage(int pageIndex)` method. This method removes the page at the specified zero-based index. Ensure that any cached page indexes are updated, as removing a page shifts subsequent pages down by one position.

What is the process to remove a range of pages from a PDF in Java with IronPDF?

Use the `removePages(PageSelection)` method, specifying the range with `PageSelection.pageRange(int fromIndex, int toIndex)`. This deletes all pages in the range inclusively, avoiding index-shift issues.

Can I delete multiple non-consecutive pages from a PDF with IronPDF in Java?

Yes, you can remove non-consecutive pages by using `removePages` with a `PageSelection` for individual indexes, or by calling `removePage` multiple times in descending order to prevent index shift errors.

What are the prerequisites for removing PDF pages in Java using IronPDF?

Before removing pages, ensure IronPDF is integrated into your Java project (Java 8 or higher) via Maven or Gradle. A valid license key is also required for development and production.

How do zero-based indexes work in IronPDF?

IronPDF uses zero-based indexing, meaning the first page of a PDF document is at index 0, the second page at index 1, etc. This is consistent across all IronPDF methods, including page removal.

What is the advantage of using `removePages` over repeated `removePage` calls?

`removePages` with `PageSelection` allows you to delete multiple pages in a single atomic operation, minimizing the risk of index-shift errors that can occur when calling `removePage` multiple times.

How can I handle index-shift errors when removing PDF pages in IronPDF?

Avoid index-shift errors by sorting the page indexes in descending order before removal and processing from the highest to lowest index, or use the `removePages` method for contiguous page ranges.

How do you save a PDF after removing pages with IronPDF in Java?

After removing pages, save the modified PDF using the `saveAs(Path path)` method, specifying the file path for the new PDF.

What methods does IronPDF provide for PDF page manipulation beyond deletion?

IronPDF offers methods for merging, splitting, and adding watermarks to PDF documents, in addition to page deletion, all accessible through its Java API.

Where can I find additional IronPDF Java examples for PDF manipulation?

Visit the IronPDF for Java examples page for copy-paste code samples and an overview of the full API, including document manipulation capabilities beyond page deletion.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Version:2026.8just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
Java Maven Library for PDF
Install with Maven

Version: 2026.8

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>ironpdf</artifactId>
   <version>2026.8.2</version>
</dependency>
https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2026.8.2
or
Java PDF JAR
Download JAR

Version: 2026.8

Manually install into your project

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
Java Maven Library for PDF
Install with Maven

Version: 2026.8

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>ironpdf</artifactId>
   <version>2026.8.2</version>
</dependency>
https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2026.8.2
or
Java PDF JAR
Download JAR

Version: 2026.8

Manually install into your project