IRONSOFTWAREHOME

How to Split a PDF in Java

Curtis Chau
Curtis Chau
Updated: July 29, 2026

Splitting a PDF in Java is straightforward with IronPDF, which provides copyPage, copyPages, and splitBy methods on the PdfDocument class for extracting individual pages, pulling page ranges, or dividing a document at a boundary. Each method returns a new PdfDocument that you save independently of the original.

Quickstart: Split a PDF 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. Use copyPage() to extract a single page
  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-split-pdf-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

PDF splitting is a common requirement when producing documents programmatically. A report generated as a single file may need to be broken into individual sections before distribution. Archival workflows often require storing each chapter or invoice as a separate file. Selective sharing scenarios, such as sending only a specific page range to a client, benefit from extracting exactly the pages required rather than sending the entire document.

IronPDF handles all three splitting patterns through a consistent API on the PdfDocument class. The same class used to generate PDFs from HTML or load existing files exposes the page extraction methods, so splitting integrates naturally into any existing IronPDF workflow. For setup and dependency configuration, refer to the Get Started Overview.

All page indexes in IronPDF are zero-based. If your source document has ten pages, valid indexes run from 0 through 9. This applies to copyPage, both bounds of copyPages, and the boundary argument of splitBy. Passing an out-of-range index will throw an exception at runtime, so validate page counts against pdf.getPageCount() when working with documents of variable length.

What Do I Need Before Getting Started?

Before splitting PDFs, confirm that IronPDF is added to your Java project as a Maven or Gradle dependency. The library requires Java 8 or higher. Add the IronPDF artifact from Maven Central to your build file:

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>2024.9.1</version>
</dependency>
XML

For Gradle projects, add the equivalent implementation line to your build.gradle. Full dependency and configuration instructions are available in the Get Started Overview.

A valid license key is required. Set it at application startup before calling any IronPDF method:

License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
Java

For licensing options and trial keys, 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 Extract a Single Page From a PDF?

The copyPage(int pageIndex) method extracts one page from a PdfDocument and returns it as a new, independent PdfDocument. The original document is not modified. The pageIndex argument is zero-based: pass 0 for the first page, 1 for the second, and so on.

This method is appropriate when your workflow requires isolating a specific page, such as extracting a cover page, a signed signature page, or a single invoice from a batch-generated document.

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

        // Extract the third page (zero-based index 2) into a new document
        PdfDocument thirdPage = pdf.copyPage(2);

        // Save the extracted page as a standalone PDF
        thirdPage.saveAs(Path.of("third-page.pdf"));
    }
}
Java

The returned PdfDocument is fully independent of the source. You can apply further transformations, add annotations, or merge it with other documents before saving.

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

The copyPages(int fromIndex, int toIndex) method extracts a contiguous range of pages and returns them as a new PdfDocument. Both fromIndex and toIndex are zero-based and inclusive: passing copyPages(1, 4) on a ten-page document extracts pages 2, 3, 4, and 5 (indexes 1 through 4).

Page range extraction suits scenarios such as pulling a chapter from a multi-chapter report, extracting a multi-page appendix, or isolating a set of slides from a presentation exported as PDF.

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 multi-page source PDF
        PdfDocument pdf = PdfDocument.fromFile(Path.of("multi-page-report.pdf"));

        // Extract pages 2 through 5 (zero-based indexes 1 through 4, inclusive)
        PdfDocument chapter = pdf.copyPages(1, 4);

        // Save the extracted page range as a standalone document
        chapter.saveAs(Path.of("chapter-pages-2-to-5.pdf"));
    }
}
Java

If the source document has fewer pages than the requested toIndex, IronPDF will throw an exception. Check pdf.getPageCount() before calling copyPages when working with documents whose page count may vary.

How Do I Split a PDF Into Two Documents?

The splitBy(int splitAfterPageIndex) method divides a PDF at a boundary and returns a List<PdfDocument> containing exactly two elements. The first element contains pages from index 0 through splitAfterPageIndex (inclusive). The second element contains all remaining pages from splitAfterPageIndex + 1 through the end of the document.

For example, calling splitBy(2) on a ten-page document produces:

  • parts.get(0): pages 0, 1, 2 (the first three pages)
  • parts.get(1): pages 3 through 9 (the remaining seven pages)

This method suits splitting a document at a known boundary: separating a cover sheet from the body, dividing a document at a section break, or cutting a batch file into two halves for parallel processing.

import java.io.IOException;
import java.nio.file.Path;
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 source PDF from disk
        PdfDocument pdf = PdfDocument.fromFile(Path.of("source.pdf"));

        // Split after page index 2 -- first part: pages 0-2, second part: pages 3 to end
        List<PdfDocument> parts = pdf.splitBy(2);

        // Save the first part (pages 0 through 2)
        parts.get(0).saveAs(Path.of("part-one.pdf"));

        // Save the second part (pages 3 through end)
        parts.get(1).saveAs(Path.of("part-two.pdf"));
    }
}
Java
Please note: splitBy always returns a list with exactly two elements. If splitAfterPageIndex equals the last page index of the document, the second element will be an empty document. Validate the split boundary against pdf.getPageCount() - 1 when the split point is calculated dynamically.

What Are the Next Steps for Splitting PDFs in Java?

IronPDF's copyPage, copyPages, and splitBy methods cover the full range of PDF splitting requirements in Java, from single-page extraction to range-based splitting to boundary-based division. Each method returns an independent PdfDocument ready for further manipulation or immediate saving.

To continue working with PDF documents in Java, explore these related resources:

Start your free trial to add PDF splitting to your Java workflow. To purchase a license for production use, view licensing options.

Frequently Asked Questions

How do I split a PDF in Java with IronPDF?

Load your PDF with PdfDocument.fromFile(), then call copyPage(index) to extract a single page, copyPages(from, to) to extract a range, or splitBy(index) to divide the document into two parts. Save each result with saveAs().

What is the difference between copyPage, copyPages, and splitBy?

copyPage(index) extracts a single page by zero-based index. copyPages(fromIndex, toIndex) extracts a contiguous range where both bounds are inclusive. splitBy(index) divides the document at a boundary and returns a List<PdfDocument> with two elements: pages 0 through the boundary index, and all remaining pages.

Are page indexes in IronPDF zero-based or one-based?

Page indexes in IronPDF are zero-based. The first page of a document is index 0, the second is index 1, and so on. This applies to copyPage, copyPages, and splitBy.

What does splitBy return?

splitBy(splitAfterPageIndex) returns a List<PdfDocument> with exactly two elements. The first element contains pages 0 through the split index (inclusive). The second element contains all remaining pages. If the split index is the last page, the second element will be an empty document.

What are the prerequisites for splitting PDFs 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.

How do I extract pages 2 through 5 from a PDF in Java?

Use pdf.copyPages(1, 4) to extract pages 2 through 5. IronPDF uses zero-based indexes, so page 2 is index 1 and page 5 is index 4. Both bounds are inclusive. Save the result with saveAs().

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

Download Now

Manually install into your project

Key in blue circle

Get your free 30-day Trial Key instantly.

No limitations. 100% unlocked. No credit card.

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