Moving PDF Pages programmatically via Python

Moving a Page from one PDF Document to Another

Aspose.PDF for Python lets you move a page (not just copy it) from one PDF to another. It removes the selected page from the original document and then adds it to a new PDF file.

Think of it as cutting out a page from one book and gluing it into another — the page no longer exists in the original file after the move.

  1. Open the source PDF document using the Document class.
  2. Select a specific page to move (in this case, page 2) — this refers to a Page.
  3. Create a new PDF document (instantiate another Document).
  4. Add the selected page to the new PDF document using the destination document’s PageCollection (for example, another_document.pages.add(page)).
  5. Delete the page from the original document via its PageCollection (for example, document.pages.delete(index)).
  6. Save both documents.

The following code snippet shows you how to move one page.


import os
import aspose.pdf as ap

# Global configuration
DATA_DIR = "your path here"

def moving_page_from_one_document_to_another(input_file_name, output_file_name):
    """
    Move a single page from one PDF document to another.

    Parameters:
    - input_file_name (str): Path to the source PDF file.
    - output_file_name (str): Path to the destination PDF file after moving the page.
    """
    document = ap.Document(input_file_name)
    page = document.pages[2]
    another_document = ap.Document()
    another_document.pages.add(page)
    document.pages.delete(2)
    document.save(input_file_name.replace(".pdf","_new.pdf"))
    another_document.save(output_file_name)

Moving bunch of Pages from one PDF Document to Another

Unlike copying, this operation transfers the selected pages — removing them from the source file and saving them in a new PDF.

  1. Create a new, empty destination document (Document).
  2. Select multiple pages (in this case, pages 1 and 3) from the source document’s PageCollection.
  3. Loop through the selected pages and add each one to the destination document’s PageCollection.
  4. Save the destination document containing the moved pages.
  5. Delete the moved pages from the source document using its PageCollection.
  6. Save the modified source document with a new file name to preserve both versions.

The following code snippet shows you how to insert an empty page at the end of a PDF file.


import os
import aspose.pdf as ap

# Global configuration
DATA_DIR = "your path here"

def moving_bunch_pages_from_one_document_to_another(input_file_name, output_file_name):
    """
    Move a set of pages from one PDF document to another.

    Parameters:
    - input_file_name (str): Path to the source PDF file.
    - output_file_name (str): Path to the destination PDF file where selected pages will be saved.
    """
    src_document = ap.Document(input_file_name)
    dst_document = ap.Document()
    pages = [1, 3]
    for page_index in pages:
        page = src_document.pages[page_index]
        dst_document.pages.add(page)
    # Save output files
    dst_document.save(output_file_name)
    src_document.pages.delete(pages)
    src_document.save(input_file_name.replace(".pdf","_new.pdf"))

Moving a Page in new location in the current PDF Document

It shows how to move a specific page to a different position within the same document — a common need when reorganizing or editing PDF layouts.

  1. Load the input PDF document using the Document class.
  2. Select the page you want to move (page 2) — this is a Page.
  3. Add it to the end of the document using the document’s PageCollection.
  4. Delete the original page from its previous location via the PageCollection.
  5. Save the modified document as a new file.

import os
import aspose.pdf as ap

# Global configuration
DATA_DIR = "your path here"

def moving_page_in_new_location_in_same_document(input_file_name, output_file_name):
    """
    Move a page to a new location within the same PDF document.

    Parameters:
    - input_file_name (str): Path to the source PDF file.
    - output_file_name (str): Path to the destination PDF file after moving the page.
    """
    srcDocument = ap.Document(input_file_name)

    page = srcDocument.pages[2]
    srcDocument.pages.add(page)
    srcDocument.pages.delete(2)

    # Save output file
    srcDocument.save(output_file_name)