How to Split a PDF in Java

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()

```java :title=Quickstart //:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-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"));

    // Extract the first page (zero-based index 0) into a new document
    PdfDocument firstPage = pdf.copyPage(0);

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

}


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](https://ironpdf.com/java/docs/).

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.

<div class="hsg-featured-snippet">
    <h2>How to Split a PDF in Java</h2>
    <ol>
        <li><a href="https://ironpdf.com/java/#download-modal">Install the Java library to split PDFs</a></li>
        <li>Use the <strong>PdfDocument</strong> class to load the source PDF</li>
        <li>Use <code>copyPage</code> to extract a single page into a new document</li>
        <li>Use <code>copyPages</code> to extract a range of pages</li>
        <li>Use <code>splitBy</code> to divide the PDF at a specific page boundary</li>
        <li>Save each resulting document with <code>saveAs</code></li>
    </ol>
</div>

## 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:

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

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");
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
JAVA

For licensing options and trial keys, visit the license keys guide.

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 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.

//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-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("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"));
    }
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-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("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.

//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-page-range.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 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"));
    }
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/copy-page-range.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 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.

//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/split-by-boundary.java
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"));
    }
}
//:path=/static-assets/ironpdf-java/content-code-examples/how-to/java-split-pdf-tutorial/split-by-boundary.java
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 notesplitBy 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().

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity ...

Read More
Ready to Get Started?
Version: 2026.4 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast?
run a sample watch your HTML become a PDF.