Split PDF programmatically

Contents
[ ]

This topic shows how to split PDF pages with Aspose.PDF for PHP via Java into individual PDF files in your PHP applications. To split PDF pages into single page PDF files using PHP, 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 PHP code snippet shows you how to split PDF pages into individual PDF files.


    // Open document
    $document = new Document($inputFile);
    $pages = $document->getPages();
    $pagesSize = java_values($pages->size());
       
    // Loop through all the pages
    for ($pageCount = 1; $pageCount <= $pagesSize; $pageCount++) {
        $page = $pages->get_Item($pageCount);
        $newDocument = new Document();
        $newDocument->getPages()->add($page);
        $newDocument->save($outputFile . "page_" . $pageCount . ".pdf");
        $newDocument->close();
    }
    $document->close();