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:
- Open an existing
Documentusing appropriate methods. - Insert a new empty page at a specific index using the
PageCollectioninsert()method. - Save the modified
Documentto 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:
- Open an existing
Documentusing appropriate methods. - Add a new empty page to the end of the document using the
PageCollectionadd()method. - 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.
- Create a new
Document. - Add a new blank
Pageand write some text on it usingTextFragment. - Open another existing
Document. - Copy a
Pagefrom that document. - Paste the copied page into your main document using
PageCollection. - 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)