Move PDF Pages in Python
Contents
[
Hide
]
Move 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 using the
Documentclass. - Select a specific page to move (in this case, page 2) — this refers to a
Page. - Create a new PDF document (instantiate another
Document). - Add the selected page to the new PDF document using the destination document’s
PageCollection(for example,another_document.pages.add(page)). - Delete the page from the original document via its
PageCollection(for example,document.pages.delete(index)). - Save both documents.
The following code snippet shows you how to move one page.
import aspose.pdf as ap
def move_page_from_one_document_to_another(
input_file_name: str, output_file_name: str
) -> None:
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)
Move Multiple 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 (
Document). - Select multiple pages (in this case, pages 1 and 3) from the source document’s
PageCollection. - Loop through the selected pages and add each one to the destination document’s
PageCollection. - Save the destination document containing the moved pages.
- Delete the moved pages from the source document using its
PageCollection. - Save the modified source document with a new file name to preserve both versions.
The following code snippet shows how to move multiple pages.
import aspose.pdf as ap
def move_multiple_pages_from_one_document_to_another(
input_file_name: str, output_file_name: str
) -> None:
src_document = ap.Document(input_file_name)
dst_document = ap.Document()
pages = [1, 2]
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"))
Move a Page to a New Location in the Same 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 using the
Documentclass. - Select the page you want to move (page 2) — this is a
Page. - Add it to the end of the document using the document’s
PageCollection. - Delete the original page from its previous location via the
PageCollection. - Save the modified document as a new file.
import aspose.pdf as ap
def move_page_in_new_location_in_same_document(
input_file_name: str, output_file_name: str
) -> None:
src_document = ap.Document(input_file_name)
page = src_document.pages[2]
src_document.pages.add(page)
src_document.pages.delete(2)
# Save output file
src_document.save(output_file_name)