Insert Pages into PDF
Contents
[
Hide
]
Inserting pages into an existing PDF is a common requirement when combining documents, adding content, or reorganizing reports. Using Aspose.PDF for Python, developers can programmatically insert pages from one PDF into another at a specified location.
This article shows how to use the insert method of the PdfFileEditor class. By specifying the page numbers to insert and the target location, you can merge content from different PDFs while maintaining the original formatting and structure.
- Create a PdfFileEditor Object.
- Define the Insertion Position and Pages.
- Insert Pages.
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
import sys
from os import path
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
# Insert Pages into PDF
def insert_pages_into_pdf(infile, sample_file, outfile):
# Create a PdfFileEditor object
pdf_editor = pdf_facades.PdfFileEditor()
# Define the page number where new pages will be inserted (1-based index)
insert_page_number = 2
pdf_editor.insert(infile, insert_page_number, sample_file, [1, 2], outfile)