Moving PDF Pages programmatically via Python
Contents
[
Hide
]
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.
- Open the source PDF document.
- Select a specific page to move (in this case, page 2).
- Create a new PDF document.
- Add the selected page to the new PDF document.
- Delete the page from the original document.
- 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.
- Create a new, empty destination document.
- Select multiple pages (in this case, pages 1 and 3).
- Loop through the selected pages and add each one to the destination document.
- Save the destination document containing the moved pages.
- Delete the moved pages from the source document.
- 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.
- Load the input PDF document.
- Select the page you want to move (page 2).
- Add it to the end of the document.
- Delete the original page from its previous location.
- 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)