How to Add a Text Watermark to Any PDF Using Python

Watermarks are a clear, yet subtle way of identifying the owner of a particular file. While people may find them annoying, they serve their purpose gracefully.

With a watermark, you can not only protect your files, but also add descriptive text, location, time of generation, and more. So, the next time you turn in your assignment or journal paper, be sure to add a watermark using this simple Python program.

4

What Is the ReportLab Module?

To add a watermark to a PDF file, you’ll need a good library like the ReportLab module. It’s an open-source library that lets you create PDFs, as well as add charts, data, and graphics to them. This module helps you to generate dynamic PDfs, convert XML files to PDFs, and generate reports. To install ReportLab on your system, open a terminal and type:

This installs ReportLab in your Python environment.

Watermark image of a document displayed on an e-reader

What Is the PyPDF2 Module?

PyPDF2 is a popular library that you can use to perform a variety of useful PDF operations. These include cropping, merging, splitting, rotating, transforming, adding data, stamping, and watermarking. You can also encrypt and decrypt, extract text, images, and metadata, and read or add PDF annotations. To install PyPDF2 into your system, open a terminal and type:

This installs PyPDF2 in your Python environment.

Sai Ashish Text Watermark

How to Add the Desired Watermark to PDF Using Python

Adding a watermark to a PDF file using Python is pretty simple. You’re going to create a canvas and draw the text watermark using your favorite color and direction, then save it as a PDF.

After the generation of this one-page watermark design, you can open both PDFs and merge them page by page. Voilà, your watermarked PDF is then ready.

PDF Text Watermark Sample

Begin your program by importingcanvasfrompdfgen, located in thereportlabmodule. The pdfgen interface acts at the lowest level to generate PDFs. You can visualize it as painting a document design and structure on a page. The canvas class provides the operations needed to do so.

You can think of the canvas as a sheet of plain paper with Cartesian coordinates. The point of origin (0,0) is in the lower left corner. The values of the x and y coordinates increase as points move away from the origin, upwards and to the right.

someone erasing the keyboard with pdf logo on screen

From the ReportLab library, importinch,color,andpagesize. The program will use inches as the unit of measurement. There are two color formats available under ReportLab: theRGBmodel, and theCYMKmodel. RGB is preferable, but if you want more control over the level of colors that appear when you print the PDF, you can opt for CYMK instead.

As the name suggests, the size of the page is set toA4. Additionally, import thePdfFileReaderandPdfFileWriterfromPyPDF2to perform read and write operations on PDF files.

Define a function calledmakeWatermark()to create the one-page watermark PDF file that you will later use to apply on the pages of the desired document. Ask the user to input the text of the watermark using the input statement.

Use the canvas to generate a drawing area with the name watermark.pdf and A4 pagesize. Thetranslate()function moves the origin to the upper left corner.

Set the file of the text to grey with an opacity of 0.6; on a scale of 0-1, 0 is the most transparent and 1 is the most opaque. Set the font style to Helvetica with a size of 50. Apply a rotation of 45 degrees in the counterclockwise direction. Pass the x and y coordinates along with the text to draw it over the canvas and save it using the save() function.

The watermark PDF file that the above function generates looks like this:

Define a function namedmakepdf()to watermark the pages of the PDF. Input the name of the file you want to watermark, including any.pdfextension. Initialize a variable to store the name of the watermarked PDF. Open both files in the read binary mode.

Use thePdfFileReader()to instantiate an object that represents each PDF. Use thegetPage()function to get the first and only page of the watermarked PDF.

Now it’s time to merge the watermark page with each page in the target PDF.

Create an object of the PdfFileWriter class to use for generating the output PDF. Define a loop that iterates over each page in the desired PDF. The getNumPages() function returns the total number of pages in a PDF. Use the getPage() function to get the page and merge it with the watermark page stored earlier.

Use the PdfFileWriter object to add each merged page to the final PDF. Once you’ve created the output PDF, open the output file in write binary mode and use the write() function to save the watermarked pdf to this file.

Finally, call both functions—makeWatermark() and makepdf()—to create the watermarked PDF file:

The text watermarked PDF generated using the above Python program looks like this:

Additionally, you can follow this five-step method tocreate and add a watermark to any image using Canva. You can also usetechniques to watermark your videosin just a few minutes.

Tools to Remove Watermarks From PDFs, Images, and Videos

There might be times when you added a watermark by mistake and want to remove it. You can use online tools such as Soda PDF, Microsoft Word, Google Docs, and PDF Zorro for free. For images, you can explore tools such as Wondershare PixCut, Inpaint, Aiseesoft, Apowersoft, and Cutout.pro.

For free watermark removal in videos, use Apowersoft, 123apps, PickFrom Blur, and BeeCut.

However, remember not to use any copyrighted material without the owner’s permission.

A PDF watermark can distract you from the document at hand, so the best solution is often to just remove it.

Turn these settings on, and your iPhone will be so much better than before.

The fix was buried in one tiny toggle.

Windows is great, but adding this makes it unstoppable.

Don’t let someone else take over your phone number.

The key is not to spook your friends with over-the-top shenanigans.

Technology Explained

PC & Mobile