Python: Change the Margins of a PDF Document

Margins in a PDF document refer to the blank spaces surrounding the content on each page. They act as a buffer zone between the text or images and the edges of the page. Changing the margins of a PDF document can be a useful task when you want to adjust the layout, accommodate annotations or comments, or prepare the document for printing or presentation.

This article introduces how to modify the margins of a PDF document using the Spire.PDF for Python library. You will discover techniques to both increase and reduce the margins of your PDFs, enabling you to customize the layout according to your specific requirements.

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 this tutorial: How to Install Spire.PDF for Python on Windows

Increase the Margins of a PDF Document in Python

In Spire.PDF for Python, there isn't a direct method to modify the margins of an existing PDF document. However, you can increase the margins by creating a new PDF document with a page size equal to the original document's page size plus the increased margin values. Then, copy and paste (draw) each page of the original document into the appropriate place on the new document page.

The following are the steps to increase the margins of a PDF document using Python.

  1. Create a PdfDocument object called "originalPdf" and load the original PDF document.
  2. Create another PdfDocument object called "newPdf" for creating a new PDF document.
  3. Specify the desired increase values for the top, bottom, left, and right margins.
  4. Calculate the new page size by adding the margin increase values to the original page dimensions.
  5. Create a template based on the original PDF page using PdfPageBase.CreateTemplate() method.
  6. Add a new page to the "newPdf" document with the calculated page size using PdfDocument.Pages.Add() method.
  7. Draw the template onto the new page at the appropriate location to using PdfTemplate.Draw() method.
  8. Repeat steps 5-7 for each page in the original PDF document.
  9. Save the "newPdf" object to a PDF file.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
originalPdf = PdfDocument()

# Load a PDF file
originalPdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Get the first page
firstPage = originalPdf.Pages[0]

# Create another PdfDocument object for creating new document
newPdf = PdfDocument()

# Set the increase values of the margins
marginsToAdd = newPdf.PageSettings.Margins
marginsToAdd.Top = 40
marginsToAdd.Bottom = 40
marginsToAdd.Left = 40
marginsToAdd.Right = 40

# Calculate the new page size
sizeF = SizeF(firstPage.Size.Width + marginsToAdd.Left + marginsToAdd.Right, firstPage.Size.Height + marginsToAdd.Top + marginsToAdd.Bottom)

# Iterate through the pages in the original document
for i in range(originalPdf.Pages.Count):

    # Create a template based on a specific page
    pdfTemplate = originalPdf.Pages[i].CreateTemplate()

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

    # Draw template on the page
    pdfTemplate.Draw(page, 0.0, 0.0)

# Save the new document
newPdf.SaveToFile("Output/IncreaseMargins.pdf", FileFormat.PDF)

# Dispose resources
originalPdf.Dispose()
newPdf.Dispose()

Python: Change the Margins of a PDF Document

Reduce the Margins of a PDF Document in Python

Similarly, you can reduce the margins by creating a new PDF document with a page size equal to the page size of the original document minus the margin value to be reduced. Then, copy and paste (draw) each page of the original document into the appropriate place on the new document page.

To reduce the margins of a PDF document using Python, follow these steps:

  1. Create a PdfDocument object called "originalPdf" and load the original PDF document.
  2. Create another PdfDocument object called "newPdf" for creating a new PDF document.
  3. Specify the desired reduction values for the top, bottom, left, and right margins.
  4. Calculate the new page size by subtracting the margin value to be reduced from the original page size.
  5. Create a template based on the original PDF page using PdfPageBase.CreateTemplate() method.
  6. Add a new page to the "newPdf" document with the calculated page size using PdfDocument.Pages.Add() method.
  7. Draw the template onto the new page at the appropriate location using PdfTemplate.Draw() method.
  8. Repeat steps 5-7 for each page in the original PDF document.
  9. Save the "newPdf" object to a PDF file.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
originalPdf = PdfDocument()

# Load a PDF file
originalPdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Get the first page
firstPage = originalPdf.Pages[0]

# Create another PdfDocument object
newPdf = PdfDocument()

# Set the reduction value of the margins
topToReduce = 20.0
bottomToReduce = 20.0
leftToReduce = 20.0
rightToReduce = 20.0

# Calculate the new page size
sizeF = SizeF(firstPage.Size.Width - leftToReduce - rightToReduce, firstPage.Size.Height - topToReduce - bottomToReduce)

# Iterate through the pages in the original document
for i in range(originalPdf.Pages.Count):

    # Create a template based on a specific page
    pdfTemplate = originalPdf.Pages[i].CreateTemplate()

    # Add a page to the new PDF
    page = newPdf.Pages.Add(sizeF, PdfMargins(0.0))

    # Draw template on the page
    pdfTemplate.Draw(page, -leftToReduce, -topToReduce)

# Save the new document
newPdf.SaveToFile("Output/ReduceMargins.pdf", FileFormat.PDF)

# Dispose resources
originalPdf.Dispose()
newPdf.Dispose()

Python: Change the Margins of a PDF Document

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.