Python: Split or Merge PDF Pages

Modifying PDF documents to suit various usage scenarios is a common task for PDF document creators and managers. Among these operations, splitting and merging PDF pages can assist in reorganizing PDF content for printing, typesetting, etc. By using Python programs, developers can easily split one page from a PDF document to several pages or merge multiple PDF pages into a single page. This article will demonstrate how to use Spire.PDF for Python for splitting and merging PDF pages in Python programs.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They 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: How to Install Spire.PDF for Python on Windows

Split One PDF Page into Several PDF Pages with Python

With Spire.PDF for Python, developers draw a PDF page on a new PDF page using the PdfPageBase.CreateTemplate().Draw(newPage PdfPageBase, PointF) method. When drawing, if the current new page cannot fully accommodate the content of the original page, a new page is automatically created, and the remaining content is drawn on it. Therefore, we can create a new PDF document and control the drawing result by specifying the page size to achieve specified division of PDF pages horizontally or vertically.

Here are the steps to vertically split a PDF page into two separate PDF pages:

  • Create an object of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the first page of the document using PdfDocument.Pages.get_Item() method.
  • Create a new PDF document by creating an object of PdfDocument class.
  • Set the margins of the new document to 0 through PdfDocument.PageSettings.Margins.All property.
  • Get the width and height of the retrieved page through PdfPageBase.Size.Width property and PdfPageBase.Size.Height property.
  • Set the width of the new PDF document to the same as the retrieved page through PdfDocument.PageSettings.Width property and its height to half of the retrieved page's height through PdfDocument.PageSettings.Height property.
  • Add a new page in the new document using PdfDocument.Pages.Add() method.
  • Draw the content of the retrieved page onto the new page using PdfPageBase.CreateTemplate().Draw() method.
  • Save the new document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf import *
from spire.pdf.common import *

# Create an object of PdfDocument class and load a PDF document
pdf = PdfDocument()
pdf.LoadFromFile("Sample.pdf")

# Get the first page of the document
page = pdf.Pages.get_Item(0)

# Create a new PDF document
newPdf = PdfDocument()

# Set the margins of the new PDF document to 0
newPdf.PageSettings.Margins.All = 0.0

# Get the width and height of the retrieved page
width = page.Size.Width
height = page.Size.Height

# Set the width of the new PDF document to the same as the retrieved page and its height to half of the retrieved page's height
newPdf.PageSettings.Width = width
newPdf.PageSettings.Height = height / 2

# Add a new page to the new PDF document
newPage = newPdf.Pages.Add()

# Draw the content of the retrieved page onto the new page
page.CreateTemplate().Draw(newPage, PointF(0.0, 0.0))

# Save the new PDF document
newPdf.SaveToFile("output/SplitPDFPage.pdf")
pdf.Close()
newPdf.Close()

Python: Split or Merge PDF Pages

Merge Multiple PDF Pages into a Single Page with Python

Similarly, developers can merge PDF pages by drawing different pages on the same PDF page. It should be noted that the pages to be merged are preferably in the same width or height, otherwise it is necessary to take the maximum value to ensure correct drawing.

The detailed steps for merging two PDF pages into a single PDF page are as follows:

  • Create an object of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the first and second pages of the document using PdfDocument.Pages.get_Item() method.
  • Create a new PDF document by creating an object of PdfDocument class.
  • Set the margins of the new document to 0 through PdfDocument.PageSettings.Margins.All property.
  • Get the width and height of the two retrieved pages through PdfPageBase.Size.Width property and PdfPageBase.Size.Height property.
  • Set the width of the new PDF document to the same as the retrieved pages through PdfDocument.PageSettings.Width property and its height to the sum of the two retrieved pages' heights through PdfDocument.PageSettings.Height property.
  • Draw the content of the two retrieved pages onto the new page using PdfPageBase.CreateTemplate().Draw() method.
  • Save the new document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf import *
from spire.pdf.common import *

# Create an object of PdfDocument class and load a PDF document
pdf = PdfDocument()
pdf.LoadFromFile("Sample1.pdf")

# Get the first page and the second page of the document
page = pdf.Pages.get_Item(0)
page1 = pdf.Pages.get_Item(0)

# Create a new PDF document
newPdf = PdfDocument()

# Set the margins of the new PDF document to 0
newPdf.PageSettings.Margins.All = 0.0

# Set the page width of the new document to the same as the retrieved page
newPdf.PageSettings.Width = page.Size.Width

# Set the page height of the new document to the sum of the heights of the two retrieved pages
newPdf.PageSettings.Height = page.Size.Height + page1.Size.Height

# Add a new page to the new PDF document
newPage = newPdf.Pages.Add()

# Draw the content of the retrieved pages onto the new page
page.CreateTemplate().Draw(newPage, PointF(0.0, 0.0))
page1.CreateTemplate().Draw(newPage, PointF(0.0, page.Size.Height))

# Save the new document
newPdf.SaveToFile("output/MergePDFPages.pdf")
pdf.Close()
newPdf.Close()

Python: Split or Merge PDF Pages

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.