Add Page Stamp to PDF using 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, use Aspose.PDF for Python via .NET, a powerful and easy-to-use API. Open PyPI, search for aspose-pdf, and install it. Alternatively, run the command:

Console

pip install aspose-pdf

Add Page Stamp to PDF Document Python


You need Aspose.PDF for Python via .NET 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 apdf


from os import path

path_infile = path.join(self.data_dir, infile)
path_input_stamp = path.join(self.data_dir, "image.pdf")
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)
bluePageStamp = apdf.PdfPageStamp(path_input_stamp, 1)
bluePageStamp.set_height(800)
bluePageStamp.set_background(True)

plumPageStamp = apdf.PdfPageStamp(path_input_stamp, 2)
plumPageStamp.set_height(800)
plumPageStamp.set_background(True)

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

document.save(path_outfile)