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.
- 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()
-
1Install IronPDF with Maven Central Repository
-
2Copy and run this code snippet.
:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-delete-pdf-pages-tutorial/quickstart.javaJava -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
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.
Minimal Workflow (5 steps)
- Install the Java library to delete PDF pages
- Use the PdfDocument class to load the PDF
- Use
removePageto delete a single page by index - Use
removePageswithPageSelectionto delete multiple pages or a range - Save the modified PDF with
saveAs
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.
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"));
}
}
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"));
}
}
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"));
}
}
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"));
}
}
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 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 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.