PDF format has now become a standard for sharing and preserving documents. When working with PDF files, you may sometimes need to copy specific pages in the PDF to extract valuable content, create summaries, or simply share relevant sections without distributing the entire document. In this article, you will learn how to copy pages in PDF in Python using Spire.PDF for Python.
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python. It can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Copy Pages within the Same PDF in Python
To duplicate PDF pages, you can first create template based on a specified page in PDF, and then draw the template on a newly added page through the PdfPageBase.Canvas.DrawTemplate() method. The following are the detailed steps.
- Create a PdfDocument instance.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Get a specified page using PdfDocument.Pages[] property.
- Get the size of the page using PdfPageBase.Size property.
- Create a template based on the page using PdfPageBase.CreateTemplate() method.
- Add a new page of the same size at the end using PdfDocument.Pages.Add(size: SizeF, margins: PdfMargins) method. Or you can insert a new page of the same size at a specified location using PdfDocument.Pages.Insert(index: int, size: SizeF, margins: PdfMargins) method.
- Draw template on the newly added page using PdfPageBase.Canvas.DrawTemplate(template: PdfTemplate, location: PointF) method.
- Save the result file using PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import * from spire.pdf import * # Create a PdfDocument object pdf = PdfDocument() # Load a PDF file from disk pdf.LoadFromFile("Butterflies.pdf") # Get the first page page = pdf.Pages[0] # Get the size of the page size = page.Size # Create a template based on the page template = page.CreateTemplate() # Add a new page of the same size at the end page = pdf.Pages.Add(size, PdfMargins(0.0)) # Insert a new page at the specified location # page = pdf.Pages.Insert(1, size, PdfMargins(0.0)) # Draw the template on the newly added page page.Canvas.DrawTemplate(template, PointF(0.0, 0.0)) # Save the PDF file pdf.SaveToFile("CopyPDFPages.pdf"); pdf.Close()
Copy Pages from One PDF to Another in Python
Spire.PDF for Python also allows you to load two PDF files, create templates based on the pages in one PDF file, and then draw them onto the pages in another PDF file. The following are the detailed steps.
- Create a PdfDocument instance.
- Load two PDF files using PdfDocument.LoadFromFile() method.
- Get a specified page in the first PDF using PdfDocument.Pages[] property.
- Get the size of the page using PdfPageBase.Size property.
- Create a template based on the page using PdfPageBase.CreateTemplate() method.
- Insert a new page of the same size at a specified location in the second PDF using PdfDocument.Pages.Insert(index: int, size: SizeF, margins: PdfMargins) method. Or you can add a new page of the same size at the end of the second PDF using PdfDocument.Pages.Add(size: SizeF, margins: PdfMargins) method.
- Draw template on the newly added page using PdfPageBase.Canvas.DrawTemplate(template: PdfTemplate, location: PointF) method.
- Save the result file using PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import * from spire.pdf import * # Load the first PDF file pdf1 = PdfDocument() pdf1.LoadFromFile("Butterflies.pdf") # Load the second PDF file pdf2 = PdfDocument() pdf2.LoadFromFile("SamplePDF.pdf") # Get the first page in the first PDF file page = pdf1.Pages[0] # Get the size of the page size = page.Size # Create a template based on the page template = page.CreateTemplate() # Insert a new page at a specified location in the second PDF file newPage = pdf2.Pages.Insert(0, size, PdfMargins(0.0)) # Add a new page at the end of the second PDF file # newPage = pdf2.Pages.Add(size, PdfMargins(0.0)) # Draw the template on the newly added page newPage.Canvas.DrawTemplate(template, PointF(0.0, 0.0)) # Save the result file pdf2.SaveToFile("CopyPagesToAnotherPDF.pdf") pdf2.Close()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.