Add Page Stamp to PDF via Python

Create page stamp programmaticaly using Aspose.PDF for Python for .NET Library

How to add Page Stamps to PDF Using Python for .NET Library

In order to work with page stamp into PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for python-net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

Add Page Stamp to PDF Document Python


You need Aspose.PDF for Python to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Open a PDF document using Document object.
  3. Create a Page Stamp and define its properties.
  4. Add the Stamp to Page using AddStamp method

Add Page Stamp to PDF - Python

import aspose.pdf as ap
# Open document
dataDir = "..."
inputFileName = "sample-4pages.pdf"
outputFileName = "AddPageStamp_out.pdf"
pageStampFileName = "PageStamp.pdf"

pdfDocument = ap.Document(dataDir + inputFileName)
bluePageStamp = ap.PdfPageStamp(dataDir + pageStampFileName, 1)
bluePageStamp.set_height(800)
bluePageStamp.set_background(True)

plumPageStamp = ap.PdfPageStamp(dataDir + pageStampFileName, 2)
plumPageStamp.set_height(800)
plumPageStamp.set_background(True)

for i in range(1, 6):
    if (i % 2 == 0):
        pdfDocument.pages[i].addStamp(bluePageStamp)
    else:
        pdfDocument.pages[i].addStamp(plumPageStamp)

pdfDocument.save(dataDir + outputFileName)