Adding Page Stamps to PDF with Python

Contents
[ ]

Aspose.PDF for Python via .NET shows how to apply a page stamp (watermark or overlay) to a specific page in a PDF document using Aspose.PDF for Python. The page stamp can be an existing PDF page used as a background or foreground layer. This is useful for adding logos, watermarks, or other repetitive page content.

  1. Open the PDF document using ‘ap.Document()’.
  2. Create a ‘PdfPageStamp’ object using the PDF page or file to use as the stamp.
  3. Set the stamp properties, e.g., ‘background = True’ to place it behind the content.
  4. Add the stamp to a specific page using ‘document.pages[page_number].add_stamp(page_stamp)’.
  5. Save the modified PDF to the specified output file.

import os
import aspose.pdf as ap

# Global configuration
DATA_DIR = "your path here"

def add_page_stamp(input_file_name, page_stamp_name, output_file_name):
    # Open PDF document
    document = ap.Document(input_file_name)

    page_stamp = ap.PdfPageStamp(page_stamp_name, 1)
    page_stamp.background = True

    # Add stamp to particular page
    document.pages[1].add_stamp(page_stamp)

    document.save(output_file_name)