Split PDF programmatically

Contents
[ ]

This topic shows how to split PDF pages with Aspose.PDF for Java into individual PDF files in your Java applications. To split PDF pages into single page PDF files using Java, the following steps can be followed:

  1. Loop through the pages of PDF document through the Document object’s PageCollection collection.
  2. For each iteration, create a new Document object and add the individual Page object into the empty document.
  3. Save the new PDF using Save method.

The following Java code snippet shows you how to split PDF pages into individual PDF files.

package com.aspose.pdf.examples;

import com.aspose.pdf.*;

public class ExampleSplit {
    // The path to the documents directory.
    private static String _dataDir = "/home/admin1/pdf-examples/Samples/";

    public static void Split() {
        
        // Open document
        Document pdfDocument = new Document(_dataDir + "SplitToPages.pdf");

        int pageCount = 1;

        // Loop through all the pages
        for(Page pdfPage : pdfDocument.getPages())
        {
            Document newDocument = new Document();
            newDocument.getPages().add(pdfPage);
            newDocument.save(_dataDir + "page_" + pageCount + "_out" + ".pdf");
            pageCount++;
        }
    }

}