DEV Community

jelizaveta
jelizaveta

Posted on

Adding a Watermark to PDF Using Python

In the modern digital office environment, PDF has become a widely used file format, especially when document formatting needs to be preserved. Adding a watermark is a common method to protect the content of documents. This article will introduce how to add a watermark to PDF files using Python, with detailed steps illustrated through code examples.

1. Preparation

To implement the addition of a PDF watermark, we will use Spire.PDF, a powerful PDF manipulation library that makes it easy to handle PDF documents. You can download and install the relevant library from the Spire.PDF official website. Ensure that Python and the necessary dependencies are configured in your environment.

2. Steps to Implement

1. Import Necessary Libraries

First, we need to import the relevant classes from Spire.PDF. Below are the basic import statements:

from spire.pdf import *
from spire.pdf.common import *
import math
Enter fullscreen mode Exit fullscreen mode

2. Create a PdfDocument Object

Next, we need to create an object of the PdfDocument class and load the PDF file to be processed.

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

# Load the PDF document from the specified path
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")
Enter fullscreen mode Exit fullscreen mode

Here, we can replace the path with the actual location of your PDF file.

3. Set Watermark Parameters

We need to define the content and font style of the watermark. In this example, we will use "Do Not Copy" as the watermark text.

# Create an object of PdfTrueTypeFont class for the watermark font
font = PdfTrueTypeFont("SimHei", 48.0, 0, True)

# Specify the watermark text
text = "CONFIDENTIAL"
Enter fullscreen mode Exit fullscreen mode

The font can be adjusted to your needs, such as using different text styles or sizes.

4. Measure Text Dimensions

To ensure the watermark text displays correctly in the PDF, we need to measure the text's width and height:

# Measure the text dimensions to ensure proper positioning
text_width = font.MeasureString(text).Width
text_height = font.MeasureString(text).Height
Enter fullscreen mode Exit fullscreen mode

5. Loop Through Each Page

Now, we will loop through each page of the PDF file and add the watermark to each page.

# Loop through each page in the document
for i inrange(doc.Pages.Count):
# Get the current page
    page = doc.Pages.get_Item(i)

# Save the current canvas state
    state = page.Canvas.Save()

# Calculate the center coordinates of the page
    x = page.Canvas.Size.Width / 2
    y = page.Canvas.Size.Height / 2

# Translate the coordinate system to center, making the center of the page the origin (0, 0)
    page.Canvas.TranslateTransform(x, y)

# Rotate the canvas counter-clockwise by 45 degrees to display the watermark
    page.Canvas.RotateTransform(-45.0)

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

# Draw the watermark text at the center position using negative offsets
    page.Canvas.DrawString(text, font, PdfBrushes.get_Blue(), PointF(-text_width / 2, -text_height / 2))

# Restore the canvas state to prevent transformations affecting subsequent drawings
    page.Canvas.Restore(state)
Enter fullscreen mode Exit fullscreen mode

In the code above, we followed these steps:

  • Get the current page and save the canvas state.
  • Calculate the center coordinates of the page and translate the coordinate system to the center.
  • Rotate the canvas counter-clockwise to display the watermark.
  • Set transparency to make the watermark less prominent.
  • Draw the watermark text at the center of the page.
  • Restore the canvas state, ensuring subsequent operations are unaffected.

6. Save the Modified PDF

Finally, we save the modified PDF document to a new file:

# Save the modified document to a new PDF file
doc.SaveToFile("output/TextWatermark.pdf")
doc.Dispose()
Enter fullscreen mode Exit fullscreen mode

Here, we specify the path for the new output file to ensure a successful save.

3. Conclusion

Through the steps outlined above, we successfully added a watermark to a PDF file using Python. This method is simple and effective, suitable for situations where document content needs protection.

In practical applications, the style and content of the watermark can be flexibly adjusted as needed. Whether for corporate documents, personal works, or simply to prevent unauthorized copying, watermarks can better protect your intellectual property.

If you have more PDF-related needs, Spire.PDF also supports many other features, such as merging and splitting PDF documents, extracting text and images, etc. We hope this article provides you with practical references for manipulating PDFs using Python.

Top comments (0)