Adding Page Stamps to PDF with Python
Contents
[
Hide
]
Aspose.PDF for Python via .NET shows how to apply a page stamp (watermark or overlay) to a specific page in a PDF Document. The page stamp can be an existing PDF page used as a background or foreground layer (see PdfPageStamp). This is useful for adding logos, watermarks, or other repetitive page content.
- Open the PDF document using
ap.Document()(seeDocument). - Create a
PdfPageStampobject using the PDF page or file to use as the stamp (seePdfPageStamp). - Set the stamp properties, e.g.,
background = Trueto place it behind the content. - Add the stamp to a specific page using
document.pages[page_number].add_stamp(page_stamp)(seePage.add_stamp()andPageCollection). - Save the modified PDF to the specified output file using
Document.save().
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)