Add PDF Pages in Python

Aspose.PDF for Python via .NET provides flexible page-level operations for PDF documents. You can manage pages through PageCollection and add pages to a Document at specific positions or at the end of the file.

Use this page when you need to insert new blank pages into an existing PDF or append pages to the end of a document during generation workflows.

Add or Insert Pages in a PDF File

Aspose.PDF for Python via .NET supports both page insertion at a specific index and appending pages at the end of a PDF.

Insert an Empty Page in a PDF File

To insert an empty page in a PDF file:

  1. Open an existing Document using appropriate methods.
  2. Insert a new empty page at a specific index using the PageCollection insert() method.
  3. Save the modified Document to the desired output path.

Insert an empty page into an existing PDF file at a specified position:

import aspose.pdf as ap

def insert_empty_page(input_file_name: str, output_file_name: str) -> None:
    document = ap.Document(input_file_name)
    document.pages.insert(2)
    document.save(output_file_name)

Add an Empty Page at the End of a PDF File

Sometimes, you want to ensure that a document ends on an empty page. This topic explains how to insert an empty page at the end of the PDF document.

To insert an empty page at the end of a PDF file:

  1. Open an existing Document using appropriate methods.
  2. Add a new empty page to the end of the document using the PageCollection add() method.
  3. Save the updated Document.

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

import aspose.pdf as ap

def add_empty_page_to_end(input_file_name: str, output_file_name: str) -> None:
    document = ap.Document(input_file_name)
    document.pages.add()
    document.save(output_file_name)

Add a Page from Another PDF Document

With Aspose.PDF for Python via .NET, you can create a new Document, add an initial page, and then import a page from another PDF into it.

  1. Create a new Document.
  2. Add a new blank Page and write some text on it using TextFragment.
  3. Open another existing Document.
  4. Copy a Page from that document.
  5. Paste the copied page into your main document using PageCollection.
  6. Save the combined file.
import aspose.pdf as ap

def add_page_from_another_document(input_file_name: str, output_file_name: str) -> None:
    document = ap.Document()
    page = document.pages.add()
    text_fragment = ap.text.TextFragment("This is first page!")
    page.paragraphs.add(text_fragment)

    another_document = ap.Document(input_file_name)
    document.pages.add(another_document.pages[1])

    document.save(output_file_name)