How to Delete Pages From a PDF in Java
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
- Install IronPDF for Java via Maven or Gradle
- Set your license key with
License.setLicenseKey() - Load the PDF with
PdfDocument.fromFile() - Remove a single page with
removePage() - Save the result with
saveAs()
```java :title=Quickstart //:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/quickstart.java 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("source.pdf"));
// Remove the first page (zero-based index 0)
pdf.removePage(0);
// Save the modified PDF to a new file
pdf.saveAs(Path.of("modified.pdf"));
}
}
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](https://ironpdf.com/java/how-to/java-merge-pdf-tutorial/), [splitting documents](https://ironpdf.com/java/how-to/java-split-pdf-tutorial/), and [adding watermarks](https://ironpdf.com/java/how-to/custom-watermark/). For a complete overview of setup and available features, visit the [Get Started Overview](https://ironpdf.com/java/docs/).
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.
<div class="hsg-featured-snippet">
<h2>How to Delete Pages From a PDF in Java</h2>
<ol>
<li><a href="https://ironpdf.com/java/#download-modal">Install the Java library to delete PDF pages</a></li>
<li>Use the <strong>PdfDocument</strong> class to load the PDF</li>
<li>Use <code>removePage</code> to delete a single page by index</li>
<li>Use <code>removePages</code> with <code>PageSelection</code> to delete multiple pages or a range</li>
<li>Save the modified PDF with <code>saveAs</code></li>
</ol>
</div>
## 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](https://central.sonatype.com/artifact/com.ironsoftware/ironpdf). Add the `IronPdf` dependency to your project build file. For full setup instructions, refer to the [Get Started Overview](https://ironpdf.com/java/docs/).
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](https://ironpdf.com/java/get-started/license-keys/).
TipsAll 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.
```java
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/single-page.java
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"));
}
}
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.
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/page-range.java
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"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/page-range.java
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"));
}
}
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.
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/non-consecutive-pages.java
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"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/non-consecutive-pages.java
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"));
}
}
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:
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/dynamic-removal.java
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"));
}
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/dynamic-removal.java
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"));
}
}
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:
- Split PDFs in Java: extract specific pages or page ranges into separate PDF files
- Merge PDFs in Java: combine multiple documents into a single PDF with full page control
- Compress PDFs in Java: reduce file size after removing pages to optimize the output for distribution
- Outlines and bookmarks in Java: manage navigation structures that may reference the pages you removed
- IronPDF for Java examples: copy-paste code samples for the full IronPDF Java API
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 page from a PDF in Java?
Load the PDF with PdfDocument.fromFile(), then call pdf.removePage(pageIndex) with a zero-based index. For example, pdf.removePage(0) removes the first page. Save the result with pdf.saveAs().
How do I remove a range of pages from a PDF in Java?
Call pdf.removePages(PageSelection.pageRange(fromIndex, toIndex)). Both the from and to indexes are zero-based and inclusive. For example, PageSelection.pageRange(2, 4) removes the third, fourth, and fifth pages of the document in a single atomic operation.
Why do I need to remove pages from highest index to lowest?
When you call removePage multiple times, each removal shifts all subsequent page indexes down by one. Removing from highest to lowest index ensures that each call targets the correct page because no lower-indexed pages have yet been removed to cause a shift.
What is PageSelection in IronPDF Java?
PageSelection is a class in the com.ironsoftware.ironpdf.edit package that defines which pages a method should act on. PageSelection.pageRange(fromIndex, toIndex) creates a selection covering a contiguous block of zero-based, inclusive page indexes for use with removePages.
What are the prerequisites for deleting PDF pages in Java with IronPDF?
You need Java 8 or higher, the IronPDF library added as a Maven or Gradle dependency, and a valid license key set with License.setLicenseKey() before any IronPDF calls. No native PDF tools or additional frameworks are required.
Does removePage modify the original PDF file?
No. removePage and removePages modify the in-memory PdfDocument object. The original file on disk is unchanged until you call pdf.saveAs(Path.of("output.pdf")) to write the modified document to a new path.


