Python: Add Text Watermarks to PDF Documents

Text watermarks in PDF documents are semitransparent text displayed on PDF pages. By displaying copyright, confidentiality, author, company, or other information on PDF pages, watermarks can protect document copyright and confidential information, facilitate document identification and management, and help with branding. Whether it is a publicly released document or a document that needs to be kept confidential, adding a text watermark to the document will greatly help with document protection and personalization. This article will show how to use Spire.PDF for Python to add text watermarks to PDF documents through Python programs.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Add Single-Line Text Watermarks to PDF Documents

A single-line text watermark is usually text displayed slanted in the center of a page. Below are the detailed steps to add a single-line text watermark in a PDF document:

  • Create an object of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Create a font.
  • Specify the watermark text and calculate the position of the watermark.
  • Loop through the pages in the document to add watermarks.
  • Get a page using PdfDocument.Pages.get_Item() method.
  • Set the transparency using PdfPageBase.Canvas.SetTransparency() method.
  • Translate the page coordinate system to the specified position using PdfPageBase.Canvas.TranslateTransform() method and rotate the coordinate system 45 degrees counterclockwise using PdfPageBase.Canvas.RotateTransform() method, so that the watermark will be tilted 45 degrees counterclockwise in the center of the page.
  • Draw the watermark text on the page using PdfPageBase.Canvas.DrawString() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
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("Sample.pdf")

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

# Specify the watermark text and calculate the position of the watermark
text = "DRAFT"
set1 = float (font.MeasureString(text).Width * math.sqrt(2) / 4)
set2 = 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.5)
    # Translate the page coordinate system to the specified position 
    page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - set1 - set2, 
                                   page.Canvas.Size.Height / 2 + set1 - set2)
    # 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_Cyan(), 0.0, 0.0)

# Save the document  
pdf.SaveToFile("output/SingleLineTextWatermark.pdf")
pdf.Close()

Python: Add Text Watermarks to PDF Documents

Add Multi-Line Text Watermarks to PDF Documents

Multi-line text watermarks are watermarks that are regularly displayed multiple times on a PDF page. Users can create a multi-line text watermark using a PdfTillingBrush object and the repeating times of the watermark can be controlled by modifying the size of the object. The detailed steps are as follows:

  • Create an object of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Create a font and specify the watermark text.
  • Loop through the pages in the document to add watermarks.
  • Get a page using PdfDocument.Pages.get_Item() method.
  • Create an object of PdfTillingBrush class and set its size.
  • Set the transparency using PdfTillingBrush.Graphics.SetTransparency() method.
  • Translate the coordinate system to the specified position using PdfTillingBrush.Graphics.TranslateTransform() method and rotate the coordinate system 45 degrees counterclockwise using PdfTillingBrush.Graphics.RotateTransform() method, so that the watermark will be tilted 45° counterclockwise in the center of each repetition.
  • Draw the watermark text on the tilling brush using PdfTillingBrush.Graphics.DrawString() method.
  • Draw the watermark on the page using PdfPageBase.Canvas.DrawRectangle() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

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

# Load a PDF document
pdf.LoadFromFile("Sample.pdf")

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

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

# Iterate through the pages of the document
for i in range(pdf.Pages.Count):
    # Get a page
    page = pdf.Pages.get_Item(i)
    # Create an object of PdfTilingBrush class
    brush = PdfTilingBrush(SizeF(page.Canvas.ClientSize.Width / float(3), page.Canvas.ClientSize.Height / float(3)))
    # Set the transparency of the watermark
    brush.Graphics.SetTransparency(0.3)
    brush.Graphics.Save()
    # Translate the coordinate system of the brush to the specified position
    brush.Graphics.TranslateTransform(brush.Size.Width / float(2), brush.Size.Height / float(2))
    # Rotate the coordinate system 45 degrees counterclockwise
    brush.Graphics.RotateTransform(-45.0)
    # Draw the watermark text on the brush
    brush.Graphics.DrawString(text, font, PdfBrushes.get_Violet(), 0.0, 0.0, PdfStringFormat(PdfTextAlignment.Center))
    brush.Graphics.Restore()
    brush.Graphics.SetTransparency(1.0)
    # Draw the watermark on the page
    page.Canvas.DrawRectangle(brush, RectangleF(PointF(0.0, 0.0), page.Canvas.ClientSize))

# Save the PDF document
pdf.SaveToFile("output/MultiLineTextWatermark.pdf")
pdf.Close()

Python: Add Text Watermarks to PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.