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.

Installation

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. 



Adding watermarks

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.

Original PDF:

Watermark PDF:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# compatible with Python versions 2.6, 2.7,
# and 3.2 - 3.5. (pip3 install pypdf4)
from PyPDF4 import PdfFileWriter, PdfFileReader
import PyPDF4
  
  
PyPDF4.PdfFileReader('GFG.pdf')
  
  
def put_watermark(input_pdf, output_pdf, watermark):
    
    # reads the watermark pdf file through 
    # PdfFileReader
    watermark_instance = PdfFileReader(watermark)
      
    # fetches the respective page of 
    # watermark(1st page)
    watermark_page = watermark_instance.getPage(0)
      
    # reads the input pdf file
    pdf_reader = PdfFileReader(input_pdf)
      
    # It creates a pdf writer object for the
    # output file
    pdf_writer = PdfFileWriter()
  
    # iterates through the original pdf to
    # merge watermarks
    for page in range(pdf_reader.getNumPages()):
          
        page = pdf_reader.getPage(page)
          
        # will overlay the watermark_page on top 
        # of the current page.
        page.mergePage(watermark_page)
          
        # add that newly merged page to the
        # pdf_writer object.
        pdf_writer.addPage(page)
  
    with open(output_pdf, 'wb') as out:
          
        # writes to the respective output_pdf provided
        pdf_writer.write(out)
  
if __name__ == "__main__":
    put_watermark(
        input_pdf='GFG.pdf'# the original pdf
        output_pdf='watermark_added1.pdf'# the modified pdf with watermark
        watermark='geeks.pdf'  # the watermark to be provided
    )

chevron_right


Output:

Explanation:

  1. Read the pages of the originally given input pdf (using PdfFileReader() class)
  2. Read the 1st page of the watermark (using PdfFileReader() class)
  3. create a pdf writer object using PdfFileWriter() class
  4. Next, is to iterate over the pages in the input_pdf. 
  5. Call .mergePage() and pass it the watermark_page.
  6. Add this merged page to pdf writer object using addPage() method.
  7. Write it to the output page using the write() method.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.