Split PDF programmatically
Contents
[
Hide
]
You can split PDF files using Aspose.PDF and get the results online at this link: products.aspose.app/pdf/splitter
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:
- Loop through the pages of PDF document through the Document object’s PageCollection collection.
- For each iteration, create a new Document object and add the individual Page object into the empty document.
- 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++;
}
}
}