Add Watermark to PDF using PyPDF4 in Python
We are quite familiar with the most commonly used data format of PDF (Portable Document Format), with an extension .pdf. While the PDF was originally invented by Adobe, though maintained by ISO now, is used to present and exchange documents across various OSs.
It could be installed using pip or conda depending on whether you are using python or Anaconda.
But, this is how you will install using pip for the specific version:
pip install PyPDF4==1.27.0
Otherwise in general:
pip install PyPDF4
Note: Compatible with Python versions 2.6, 2.7, and 3.2 – 3.5.
Let’s add a watermark to our PDF File (the main purpose of this article). You must be knowing that watermarks are the way to claim our rights and intellectual properties in our rightful documents and hence it is very important.
Below is the implementation.
- Read the pages of the originally given input pdf (using PdfFileReader() class)
- Read the 1st page of the watermark (using PdfFileReader() class)
- create a pdf writer object using PdfFileWriter() class
- Next, is to iterate over the pages in the input_pdf.
- Call .mergePage() and pass it the watermark_page.
- Add this merged page to pdf writer object using addPage() method.
- Write it to the output page using the write() method.