Add Margins to PDF Pages

Contents
[ ]

Adding margins to PDF pages can enhance readability, prepare documents for printing, or allocate space for annotations. Using Aspose.PDF for Python, developers can programmatically add margins to specific pages of a PDF without modifying the content layout.

In this code snippet, the PdfFileEditor class is used to add 0.5-inch margins to pages 1 and 3 of the input document. The margins are defined in points (1 inch = 72 points) and applied individually to the left, right, top, and bottom of each page.

  1. Open the source PDF document.
  2. Create a ‘PdfFileEditor’ instance.
  3. Define the margins and pages to modify.
  4. Apply margins using the ‘add_margins’ method.
  5. Save the updated PDF to the output file.
from io import FileIO
import sys
from os import path
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


# Add Margins to PDF Pages
def add_margins_to_pdf_pages(infile, outfile):
    # Create a PdfFileEditor object
    pdf_editor = pdf_facades.PdfFileEditor()
    # Define the margins to be added (in points)
    left_margin = 36  # 0.5 inch
    right_margin = 36  # 0.5 inch
    top_margin = 36  # 0.5 inch
    bottom_margin = 36  # 0.5 inch

    pdf_editor.add_margins(
        infile, outfile, [1, 3], left_margin, right_margin, top_margin, bottom_margin
    )