Add a Watermark to a PDF Document with Python

Alexander Stock
3 min readJan 8, 2024

--

Adding a watermark to a PDF document can help protect your content, establish ownership, or indicate confidentiality. Watermarks can be in the form of text or images that are placed over the existing content of the PDF file. Text watermarks typically contain information like “Confidential” or “Draft,” while image watermarks may include logos or graphical elements.

In this article, I am going to introduce how to programmatically add a watermark to a PDF document in Python with the help of Spire.PDF for Python.

Install Dependency

This solution requires Spire.PDF for Python to be installed as the dependency, which is a PDF library for reading, creating and manipulating PDF documents in a Python program. You can install it by running the following pip command.

pip install Spire.PDF

Add a Text Watermark to PDF with Python

With Spire.PDF for Python, you can draw text in custom fonts, colors, orientations, and angles at specific locations on a PDF page. Below are the steps to add a text watermark to PDF with Python.

  • Create a PdfDocument object.
  • Load a PDF file using LoadFromFile method.
  • Iterate through the pages in the document, and draw text on each page at the specified location using DrawString method.
  • Save the changes using SaveToFile method.

The following example shows how to insert a text watermark to the center of each page, rotating it 45 degrees clockwise.

from spire.pdf import *
from spire.pdf.common import *
import math

# Create an object of PdfDocument class
pdf = PdfDocument()

# Load a PDF document
pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Create an object of PdfTrueTypeFont class
font = PdfTrueTypeFont("HarmonyOS Sans SC", 48.0, 0, True)

# Specify the watermark text
text = "DO NOT COPY"

# Calculate two offsets that are used to determine the position of the watermark text
offset1 = float (font.MeasureString(text).Width * math.sqrt(2) / 4)
offset2 = float (font.MeasureString(text).Height * math.sqrt(2) / 4)

# Loop through the pages in the document
for i in range(pdf.Pages.Count):

# Get a page
page = pdf.Pages.get_Item(i)

# Set the transparency of the watermark
page.Canvas.SetTransparency(0.8)

# Translate the page coordinate system to the specified position
page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2,
page.Canvas.Size.Height / 2 + offset1 - offset2)

# Rotate the coordinate system 45 degrees counterclockwise
page.Canvas.RotateTransform(-45.0)

# Draw the watermark on the page
page.Canvas.DrawString(text, font, PdfBrushes.get_Gray(), 0.0, 0.0)

# Save the document
pdf.SaveToFile("output/TextWatermark.pdf")
pdf.Close()
Add Text Watermark to PDF

P.S. The red evaluation message can be removed by applying a trial license.

Add an Image Watermark to PDF with Python

The steps for adding an image watermark are similar to those for adding a text watermark. Below are the steps to add an image watermark to PDF with Python.

  • Create a PdfDocument object.
  • Load a PDF file using LoadFromFile method.
  • Iterate through the pages in the document, and draw an image on each page at the specified location using DrawImage method.
  • Save the changes using SaveToFile method.

The following example shows how to add an image watermark to the center of each page.

from spire.pdf import *
from spire.pdf.common import *

# Create an object of PdfDocument class
pdf = PdfDocument()

# Load a PDF document
pdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Load the watermark image
image = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\logo.png")

# Get the width and height of the image
imageWidth = float(image.Width)
imageHeight = float(image.Height)

# Loop through the pages in the document
for i in range(pdf.Pages.Count):

# Get a page
page = pdf.Pages.get_Item(i)

# Set the transparency of the watermark
page.Canvas.SetTransparency(0.5)

# Get the width and height of the page
pageWidth = page.ActualSize.Width
pageHeight = page.ActualSize.Height

# Draw the image at the center of the page
page.Canvas.DrawImage(image, pageWidth/2 - imageWidth/2, pageHeight/2 - imageHeight/2, imageWidth, imageHeight)

# Save the document
pdf.SaveToFile("output/ImageWatermark.pdf")
pdf.Close()
Add Image Watermark to PDF

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.