Add Watermark via Python

Add watermarks to PDF document programmatically using Aspose.PDF for Python for .NET

Add Watermark to PDF File Using Python for .NET Tool

In order to add Watermark to PDF File, we’ll use Aspose.PDF for Python 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 Watermark via Python


To try the code in your environment, you need Aspose.PDF for Python.

  1. Load the PDF with an instance of Document.
  2. Create an instance of WatermarkArtifact.
  3. Set properties of WatermarkArtifact object.
  4. Add watermark using method Add of Aspose.Pdf.Page.Artifacts collection class.
  5. Save PDF file

Add Watermark to PDF - Python

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

doc = apdf.Document(path_infile)

artifact = apdf.WatermarkArtifact()
artifact.set_image("watermark.jpg")

artifact.artifact_horizontal_alignment = apdf.HorizontalAlignment.CENTER
artifact.artifact_vertical_alignment = apdf.VerticalAlignment.CENTER
artifact.rotation = 15
artifact.opacity = 1
artifact.is_background = True
doc.pages[1].artifacts.append(artifact)

# save result pdf to file
doc.save(path_outfile)