How to Add a Watermark to a PDF in Python

Protecting and denoting ownership of your information can be as easy as placing a non-removable watermark over your PDF documents. These watermarks can take many forms, such as stating draft level, confidentiality, and team ownership. The following API will automatically place a specified watermark over your PDF documents; you can customize the desired text, font, size, color, and transparency.

Image for post
Image for post

To use this API, you can run this command to install the SDK:

pip install cloudmersive-convert-api-client

Then, you can call the function:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))
watermark_text = 'watermark_text_example' # str | Watermark text to add to the PDF (required)
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
font_name = 'font_name_example' # str | Font Family Name for the watermark text; default is Times New Roman (optional)
font_size = 8.14 # float | Font Size in points of the text; default is 150 (optional)
font_color = 'font_color_example' # str | Font color in hexadecimal or HTML color name; default is Red (optional)
font_transparency = 8.14 # float | Font transparency between 0.0 (completely transparent) to 1.0 (fully opaque); default is 0.5 (optional)
try:
# Add a text watermark to a PDF
api_response = api_instance.edit_pdf_watermark_text(watermark_text, input_file, font_name=font_name, font_size=font_size, font_color=font_color, font_transparency=font_transparency)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditPdfApi->edit_pdf_watermark_text: %s\n" % e)

The API Key for this function can be retrieved at no cost and with no commitment on the Cloudmersive website. This will give you access to 800 monthly calls across our entire API library.

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store